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 = '[email protected]'; // SMTP username
$mail->Password = '*************************'; // SMTP password, 授权码
$mail->setFrom('[email protected]', 'Dyspace');
$mail->addAddress('[email protected]', '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}";
}