PHP 具有多个收件人和不同内容的邮件表单:有时收不到

PHP mail form with multiple recipients & different contents: Sometimes not received

我想向两个地址发送在线电子邮件表格。 每个收件人的邮件内容应该不同。 一封邮件用于进一步处理(有密码),另一封邮件作为个人副本(无密码)。

问题:

我创建的 Web 表单基本上可以工作,但是 一些用户报告说他们没有得到个人副本。 我还不能重现这个错误.(请参阅下面的更新)我现在担心有时它也不会向其他地址发送电子邮件。 我认为这与邮件处理逻辑有关(process.php) ...

如何避免这种奇怪的行为?我犯了编码错误吗?是否有任何修复需要完成?

非常感谢您的帮助!

示例代码如下:

<?php
$to =  $_POST['email'];
$to2 =  'mail@example.com';
$from = $_POST['email'];
$subject = 'Webform for' . $_POST['firstname'] . ' ' . $_POST['lastname'];
$email = $_POST['email'];
$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname'];
$password = $_POST['password'];
$comment = $_POST['comment'];

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=utf-8";
$headers[] = "From: {$email}";
$headers[] = "X-Mailer: PHP/".phpversion();

$message1 = "
Hello $firstname $lastname, \n
here is your personal copy of the web form: \n
Name:       $firstname $lastname
Comment:    $comment \n
Bye, Admin";

$message2 = "
Name:       $firstname $lastname
Password:   $password
Comment:    $comment";

$success = mail($to, $subject, $message1,implode("\r\n",$headers), '-fmail@example.com');       // Personal copy
$success = mail($to2, $subject, $message2,implode("\r\n",$headers), '-fmail@example.com');  // Further processing

if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=form.php#success\">";
}
?>

更新 [11-24-2015]:我重现了一次错误(很少发生)。 丢失的电子邮件不会被过滤为垃圾邮件,也不会出现在其他任何地方。 尚未找到解决方案。

创建一个 logger,记录已成功发送的电子邮件地址。

您可以创建一个文本文件并写下 mail 成功时的所有电子邮件地址,或者使用一个数据库 table 更新相同的内容。

$success = mail($to2, $subject, $message2,implode("\r\n",$headers), '-fmail@example.com');

if ($success) {
    // <write the email address to a file OR update to database>
    print "<meta http-equiv=\"refresh\" content=\"0;URL=form.php#success\">";
}

此外,您分配了 $success 两次。将您的代码修复为如下所示。

$success1 = mail($to, $subject, $message1,implode("\r\n",$headers), '-fmail@example.com');       // Personal copy
$success2 = mail($to2, $subject, $message2,implode("\r\n",$headers), '-fmail@example.com');  // Further processing

if ($success1 && $success2){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=form.php#success\">";
}

您的代码似乎没问题。

该电子邮件可能被某些认为是垃圾邮件的提供商阻止了。

正确发送电子邮件并不容易...