2009-07-09 38 views
4
$to = "[email protected]"; 
$subject = "Hi!"; 
$body = "Hi,\n\nHow are you?"; 
if (mail($to, $subject, $body)) { 
    echo("pMessage successfully sent!/p"); 
} else { 
    echo("pMessage delivery failed.../p"); 
} 

郵件代碼寫了基本的PHP sendmail的代碼,但它給了我下面的錯誤:PHP發送不工作

Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:\xampp\htdocs\mail.php on line 5 Message delivery failed...

我改變了``php.ini文件,並把[email protected]但仍是問題仍然存在。 第一次寫郵件腳本。

我做錯了什麼?有一個更好的方法嗎?

+2

我認爲這是值得一提的是,把你的真實電子郵件在互聯網上明文地址只是要求通過spambots收穫。我希望你稍微混淆一下,因爲我不知道你的真實電子郵件地址,所以我只是沒有看到它。 :D – 2009-07-09 17:21:02

回答

2

additional_headers (optional)

String to be inserted at the end of the email header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).

Note: When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini. Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.

我希望有幫助。

1

首先,請檢查您編輯的正確php.ini - 將phpinfo();添加到您的腳本輸出診斷信息中,包括php.ini的位置。您也應該能夠在這裏看到配置的「sendmail_from」值。

做不到這一點,提供了從頭部,通過usoban

$hdrs="From: [email protected]"; 
mail($to, $subject, $body, $hdrs); 
+0

我編輯了正確的php.ini文件,但仍然有錯誤 – anand 2009-07-09 15:31:55

1

指示如果編輯了正確的php.ini,它並不能反映你的變化,你可能需要重新啓動你的web服務,儘可能多環境只會在服務器啓動時加載php.ini。

0

而不是在php.ini中設置表單地址,而是將其發送到像usoban這樣的標題中。

當您在同一個設置中託管另一個站點時,這可以節省您的麻煩,並且下次忘記設置標題。

0

PHP mail()函數會產生很多問題。當您最終在服務器上運行它時,您會注意到您的電子郵件最終會出現在用戶的垃圾郵件文件夾中。

我會建議使用的PHP SMTP庫,而不是一個發送郵件:

1

我遇到了同樣的問題,結果是因爲我的服務器使用[email protected]作爲發件人電子郵件地址,即使我在標題中指定了自己的郵件地址。

我發現其他地方的答案是一個第五個參數添加到郵件調用強制系統使用,從我指定的郵箱地址:

$from = '[email protected]' 
$xheaders = "From: " . $from . " <" . $from . ">\n"; 
$i = mail("$useremail","$subject","$content",$xheaders,'-f [email protected]');