邮件投递失败:正在将邮件退回发件人

4
我希望在我的邮件脚本中添加一个失败消息重定向,以便如果用户在电子邮件字段中输入错误地址,则会将其返回给我,而不是发送到我的托管公司的收件箱。我已经添加了return-path但不起作用,还有什么可以做让这段代码工作吗?
以下是代码:
<?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个回答

3
邮件返回路径是由MTA根据信封发件人设置的,你不能自己在头部设置。你可以尝试使用邮件函数的$additional_parameters参数来设置信封发件人。请参考http://www.php.net/manual/en/function.mail.php上的示例#3。
在您的情况下,应该像这样设置:
mail($recipient, $subject, $msg2, $headers, "-f $to");

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


我如何切换到SMTP? - Beto
请使用任何已有的 PHP SMTP 库,比如 PHPMailer。 - Gryphius
愚蠢的问题,但是信封或邮件信封是什么? - Beto
如何设置信封,这是我的做法,应该可以工作:$headers = "From: ". $to . "\r\n"; $headers .= "Reply-To: ". $to . "\r\n"; $headers .= "Return-Path: ". $to . "\r\n"; - Beto
谢谢Gryphius,你的解决方案很有帮助。我确实需要修改第五个参数才能使它正常工作,像这样:mail($recipient, $subject, $msg2, $headers, "-f".$to); - Beto
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接