2011-06-02 144 views
1

我想設置一個PHP腳本,將發送電子郵件給選定的用戶,我看了看互聯網,發現如何,但不能運行腳本COS我得到以下錯誤:SMTP錯誤爲PHP電子郵件

Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in E:\Program Files\xampp\phpMyAdmin\emailer.php on line 10

下面是腳本的代碼,我設計下面的例子:

<?php 
    $recipient = "<[email protected]>"; 
    $subject = "Flight Status"; 
    $body = "Flight has just landed."; 

    $host = "ssl://smtp.gmail.com"; 
    $port = "465"; 


    if(mail($recipient, $subject, $body)) 
    { 
     echo("<p>Message successfully sent</p>"); 
    } 
    else 
    { 
     echo("<p>Message Delivery failed </p>"); 
    } 
?> 

在此先感謝。

+0

你需要傳遞額外的頭文件,因爲錯誤是說你錯過了從部分檢查PHP郵件功能doc他們有頭文件的例子 – 2011-06-02 15:47:47

回答

1

由於錯誤建議你一定要「從」字段(誰發送郵件)在php.ini中添加默認:

SMTP = localhost 


sendmail_from = [email protected] 

然後重啓apache

否則,您可以dinamically對其進行設置如在PHP手冊中所述的第四個參數(查找http://php.net/manual/en/function.mail.php

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.

實施例:

<?php 
$to  = '[email protected]'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

mail($to, $subject, $message, $headers);