2011-06-02 77 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);