PHP中呼叫mail()函式傳送郵件所需sendmail的基本配置和html格式的郵件資訊
阿新 • • 發佈:2018-12-31
- 首先從http://glob.com.au/sendmail上下載sendmail壓縮包;並將其解壓到D:盤中(一般最好不要解壓到C:盤,且目錄不要太長)。
設定一下PHP.ini檔案:
[mail function] ; For Win32 only. ; http://php.net/smtp SMTP = smtp.163.com ; http://php.net/smtp-port smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = [email protected] ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ; http://php.net/sendmail-path sendmail_path ="D:\wamp\sendmail\sendmail.exe -t -i" mail.add_x_header = On
- 分號‘;’表示註釋的意思,smtp.163.com是說明採用的是163郵箱的smtp(simple mail transfer protocol 簡單郵件傳輸協議的縮寫)代理伺服器(此服務預設未啟用,需要自己手動開啟),這是為郵件設定的專門通道傳送資料報,就像快遞一樣層層遞交,最後交到你的163賬號傳送出去。
- [email protected]是你站點的伺服器呼叫來發郵件的From(簡稱發件人),先登入進去,在設定中的賬戶選單下面啟動IMAP/SMTP服務,傳送完簡訊後,你將要設定第三方登入密碼(它不是你平時登入郵箱的密碼,為防止密碼洩露,在公共狀態下需要此第三方密碼,其實就是授權碼,它會在登入時被授予登入許可自動轉換為普通密碼),也就是sendmail.ini檔案裡的userpassword,username就是這個郵箱賬號,其smtp一樣是填smtp.163.com(如果是qq郵箱,兩邊一樣填smtp.qq.com,不過qq的第三方密碼不能自己設定)。
- mail.add_x_header = On 中 On 的意思是 開啟 mail()函式中的一個引數$header,裡面包括了相關重要資訊,像說明此郵件從哪裡傳送到哪去、字元格式是什麼、包含什麼格式的文字等諸如此類資訊。
開啟IMAP/SMTP服務(這是qq的,163的也差不多):
- 埠號:25是預設的,一般不用改。
設定sendmai.ini檔案:
[sendmail]
smtp_server=smtp.163.com
; smtp port (normally 25)
smtp_port=25
smtp_ssl=auto
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=zdfdsfsdfjdiffidj
- error.log跟debug.log在同級目錄下,若出現錯誤它就會自動建立txt檔案告知最近一次error_message。
- 好,配置好後,接下來就可以使用mail()函式發郵件了。
用mail()傳送郵件:
$to = "[email protected]";
$subject = "網站反饋";
$msg = "<html><head><meta charset='utf-8'></head><body style='background-color:gray;'><h4>來自 $name 的一封郵件:</h4><div style='padding-left:20px;padding-top:5px;font-size:0.7em;'><strong>性別:</strong>$sex<br><strong>年齡範圍:</strong>$age<br><strong>愛好:</strong>$hobby<br><strong>郵箱地址:</strong>.$dzyj<br><strong>手機號碼:</strong>$telephone<br><strong>所屬公司:</strong>.$company<br><strong>內容:</strong>$contents<br></div></body></html>";
$headers = "X-Mailer:PHP";
$headers .= "MIME-Version: 1.0";
$headers .= "PHP-Version:phpversion()\r\n";
$headers .= "Content-type:text/html;charset=utf-8\r\n";
$headers .= "From:[email protected]\r\n";
$headers .= "Reply-To:[email protected]\r\n";
if([email protected]($to,$subject,$msg,$headers)){
echo '<img class="warning" src="images/error.png">';
echo '<div id="sorry" style="font-size:1.5em;margin:30px 30px;"><p>遇到未知錯誤!無法提交相關資訊</p><br/>'.'<a href="contact_form.php" style="font-size:1.0em">返回前一頁</a></div>';
}
else{
echo '<img class="warning" src="images/success.png">';
echo '<div id="sorry" style="font-size:1.9em;margin:30px 30px;"><p>提交成功!</p><br/>'.'<a href="ddstore.php" style="font-size:1.0em">返回主頁</a></div>';
}
- $to發給誰;$subject郵件標題(其中必須為連續的字元且不包含特殊字元,不然會報錯);$msg主體資訊,我寫成了html格式,效果比較好;而$header就是為了向伺服器說明前面所有資訊的詳細細節。
傳送成功後(手機客戶端可以看到我的一些html的修飾,這是網頁版的,沒有什麼效果,單純的txt文字):
- 本人為新手營菜鳥級,如有不當還望指點,喜歡的可以加關注瞭解更多詳情!