使用php+gmail 发送邮件

时间:2021-08-01 04:21:59
 <?php
namespace app\index\controller; use think\Controller;
use PHPMailer\PHPMailer; class Test extends Controller
{ public function index2(){ $mail = new PHPMailer(); $body = "自定义您的收件箱 如果您想让某封邮件位于其他类别中,则可以将该邮件移至您所需的类别中。在移动设备上,您甚至可以选择允许哪些类别创建通知。更多自定义提示 要了解Gmail收件箱的详情,请查看帮助中心或观看此视频"; $mail->IsSMTP(); // telling the class to use SMTP $mail->Host = "mail.gmail.com"; // SMTP server $mail->SMTPDebug = 2; // enables SMTP debug information (for testing) $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->Username = "111111@gmail.com"; // GMAIL username $mail->Password = "password"; // GMAIL password $mail->SetFrom('wangdana', 'First Last'); $mail->AddReplyTo("11111@gmail.com","First Last"); $mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail->MsgHTML($body); $mail->CharSet = "utf-8"; // 这里指定字符集! $address = "12341234@163.com"; $mail->AddAddress($address, "John Doe"); if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } } }

值得注意的是,

首先你的服务器要能链接谷歌,有外网,

第二,谷歌邮箱要开启imap访问,

第三,谷歌账号要开启低安全性应用登陆认证。开启地址:  https://myaccount.google.com/lesssecureapps