我的网站每当有人发短信(私信)时,都会向我的用户发送一封电子邮件。它表示的内容是“有人在站点名称上向您发送了一条消息,请登录查看”。什么是通过PHP向大量用户发送电子邮件的最有效方式?
此刻我的网站是不是很受欢迎,所以我现在这样的方式一直没有问题的表现明智的:
发送一个PM// code that sends pm here...
// code that stores pm in messages table here...
// notify user by emailing them right away
mail($user_email, $subject, $message);
基本上每次执行邮件功能并在当场发送信息。因此,每发送100封邮件,邮件()就会被调用100次。
我期待我的网站越来越受欢迎,并且随着更多的用户来到更多的PM,所以我认为我目前的做法会成为一场性能噩梦。所以我在想这样做,而不是这样的:
// get the last message that was accounted for by the last cron
$query = mysql_query(" SELECT `last_checked_id` FROM `settings` ");
$last_checked_id = mysql_fetch_array($query);
$user_emails = array();
// get all messages that were added to this table since the last time this cron ran
$query = mysql_query(" SELECT `recipient_id`, `message_id` FROM `messages` WHERE `message_id` > '$last_checked_id' ");
$i = 0;
while($row = mysql_fetch_array($query)) {
if($i == 0) {
// set this as the last message processed
mysql_query(" UPDATE `settings` WHERE `last_checked_id` = '". $row['message_id'] ."' ");
}
// only put this user in the to email list if he hasn't been added already (since there can be multiple messages for the same user but we need to notify him only once)
if(!in_array($row['recipient_id'], $user_emails)) {
array_push($user_emails, $row['recipient_id']);
}
}
$i++;
// send email to all users at one
mail(implode(',', $user_emails), $subject, $message);
我可以将这个脚本为计划并运行它每隔一小时。所有的电子邮件都是一次发送的,邮件功能只被调用一次。唯一的缺点是用户在他们收到PM时不会立即通知,而是在一小时内收到通知。但这并不是什么大事,我可以忍受。
我的问题是:
- 是cron的方法显著更好的性能明智或增加微不足道
- 这是多数大网站做到这一点?还是有更好的方法,一些已建立的图书馆可能会再见?
谢谢。从表
关于SO最低调的PHP问题可能有一些见解http://stackoverflow.com/questions/3905734/how-to-send-100-000-emails-weekly – 2012-04-18 20:11:06
相关:我也会限制一些电子邮件(如线程回复通知),以便他们不会突然收到10封电子邮件,并告诉他们同一个线索已被评论。 – Xeoncross 2012-04-18 20:11:21