環境:WampServer2.5(Windows 10,Apache 2.4.9,MySQL 5.6.17,PHP 5.5.12)
① 在 Windows 下使用 PHP 內置的 mail() 函數發送郵件,須要先安裝 sendmail(下載地址:http://glob.com.au/sendmail/)php
把下載下來的 sendmail.zip 解壓到自定義的目錄(我這裏是 D:\wamp\bin)html
② 配置 php.ini 文件(經過 phpinfo 肯定 ph.ini 文件真實路徑)linux
郵件服務器以騰訊郵箱爲例,php.ini 文件主要配置git
SMTP = smtp.qq.comgithub
smtp_port = 25(郵件服務端口),vim
sendmail_path = "D:\wamp\bin\sendmail\sendmail.exe -t"windows
③ 配置 sndmail.ini服務器
須要配置:函數
smtp_server=smtp.qq.com smtp_port=25 ,開啓 log 方便排錯,生成的log文件在sendmail根目錄 error_logfile=error.log debug_logfile=debug.log auth_username=472323087@qq.com auth_password=你的受權碼 ,force_sender 要和auth_username一致 force_sender=472323087@qq.com
說明:測試
配置項中的 auth_password 不是郵箱的密碼, 而是騰訊郵箱的第三方客戶端受權碼,獲取受權碼的方式是:
登錄 mail.qq.com(472323087@qq.com),選擇「設置」 -- 「POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務」 -- 「生成受權碼」
點擊「生成受權碼」,須要發送一條驗證信息,驗證經過以後獲得受權碼
④ 測試發送郵件:
<?php // 使用 PHP 內置的 mail() 函數 $to = '472323087@qq.com'; $subject = 'Hello World!'; $body = 'Welcome to China!'; mail($to, $subject, $body);
收到郵件:
環境:LNMP(CentOS 6.6 ,Nginx 1.8.0,MySQL 5.6.23,PHP 5.6.9)
① 安裝 sendmail
# yum install sendmail
② 啓動 sendmail
# /etc/rc.d/init.d/sendmail start
③ 此時能夠直接經過 mail 命令來給指定郵箱發送郵件:
[root@localhost ~]# echo 'this is a mail test'|mail -s text dee1566@126.com
這裏先用 126 郵箱舉例,騰訊郵箱因爲騰訊郵件服務器的限制,不作設置很容形成拒收,後面再說。
打開郵件:
④ 使用 PHP 的 mail() 函數發送郵件
須要修改 php.ini
smtp_port = 25 sendmail_path = /usr/sbin/sendmail -t
SMTP 不用設置
mail.php
<?php header('Content-type:text/html;charset=utf-8'); // 使用 PHP 內置的 mail() 函數 $to = 'dee1566@126.com'; $subject = 'Hello World!'; $body = 'Welcome to China!'; if(mail($to, $subject, $body)) { echo '發送成功'; } else { echo '發送失敗'; }
收到郵件:
打開郵件:
說明:
mail("接受方email", "郵件主題", "正文內容", headers, "from:發送方email");
要修改發件人,能夠添加第四個參數
<?php header('Content-type:text/html;charset=utf-8'); // 使用 PHP 內置的 mail() 函數 $to = (isset($_GET['type']) && $_GET['type'] == 'qq') ? '472323087@qq.com' : 'dee1566@126.com'; $subject = 'Hello World!'; $body = 'Welcome to China!'; $headers = array(); $headers[] = "MIME-Version: 1.0"; $headers[] = "Content-type: text/plain; charset=iso-8859-1"; $headers[] = "From: dee <472323087@qq.com>"; $from = '472323087@qq.com'; if(mail($to, $subject, $body, implode("\r\n", $headers), $from)) { echo '發送成功'; } else { echo '發送失敗'; }
此時收到的郵件:
打開郵件:
⑤ 若是接收方 email 是騰訊郵箱的話,很容易顯示發送成功可是實際上根本就沒有發送成功,經過查看日誌
[root@localhost sbin]# tail -f /var/spool/mail/root
可能會出現
<<< 550 Mail content denied. http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=20022&&no=1000726 554 5.0.0 Service unavailable
550 Mail content denied 出錯緣由:該郵件內容涉嫌大量羣發,而且被多數用戶投訴爲垃圾郵件
編輯 /etc/mail.rc
[root@localhost ~]# vim /etc/mail.rc
添加:
set from=472323087\@qq.com smtp=smtp.qq.com //郵件來自 set smtp-auth-user=472323087\@qq.com smtp-auth-password=你的受權碼 smtp-auth=login //登陸qq SMTP服務器的用戶名和密碼
此時直接使用 mail 命令發送郵件:
[root@localhost ~]# echo 'this is a mail test'|mail -s title 472323087@qq.com
但仍是不能使用 PHP 的 mail 函數給騰訊郵箱發送郵件,查了不少資料,仍是沒能解決。
總結:
在 Windows 下使用 sendmail 結合 mail() 函數能很容易地給騰訊郵箱發郵件,在 Linux 下騰訊郵箱幾乎一概拒收,連垃圾箱都進不了,其餘的好比 126 郵箱就沒有問題,可能仍是和主機設置有關。
最終代碼:
<?php header('Content-type:text/html;charset=utf-8'); // 使用 PHP 內置的 mail() 函數 $to = 'dee1566@126.com'; $subject = 'Hello World!'; $body = 'Welcome to China!'; $headers = array(); $headers[] = "MIME-Version: 1.0"; $headers[] = "Content-type: text/plain; charset=iso-8859-1"; $headers[] = "From: dee <472323087@qq.com>"; //決定郵件的發件人顯示 $from = '472323087@qq.com'; if(mail($to, $subject, $body, implode("\r\n", $headers), $from)) { echo '發送成功'; } else { echo '發送失敗'; }
文檔地址:http://ezcomponents.org/docs/tutorials/Mail
下載地址:http://ezcomponents.org/download
下載後解壓壓縮包
Zetacomponent ezcMailComposer 類能夠與 SMTP 服務器直接通訊:
<?php header('Content-type:text/html;charset=utf-8'); // 使用 Zeccomponent 的 ezcMailComposer 類 // http://ezcomponents.org/docs/tutorials/Mail require_once 'ezcomponents/Mail/docs/tutorial/tutorial_autoload.php'; $message = new ezcMailComposer(); $message->from = new ezcMailAddress('47232087', 'dee'); //發送郵箱是qq郵箱,例如472323087@qq.com,郵件很容易被拒,sendmail根目錄下error.log中錯誤記錄是:Error: content rejected.http://mail.qq.com/zh_CN/help/content/rejectedmail.html<EOL> $message->addTo(new ezcMailAddress('472323087@qq.com', 'emperor')); $message->subject = 'php sendmail'; $body = 'this is a test mail'; $message->plainText = $body; $message->build(); $host = 'smtp.qq.com'; $username = '472323087@qq.com'; $password = 'niwogqkejpnzbibh'; $port = '25'; $smtpOptions = new ezcMailSmtpTransportOptions(); $smtpOptions->preferredAuthMethod = ezcMailSmtpTransport::AUTH_LOGIN; $sender = new ezcMailMtaTransport($host, $username, $password, $port, $smtpOptions); try { $sender->send($message); echo '發生成功'; } catch(ezcMailTransportException $e) { echo $e->getMessage(); }
收到郵件:
打開郵件:
一樣在 Linux 下一樣會遇到騰訊郵箱直接拒收的問題。
PHPMailer 版本 5.2.13
下載地址:https://github.com/Synchro/PHPMailer
Windows 下調試代碼:
<?php header("content-type:text/html;charset=utf-8"); require 'PHPMailer/class.smtp.php'; require 'PHPMailer/class.phpmailer.php'; try { $mail = new PHPMailer(true); $mail->IsSMTP(); $mail->CharSet='UTF-8'; //設置郵件的字符編碼,這很重要,否則中文亂碼 $mail->SMTPAuth = true; //開啓認證 $mail->Port = 25; $mail->Host = "smtp.qq.cn"; $mail->Username = "472323087@qq.com"; $mail->Password = "你的受權碼"; $mail->IsSendmail(); //windows下開啓;linux下若是沒有sendmail組件就註釋掉,不然出現「Could not execute: /usr/sbin/sendmail」的錯誤提示 $mail->AddReplyTo("472323087@qq.com","dee");//回覆地址 $mail->From = "472323087@qq.com"; $mail->FromName = "472323087@qq.com"; $to = "472323087@qq.com"; $mail->AddAddress($to); $mail->Subject = "phpmailer測試標題"; $mail->Body = "<h1>phpmail演示</h1>這是emperor對phpmailer的測試內容"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //當郵件不支持html時備用顯示,能夠省略 $mail->WordWrap = 80; // 設置每行字符串的 長度 //$mail->AddAttachment("d:/test.jpg"); //能夠添加附件 $mail->IsHTML(true); $mail->Send(); echo '郵件已發送'; } catch (phpmailerException $e) { echo "郵件發送失敗:".$e->errorMessage(); }
Linux 下修改 php.ini,註釋
sendmail_path = /usr/sbin/sendmail -t -i
把程序中的
$mail->IsSendmail();
也註釋,能夠完成包括對騰訊郵箱的郵件任務。
注意,郵件服務器儘可能不要選騰訊郵箱。
參考: