用了很長時間去一步一步摸索,終于先在163 網易郵箱上測試成功了,下面就把這個過程分享給大家。
在進入正題這前先看下網易(163)郵箱的服務器地址和端口號:
一、前期準備
使用網易郵箱,當然要注冊個賬號,這個就不用我多說了,自己去注冊。。。
注冊完之后,就要去開啟 pop3/smtp/imap服務。 在開啟服務時,需要客戶端授權密碼(這里需要手機驗證,md拐彎抹角的要手機號碼)。
步驟一:
步驟二:
確定后會彈出下面這樣的對話框,也會把這個授權密碼發送你的短信里,記住這個授權密碼一定要記住
服務開啟后,如果沒有設置【姓名】,在寫郵件發送時會提示設置【姓名】后才能發送郵件,當然也可以提前設置好。。
二、代碼部分
phpmailer下載 ( 下載后把phpmailer放在vendor目錄下,另外文件中有很多不必要的東西,自己看著處理吧)
細心的同學在 class.phpmailer.php class.pop3.php class.smtp.php 這三個文件里看下默認的端口號,其smtp的默認端口號是25 與 163下的smtp發件服務器的非ssl協議端口號一樣。
html布局:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!doctype html> <html lang= "en" > <head> <meta charset= "utf-8" > <title>document</title> </head> <body> <form action= "__url__/add" method= "post" enctype= "multipart/form-data" > 收件人郵箱:<input type= "text" name= "mail" /> 標題:<input type= "text" name= "title" /> 內容<input type= "text" name= "content" /> <input class = "button" type= "submit" value= "發送" /> </form> </body> </html> |
config.php 配置:
1
2
3
4
5
6
7
8
|
'mail_host' => 'smtp.163.com' , //smtp服務器的名稱 'mail_smtpauth' =>true, //啟用smtp認證 'mail_password' => 'olagbqsyeyhilcwu' , //163郵箱發件人授權密碼 'mail_fromname' => '天空還下著雪' , //發件人姓名 'mail_charset' => 'utf-8' , //設置郵件編碼 'mail_ishtml' =>true, // 是否html格式郵件 |
function.php公共函數:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/* * 發送郵件 * @param $to string * @param $title string * @param $content string * @return bool * */ function sendmail( $to , $title , $content ) { vendor( 'phpmailer.phpmailerautoload' ); $mail = new phpmailer(); //實例化 $mail ->issmtp(); // 啟用smtp $mail ->host=c( 'mail_host' ); //smtp服務器的名稱(這里以qq郵箱為例) $mail ->smtpauth = c( 'mail_smtpauth' ); //啟用smtp認證 $mail ->username = c( 'mail_username' ); //發件人郵箱名 $mail ->password = c( 'mail_password' ) ; //163郵箱發件人授權密碼 $mail ->from = c( 'mail_from' ); //發件人地址(也就是你的郵箱地址) $mail ->fromname = c( 'mail_fromname' ); //發件人姓名 $mail ->addaddress( $to , "尊敬的客戶" ); $mail ->wordwrap = 50; //設置每行字符長度 $mail ->ishtml(c( 'mail_ishtml' )); // 是否html格式郵件 $mail ->charset=c( 'mail_charset' ); //設置郵件編碼 $mail ->subject = $title ; //郵件主題 $mail ->body = $content ; //郵件內容 $mail ->altbody = "這是一個純文本的身體在非營利的html電子郵件客戶端" ; //郵件正文不支持html的備用顯示 return ( $mail ->send()); } |
add方法調用:
1
2
3
4
5
6
7
|
public function add() { if (sendmail( $_post [ 'mail' ], $_post [ 'title' ], $_post [ 'content' ])) { $this ->success( '發送成功!' ); } else { $this ->error( '發送失敗' ); } } |
做完以上工作后,接下來訪問地址,通過表單向163(網易)郵箱發送郵件(如:發送給[email protected]),也可以發送給自己,發送后,就會看到發送成功。下面你可以登錄郵箱查看郵件。
qq郵箱收發郵件
qq郵箱收發件服務器地址和端口
準備:
1、設置郵箱獨立密碼
2、開啟pop3/smtp服務
配置:
1
2
3
4
5
6
7
8
|
'mail_host' => 'smtp.qq.com' , //smtp服務器的名稱 'mail_smtpauth' =>true, //啟用smtp認證 'mail_password' => 's****1241' , //qq郵箱發件人獨立密碼 'mail_fromname' => '戀獄' , //發件人姓名(qq郵箱昵稱) 'mail_charset' => 'utf-8' , //設置郵件編碼 'mail_ishtml' =>true, // 是否html格式郵件 |
其他無需改變,完成后不僅可以給qq郵箱用戶發送郵件,也可以給163郵箱用戶發送郵件。
以上就是thinkphp實現163等郵箱收發郵件的方法,希望對大家的學習有所幫助。