首先,准备工作,登录Gmail ,
图片.png
点击头像后再点击管理您的google账号,进入账号管理页面
图片.png
点击左侧菜单 -》安全性
下拉找到登录GOOGLE ,把两步验证和应用专用密码 都开启
图片.png
再找到设置按钮,点击查看所有设置
图片.png
把POP和 IMAP 开启
图片.png
thinkphp 对接代码:
public function sendCode()
{
$toemail = $this->email;//定义收件人的邮箱
$code = $this->code;
$mail = new PHPMailer();
$mail->isSMTP();// 使用SMTP服务
$mail->CharSet = "utf8";// 编码格式为utf8,不设置编码的话,中文会出现乱码
$mail->Host = "smtp.gmail.com";// 发送方的SMTP服务器地址
$mail->SMTPAuth = true;// 是否使用身份验证
$mail->Username = "xxxxxxxxxx@gmail.com";// 发送方的gmail邮箱用户名,就是你申请gmail的SMTP服务使用的gmail邮箱
$mail->Password = "xxxxxxxxxxxxxxx";// 发送方的邮箱密码,注意用gmail邮箱这里填写的是应用专用密码
$mail->SMTPSecure = "ssl";// 使用ssl协议方式
$mail->Port = 465;// 端口号
$mail->setFrom("xxxxxxxxxxxxxxxxxxx@gmail.com", "Verification code");// 设置发件人信息,如邮件格式说明中的发件人,这里会显示为Mailer(xxxx@gmail.com),Mailer是当做名字显示
$mail->addAddress($toemail, $toemail);// 设置收件人信息,如邮件格式说明中的收件人,这里会显示为Liang(yyyy@gmail.com)
$mail->addReplyTo("xxxxxxxxxxxxxxxxxxxxxx@gmail.com", "Reply");// 设置回复人信息,指的是收件人收到邮件后,如果要回复,回复邮件将发送到的邮箱地址
$mail->Subject = "Genesis Pay Verification code";// 邮件标题
$mail->Body = "Your verification code is: {$code}. Please keep it safe.";// 邮件正文
// dump($mail->send());die;
if (!$mail->send()) {// 发送邮件
// echo "Message could not be sent.";
// echo "Mailer Error: " . $mail->ErrorInfo;// 输出错误信息
return $mail->ErrorInfo;
} else {
return true;
}
}
至此就大功告成啦。(^-^)V
网友评论