使用php+gmail 发送邮件
1 <?php 2 namespace app\index\controller; 3 4 use think\Controller; 5 use PHPMailer\PHPMailer; 6 7 class Test extends Controller 8 { 9 10 public function index2(){ 11 12 $mail = new PHPMailer(); 13 14 15 $body = "自定义您的收件箱 16 17 如果您想让某封邮件位于其他类别中,则可以将该邮件移至您所需的类别中。在移动设备上,您甚至可以选择允许哪些类别创建通知。更多自定义提示 18 19 要了解Gmail收件箱的详情,请查看帮助中心或观看此视频"; 20 21 $mail->IsSMTP(); // telling the class to use SMTP 22 23 $mail->Host = "mail.gmail.com"; // SMTP server 24 25 $mail->SMTPDebug = 2; // enables SMTP debug information (for testing) 26 27 $mail->SMTPAuth = true; // enable SMTP authentication 28 29 $mail->SMTPSecure = "ssl"; // sets the prefix to the servier 30 31 $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server 32 33 $mail->Port = 465; // set the SMTP port for the GMAIL server 34 35 $mail->Username = "111111@gmail.com"; // GMAIL username 36 37 $mail->Password = "password"; // GMAIL password 38 39 $mail->SetFrom('wangdana', 'First Last'); 40 41 $mail->AddReplyTo("11111@gmail.com","First Last"); 42 43 $mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic"; 44 45 $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 46 47 $mail->MsgHTML($body); 48 49 $mail->CharSet = "utf-8"; // 这里指定字符集! 50 51 $address = "12341234@163.com"; 52 53 $mail->AddAddress($address, "John Doe"); 54 55 56 if(!$mail->Send()) { 57 58 echo "Mailer Error: " . $mail->ErrorInfo; 59 60 } else { 61 62 echo "Message sent!"; 63 64 } 65 66 } 67 68 69 }
值得注意的是,
首先你的服务器要能链接谷歌,有外网,
第二,谷歌邮箱要开启imap访问,
第三,谷歌账号要开启低安全性应用登陆认证。开启地址: https://myaccount.google.com/lesssecureapps