php X-mailer:邮件发送了两次

时间:2019-03-27 14:51:49

标签: php email

我准备了发送确认电子邮件的表单和php代码。

这是我尝试的代码:

$mail_boundary = "=_NextPart_" . md5(uniqid(time()));
$sender = "postmaster@mysite.it";
'''
$text_msg = "messaggio in formato testo";
$html_msg = "<b>messaggio</b> in formato <p><a href='http://www.aruba.it'>html</a><br><img src=\"http://hosting.aruba.it/image_top/top_01.gif\" border=\"0\"></p>";

$msg = "This is a multi-part message in MIME format.\n\n";
$msg .= "--$mail_boundary\n";
$msg .= "Content-Type: text/plain; charset=UTF-8\n";
$msg .= "Content-Transfer-Encoding: 8bit\n\n";
$msg .= "\r\n";
'''
$msg .= "\n--$mail_boundary\n";
$msg .= "Content-Type: text/html; charset=UTF-8\n";
$msg .= "Content-Transfer-Encoding: 8bit\n\n";
$msg .= "<br><br>";
'''
$msg .= "\n--$mail_boundary--\n";
$headers = "From: $sender\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative;\n\tboundary=\"$mail_boundary\"\n";
$headers .= "X-Mailer: PHP " . phpversion();
if(mail('my-mail', "Summary from $name", $msg, $headers, "-f$sender")) {
    mail($email, "Summary", $msg, $headers, "-f$sender");
    header("Refresh: 5; url=landing_page");
    echo "Mail sent.<br>";
    echo "You're being redirected.";
}
else {
    echo "Error"
}

提交表单后,PHP代码将发送双倍的电子邮件。 我想给我发一封电子邮件,再给客户发一封电子邮件。 我该如何解决电子邮件翻倍的问题?

谢谢

1 个答案:

答案 0 :(得分:1)

mail命令被调用两次,因此电子邮件将发送两次。 您想要的是类似的东西

$m = mail($email, "Summary from $name", $msg, $headers, "-f$sender");

if($m) {
    header("Refresh: 5; url=landing_page");
    echo "Mail sent.<br>";
    echo "You're being redirected.";
}
else {
    echo "Error"
}