使用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 }

值得注意的是,php

首先你的服務器要能連接谷歌,有外網,安全

第二,谷歌郵箱要開啓imap訪問,服務器

第三,谷歌帳號要開啓低安全性應用登錄認證。開啓地址:  https://myaccount.google.com/lesssecureappsapp

相關文章
相關標籤/搜索