发送信息到邮箱的第三方扩展库PHPMailer使用方法
一.下载
使用composer下载PHPMailer :composer require phpmailer/phpmailer
二.使用实例
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
function mailto($to, $title, $content)
{
$mail = new PHPMailer(true); // 开发环境写成true 显示异常 生产环境改为false
// QQ邮箱
try {
//Server settings 服务器配置
$mail->SMTPDebug = 0; // 0 表示关闭异常提示 2开启调试模式
$mail->CharSet = 'utf-8'; // 字符编码
$mail->isSMTP(); // 使用SMTP,只接收信息
$mail->Host = 'smtp.qq.com'; // SMTP服务器地址
$mail->SMTPAuth = true; // 启用SMTP身份验证
$mail->Username = '327*****780@qq.com'; // SMTP username
$mail->Password = 'zqa*****yypchag'; // SMTP password 开启SMTP授权码
$mail->SMTPSecure = 'ssl'; // 使用ssl加密
$mail->Port = 465; // 端口
//Recipients 接受信息
$mail->setFrom('327*****780@qq.com', '*****企业'); // 发送方邮箱
$mail->addAddress($to); // 接受方邮箱
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $title; // 邮箱标题
$mail->Body = $content; //邮件内容
$return = $mail->send();
} catch (Exception $e) {
exception($mail->ErrorInfo, 1001); // 发送失败代码
}
// 网易邮箱
try {
//Server settings 服务器配置
$mail->SMTPDebug = 0;
$mail->CharSet = 'utf-8';
$mail->isSMTP();
$mail->Host = 'smtp.163.com';
$mail->SMTPAuth = true;
$mail->Username = 'hy*****way@163.com';
$mail->Password = 'UPO*****JKAXYGK';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
//Recipients 接受信息
$mail->setFrom('hy*****way@163.com', 'hy*****way');
$mail->addAddress($to);
// Content
$mail->isHTML(true);
$mail->Subject = $title;
$mail->Body = $content;
$return = $mail->send();
} catch (Exception $e) {
exception($mail->ErrorInfo, 1001);
}
}