邮件传递失败:向发件人返回邮件

时间:2011-08-02 23:30:24

标签: php forms email

我想添加一条失败的邮件重定向到我的邮件脚本,这样如果用户在电子邮件字段中输入了错误的地址,它会返回给我,而不是我的托管公司的收件箱,我该怎么办那?我已经添加了返回路径但是不起作用,我还能做些什么来使这段代码起作用。

以下是代码:

<?php if(isset($_POST['submit'])) { 
if(trim($_POST['first-name']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['first-name']);
}

//Check to make sure that the last name field is not empty
if(trim($_POST['last-name']) == '') {
    $hasError = true;
} else {
    $lname = trim($_POST['last-name']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", trim($_POST['email']))) {
    $hasError = true;
    } else {
    $email = trim($_POST['email']);
}

//Check to make sure that the phone field is not empty
if(trim($_POST['tel']) == '') {
    $hasError = true;
} else {
    $phone = trim($_POST['tel']);
}

//Check to make sure that the phone field is not empty
if(trim($_POST['company']) == '') {
    $hasError = true;
} else {
    $company = trim($_POST['company']);
}



foreach (array($_POST['q1'])  as  $value)  {
 $q1 = $value[0];
 $q2 = $value[1];
 $q3 = $value[2];
}



//If there is no error, send the email
if(!isset($hasError)) {

    $to = 'me@host.com';
    $recipient = $email;


    $subject = 'Subject';

    $headers = "From: Me\n" . $to . "\r\n";
    $headers .= "Reply-To: ". $to . "\r\n";
    $headers .= "Return-Path: ". $to . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $msg1 = "First Message";


    $msg2 = "Second Message";

    //Send Email
    mail($recipient, $subject, $msg2, $headers);
    mail($to, $subject, $msg1, $headers);

    $emailSent = true;
    }
}
?>

1 个答案:

答案 0 :(得分:2)

返回路径由mta根据信封发件人设置,您不能自己在标题中设置。您可以尝试使用mail函数的$ additional_parameters参数设置信封发件人。请参阅http://www.php.net/manual/en/function.mail.php

上的示例#3

在你的情况下,这就像是

mail($recipient, $subject, $msg2, $headers, "-f $to");

在某些系统上,使用-f覆盖信封发件人是受限制的。在这种情况下,您可能必须切换到通过SMTP提交邮件,而不是通过mail()调用sendmail二进制文件。