安装phpmailer
composer require phpmailer/phpmailer
使用QQ邮箱的smtp做服务器
打开QQ邮箱 ==》 账号 ==》POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,开启POP3/SMTP服务
image.png先看下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 = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.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. Mailer Error: ', $mail->ErrorInfo;
}
现在来封装分发送邮件的公共方法
/**
* 发送邮件
* @param $subject 邮件标题
* @param $body 邮件内容
*/
function sendEmail($subject, $body)
{
$mail=new PHPMailer();
$mail->SMTPDebug = 0; //邮件调试模式
$mail->isSMTP(); //设置邮件使用SMTP
$mail->Host = config('email.host'); // 设置邮件程序以使用SMTP
$mail->CharSet='UTF-8'; // 设置邮件内容的编码
$mail->SMTPAuth = true; // 启用SMTP验证
$mail->Username = config('email.username'); // SMTP username
$mail->Password = config('email.password'); // SMTP password
$mail->SMTPSecure = 'ssl'; // 启用TLS加密,`ssl`也被接受
$mail->Port = 465; // 连接的TCP端口,tls ==> 587
$mail->setFrom(config('email.send_from'), config('email.send_user')); //设置发件人
$mail->addAddress(config('email.receive_from'), config('email.receive_user')); // Add a recipient
$mail->isHTML(true); // 将电子邮件格式设置为HTML
$mail->Subject = $subject; //邮件标题
$mail->Body = $body; //邮件内容
if ($mail->send())
{
return true;
}
else
{
return false;
}
}
配置文件:application/extra/email.php
将对应的参数改成自己的
return [
'host' => 'smtp.qq.com', //SMTP主机地址
'username' => 'your_qq@qq.com', //SMTP账号
'password' => 'your _auth_code', //账号授权码
'send_from' => 'your_qq@qq.com', //发件人邮箱
'send_user' => 'Chase', //发件人姓名
'receive_from' => 'your_163@163.com', //收件人邮箱
'receive_user' => '渣渣辉', //收件人姓名
];
测试:
image.png
网友评论