原文很详尽
参考:http://blog.youkuaiyun.com/baidu_30000217/article/details/51550259
使用composer安装phpmailer
composer require phpmailer/phpmailer
php代码,使用的phpmailer官方demo
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.qq.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
//smtp登录的账号 这里填入字符串格式的qq号即可
$mail->Username = '123@qq.com'; // SMTP username
//smtp登录的密码 使用生成的授权码(通过qq邮箱活得的授权码)
$mail->Password = 'xxxxxxxxi'; // SMTP password
//设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
//设置ssl连接smtp服务器的远程服务器端口号,以前的默认是25,但是现在新的好像已经不可用了 可选465或587
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('123@qq.com', 'Mailer');
//设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址
// 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大
$mail->addAddress('123@qq.com', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//答复
$mail->addReplyTo('123@qq.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//Attachments 附件
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//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.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}