![](/img/trans.png)
[英]Can't send mail with PHP mail() in Ubuntu Server 16.04 (postfix)
[英]Mail send successfully, but not delivered using PHP Mail function
我正在使用php郵件功能發送郵件,當發送電子郵件時,它說消息已成功發送,但郵件未送達。 我已經用gmail pop3 / SMTP / Imap設置配置了服務器。 我正在使用Ubuntu,這是我的代碼:
<?php
$name = "ali";
$email ="hello";
$message = "adasdasfasf";
$from = 'From: from@gmail.com';
$to = 'to@gmail.com';
$subject = 'Customer Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers = 'From: From: from@gmail.com' . "\r\n" .
'Reply-To: From: from@gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
?>
您的PHP代碼不正確:
$headers .= "MIME-Version: 1.0\r\n";
在這里,首先將變量$ headers附加到之前未設置的位置(因此會出現錯誤)。
$headers .= "Content-type: text/html\r\n";
現在,您擴展它。
$headers = 'From: From: from@gmail.com' . "\r\n" .
現在,您可以通過覆蓋其內容,刪除先前設置的標題來再次丟棄所有內容。
因此它沒有發送,因為您的PHP代碼產生不完整的標頭。
對於StackOverflow來說更多,但是代碼可能應該是:
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: from@gmail.com' . "\r\n" .
第一次使用$ headers變量是:
$headers .= "MIME-Version: 1.0\r\n";
使用。=時,它希望將其添加到該變量的較早使用中,而您沒有該變量。 將該行更改為:
$headers = "MIME-Version: 1.0\r\n";
進行初始化,然后共享結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.