我有一个包含 12,000 多个用户的数据库,我正在尝试向所有用户发送一封电子邮件,每个用户都有一个基于他们在数据库中的信息的特定信息。我已经制作了这封电子邮件,以便在周日早上 6 点运行 cron 时发送,我在上周五完成了该功能,它在周日运行,即昨天。这就是发生的事情。
1.) 这封电子邮件从早上 6 点到晚上 7 点整天都在发送
2.) 到那时,它只发送给 750 个用户
3.)在那之后它完全停止了,原因我不知道
PS:,
I am sending the emails using PHPMailer
使用模板,我使用循环遍历所有用户并为每个用户执行计算,用信息填写模板然后发送电子邮件。
下面是一个代码片段,显示了我的工作......
foreach($users as $user){
// Construct the email template
$htmlContent = file_get_contents(__DIR__ . '/../../templates/weekly_spending_template.html');
// Replace some place holders with user's custom information.
$htmlContent = preg_replace('/\$bars/', $bars, $htmlContent);
$htmlContent = preg_replace('/\$labels/', $labels, $htmlContent);
$htmlContent = preg_replace('/\$total/', $currency . ' ' . number_format($total, 0), $htmlContent);
$htmlContent = preg_replace('/\$budget/', $currency . ' ' . number_format($budget, 0), $htmlContent);
$htmlContent = preg_replace('/\$first_name/', ucfirst($user->first_name), $htmlContent);
$htmlContent = preg_replace('/\$remark/', $remark, $htmlContent);
$htmlContent = preg_replace('/\$percentage_difference/', $percentage_difference, $htmlContent);
$htmlContent = preg_replace('/\$others/', $others, $htmlContent);
try {
// Setup email parameters
$mail = new PHPMailer(true);
$subject = "Your weekly spending breakdown";
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->AddAddress($user->email, ucfirst($user->first_name) . ' ' . ucfirst($user->last_name));
$mail->Username = "mymail@example.com";
$mail->Password = "myPassW0rd";
$mail->SetFrom('mymail@example.com', 'Name');
$mail->AddReplyTo("mymail@example.com", "Name");
$mail->Subject = $subject;
$mail->Body = $htmlContent;
$mail->isHTML(true);
if (!$mail->send()) {
echo "Message was not sent.\n";
echo 'Mailer error: ' . $mail->ErrorInfo . "\n";
} else {
echo "Message has been sent.\n";
}
} catch (\Exception $ex) {
echo $ex->getMessage();
}
}
拜托,任何人都可以就如何使这个过程更有效、更快或更好地实现这一目标的选择给我建议吗?谢谢。