PHPMailer通过电子邮件循环的最佳解决方案
我正在尝试建立一个召回系统,根据指定的标准向用户发送提醒电子邮件。目前,我可能每周一次发送10-15次提醒,但这可能会在未来升级到每周数百次。我决定使用PHPMailer类来处理邮件发送,但我对PHPMailer非常陌生,因此我不确定我提出的解决方案是否能提供最佳性能,或者我的代码是否可以改进。此外,我还存储电子邮件成功发送后的发送日期 这就是我想到的:PHPMailer通过电子邮件循环的最佳解决方案,php,email,sendmail,phpmailer,Php,Email,Sendmail,Phpmailer,我正在尝试建立一个召回系统,根据指定的标准向用户发送提醒电子邮件。目前,我可能每周一次发送10-15次提醒,但这可能会在未来升级到每周数百次。我决定使用PHPMailer类来处理邮件发送,但我对PHPMailer非常陌生,因此我不确定我提出的解决方案是否能提供最佳性能,或者我的代码是否可以改进。此外,我还存储电子邮件成功发送后的发送日期 这就是我想到的: <?php //I have left out the database connection details for obvious
<?php
//I have left out the database connection details for obvious reasons
require 'PHPMailer-master/PHPMailerAutoload.php';
$result = mysql_query("SELECT * FROM patients_test WHERE recall <> 0 AND DATE_ADD(`last_seen`, INTERVAL recall DAY) < DATE_ADD(NOW(), INTERVAL 21 DAY)");
while($row = mysql_fetch_array($result))
{
//Create new mail object
$mail = new PHPMailer;
///Mail settings
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'host.co.uk'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myusername'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'myusername';
$mail->FromName = 'Mailer';
$mail->addReplyTo('replytoadd', 'Information');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Reminder email';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//Recipient info
$ref = $row['identifier'];
$first_name = $row['first_name'];
$surname = $row['surname'];
$email = $row['email'];
$mail->addAddress($email, $first_name." ".$surname); // Add a recipient
if(!$mail->send())
{
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else
{
echo 'Reminder sent to '.$first_name.' '.$surname.' successfully<br/>';
mysql_query("UPDATE patients_test SET last_reminder= CURDATE() WHERE identifier='$ref'");//Update database to show that email has been sent
$mail->clearAllRecipients();
}
}
?>
填充邮件标题可以减少此问题。邮件服务器有一些规则来定义邮件是否为垃圾邮件。此外,通过循环发送邮件可能会导致阻塞,具体取决于它的配置。查看邮件的标题,查看以获得更有效的方式发送到列表。谢谢Niels。我会考虑页眉。您知道通过循环发送邮件的替代方法吗?我不确定,但您可以处理邮件服务器并管理其选项,或者尊重两次发送之间的最小延迟(您必须知道,或者降低循环速度)。除此之外,我认为动态生成代理是可能的,但这更困难,根本不推荐。但是,如果您的邮件是在垃圾邮件目录中收到的,这不是由于循环造成的。如果您的邮件发送被阻止,那么可能是循环。添加标题解决了您的问题吗?对不起,我的英语不好,谢谢你的帮助。到目前为止,电子邮件还没有被放在垃圾邮件目录中,但我正试图采取措施,一旦我正在开发的系统需要发送更大的邮件,就防止这种情况发生。根据你的建议,我一定会小心确保所有的标题都存在。