1.QQ 邮箱开通 SMTP 服务

开通方法

2.PHP开启 openssl 扩展

3.安装 Github PHPMailer

composer require phpmailer/phpmailer

4.代码

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require_once __DIR__ ."/vendor/autoload.php";

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_CLIENT;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Host       = 'smtp.qq.com';                         // Set the SMTP server to send through
    $mail->Port       =  465;                                   // TCP port to connect to  , 465
    $mail->CharSet    = "UTF-8";
    $mail->FromName   = "lvpeilin";
    $mail->Username   = '422615924@qq.com';                     // SMTP username
    $mail->Password   = '****';                     // SMTP password, 授权码
    $mail->setFrom('422615924@qq.com', 'Dyspace');
    $mail->addAddress('422615924@qq.com', 'lvpeilin');     // Add a recipient

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐