我想向两个地址发送一个在线电子邮件表单。每个收件人的邮件内容应该不同。一封邮件用于进一步处理(带密码),另一封邮件作为个人副本(无密码)。
问题:
我创建的网络表单基本上可以工作,但一些用户报告说他们没有收到个人副本我还不能重现这个bug(见下面的更新)我现在担心,有时它也不会向另一个地址发送电子邮件。我认为这与邮件处理逻辑(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
成功时写入所有电子邮件地址,也可以使用一个数据库表来更新该表。
$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'">";
}