PHP邮件脚本不起作用

时间:2014-01-18 20:04:54

标签: php email configuration sendmail

我下载了这个php邮件脚本:

<?php
/*
From http://www.html-form-guide.com 
This is the simplest emailer one can have in PHP.
If this does not work, then the PHP email configuration is bad!
*/
$msg="";
if(isset($_POST['submit']))
{
    /* ****Important!****
    replace name@your-web-site.com below 
    with an email address that belongs to 
    the website where the script is uploaded.
    For example, if you are uploading this script to
    www.my-web-site.com, then an email like
    form@my-web-site.com is good.
    */

    $from_add = "myemail@myemail.com"; 

    $to_add = "myemail@myemail.com"; //<-- put your yahoo/gmail email address here

    $subject = "Test Subject";
    $message = "Test Message";

    $headers = "From: $from_add \r\n";
    $headers .= "Reply-To: $from_add \r\n";
    $headers .= "Return-Path: $from_add\r\n";
    $headers .= "X-Mailer: PHP \r\n";


    if(mail($to_add,$subject,$message,$headers)) 
    {
        $msg = "Mail sent OK";
    } 
    else 
    {
       $msg = "Error sending email!";
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Test form to email</title>
</head>

<body>
<?php echo $msg ?>
<p>
<form action='<?php echo htmlentities($_SERVER['PHP_SELF']); ?>' method='post'>
<input type='submit' name='submit' value='Submit'>
</form>
</p>


</body>
</html>

我只是在代码中使用myemail@myemail.com代替我的电子邮件。

我正在运行OSX 10.7.5,使用系统首选项中的内置Web服务器。我从http://xxx.home/~xxx/samplemail.php加载了页面samplemail.php(xxx正在替换我的用户名)。

我点击提交 - 它会显示“邮件已发送正常”。但是,电子邮件永远不会被发送。

我假设这是一个配置问题。

我在终端中完成了php --ini,它告诉我Configuration File (php.ini) Path: /opt/local/etc/php54

我编辑了php.ini文件(位于/opt/local/etc/php54),以便知道sendmail的位置:

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = /usr/sbin/sendmail -t -i

我已确认sendmail位于/usr/sbin/sendmail

我还需要配置什么吗?

2 个答案:

答案 0 :(得分:0)

我遇到了与内置网络服务器相同的问题。当sendmail_path存在时,它会抛出错误。将其上传到实时环境并重试。我仍然不确定它是否是另一端的邮件服务器过滤我[我知道会有的],或者如果是另一个错误,但是将脚本放在实际的网络服务器上为我解决了这个问题。

但剧本很好。

答案 1 :(得分:0)

尝试使用swiftmailerphpmailer等第三方库发送电子邮件,这样会更稳定。