簡體   English   中英

為什么郵件在PHP中失敗?

[英]Why mail fails in php?

這是我的代碼:

<?php
//define the receiver of the email
$to = 'dannyfeher69@gmail.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. 
$message = "Hello World!\n\nThis is my mail.";
//define the headers we want passed. 
$header = "From: me@localhost.com";
//send the email
$mail_sent = @mail( $to, $subject, $message);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 

echo $mail_sent ? "Mail sent" : "Mail failed";
?>

- 它返回郵件失敗

請幫我

有幾個原因可能會失敗。 找到原因的主要障礙是在調用mail()函數之前使用錯誤控制操作符(@)。

其他可能的原因是缺少有效的From標頭。 雖然您在$ header變量中定義了一個,但是不將它傳遞給mail()函數。 From標頭是您發送電子郵件的域上的有效電子郵件地址也很重要。 如果不是,大多數托管公司現在將拒絕該電子郵件作為垃圾郵件。 您可能還需要為mail()提供第五個參數,該參數通常由-f后跟當前域上的有效電子郵件地址組成的字符串組成。

另一種可能性是你試圖從你自己的計算機發送它。 mail()函數不支持SMTP身份驗證,因此大多數郵件服務器將拒絕來自他們無法識別的來源的郵件。

只是為了解決所有問題,電子郵件中的換行符必須是回車后跟換行符的組合。 在PHP中,這是“\\ r \\ n”,而不是“\\ n \\ n”。

假設您使用遠程服務器發送郵件,代碼應如下所示:

<?php
//define the receiver of the email
$to = 'dannyfeher69@gmail.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. 
$message = "Hello World!\r\nThis is my mail.";
//define the headers we want passed. 
$header = "From: me@localhost.com"; // must be a genuine address
//send the email
$mail_sent = mail($to, $subject, $message, $header);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 

echo $mail_sent ? "Mail sent" : "Mail failed";
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM