[英]how to send an HTML content via php mail()
我正在嘗試使用此代碼發送HTML電子郵件,但我得到的只是來自mail()函數的FALSE。
error_log為空。
有人可以告訴我為什么mail()不起作用嗎?
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>SDFSDF</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>VXCVSDF</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
$to = 'my_mail@gmail.com';
$subject = 'Website Change Reqest';
$headers = "From: USER NAME"."\r\n";
$headers .= "Reply-To: USER EMAIL"."\r\n";
$headers .= "MIME-Version: 1.0"."\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1"."\r\n";
if (mail($to, $subject, $message, $headers)) {
echo 'Your message has been sent.';
} else {
echo 'There was a problem sending the email.';
}
調試PHP mail()函數非常困難。
檢查完腳本后,我可以確認您的代碼運行正常。 這與您的服務器或/和PHP配置有關。
從這個小片段開始,看看發生了什么:
error_reporting(E_ALL);
ini_set('display_errors', -1);
echo '<br>I am : ' . `whoami`.'<br>';
$result = mail('myaddress@mydomain.com','This is the test','This is a test.');
echo '<hr>Result was: ' . ( $result === FALSE ? 'FALSE' : 'TRUE') . ' ('. $result. ')';
echo '<hr>';
echo phpinfo();
輸出后,檢查您的sendmail_path
,大多數情況下sendmail_path
使用sendmail
MTA:
/usr/sbin/sendmail -t -i
編輯您的php.ini
文件,進行以下設置,不要忘記重啟httpd服務器:
sendmail_path = /usr/sbin/sendmail -t -i
檢查/var/log/maillog
日志文件,它確實可以幫助您解決問題。
如果仍然有問題,請仔細閱讀PHPMailer , SwiftMailer , PEAR的Mail或Zend Framework的Zend_Mail,這是一個出色,全面的現代PHP郵件庫。 畢竟,調試問題很容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.