google邮箱 发送
前期准备
一、申请google 邮箱账号 https://mail.google.com/
二、配置google 邮箱开启 pop/imap
在这里插入图片描述
三、创建应用密码,发送验证码时填写此密码
Create & use app passwords
Important: To create an app password, you need 2-Step Verification on your Google Account.
If you use 2-Step-Verification and get a “password incorrect” error when you sign in, you can try to use an app password.
Go to your Google Account.
Select Security.
Under “How you sign in to Google,” select 2-Step Verification.
At the bottom of the page, select App passwords.
Enter a name that helps you remember where you’ll use the app password.
Select Generate.
To enter the app password, follow the instructions on your screen. The app password is the 16-character code that generates on your device.
Select Done.
If you’ve set up 2-Step Verification but can’t find the option to add an app password, it might be because:
Your Google Account has 2-Step Verification set up only for security keys.
You’re logged into a work, school, or another organization account.
Your Google Account has Advanced Protection.
Tip: Usually, you’ll need to enter an app password once per app or device.
四、代码编辑
引用 PHPMailer
//端口配置
$Host = "smtp.gmail.com";
$SMTPSecure = "ssl";
$Port = 465;
$mail = new \PHPMailer(); //实例化
$mail->IsSMTP(); // 启用SMTP
$mail->SMTPDebug = 1;
$mail->Host = $Host; //SMTP服务器 以163邮箱为例子
$mail->SMTPSecure = $SMTPSecure;
$mail->Port = $Port; //邮件发送端口
$mail->SMTPAuth = true; //启用SMTP认证
$mail->CharSet = "UTF-8"; //字符集
$mail->Encoding = "base64"; //编码方式
$mail->Username = $this->web_email; //你的邮箱
$mail->Password = $this->web_email_pass; //你的密码
$mail->Subject = $title; //邮件标题
$mail->From = $this->web_email; //发件人地址(也就是你的邮箱)
$mail->FromName = ""; //发件人姓名
$mail->AddAddress($user_email, "");//添加收件人(地址,昵称)
//$mail->AddAttachment($path,'投稿附件.'.$filetype);
//$mail->AddAttachment('投稿附件.docx',$name); // 添加附件,并指定名称
$mail->IsHTML(true); //支持html格式内容
//$mail->AddEmbeddedImage("logo.jpg", "my-attach", "logo.jpg"); //设置邮件中的图片
$text = $content;
$mail->Body = $text;
$result = $mail->Send();
特别注意,服务器使用国外服务器