【问题标题】:Mail delivery failed: returning message to sender邮件传递失败:将邮件返回给发件人
【发布时间】:2011-10-18 16:44:15
【问题描述】:

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

代码如下:

<?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;
    }
}
?>

【问题讨论】:

    标签: php forms email


    【解决方案1】:

    返回路径由 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 二进制文件。

    【讨论】:

    • 如何切换到 SMTP?
    • 使用任何现有的 php smtp 库,如 phpmailer
    • 在这种情况下,SMTP 方法可能是您最好的选择。看看phpmailer.worxware.com
    • 愚蠢的问题,信封或邮件信封是什么?
    • 信封发件人/收件人地址在 smtp 协议中指定(“MAIL FROM:”/“RCPT TO:”)。信封不必与标题中的发件人/收件人匹配。标头中的发件人/收件人基本上仅用于显示目的,例如。目标邮件程序显示为发件人/收件人的内容。但是对于邮件路由路径/退回地址,信封很重要。
    猜你喜欢
    • 2021-02-02
    • 2013-06-19
    • 2021-07-10
    • 2014-11-18
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多