PHP,电子邮件和Cron [英] PHP, Email and Cron
本文介绍了PHP,电子邮件和Cron的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我重写一下我的问题,我有一个mysql数据库,它持有电子邮件发送,在共享主机上。我想运行一个cron作业,它将读取数据库并在数据库中每隔10分钟发送一次消息。
Let me rephrase my question, I have a mysql database that is holding emails to be sent, on a shared host. I would like to run a cron job that will read the database and sent out any messages in the database every 10 minutes or so.
现在我的问题是,最好的办法是使用php读取我的数据库,并发送小批量的电子邮件,这样我就不会淹没共享主机。
Now my question is, what is the best way with php to read my database and send out the emails in small batched so that I don't overwhelm the shared host.
推荐答案
我想出了这个解决方案类似于PDO一个。作为cron作业运行时是否有任何不可预见的问题?
Well I came up with this solution similar to the PDO one. Are there any unforeseen problems with running this as a cron job?
<?php
$con = mysql_connect("localhost","root","123456");
$throttle = 0;
$batch = 50;
$pause = 10; // seconds
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("maildb", $con);
// Message Table
$MSGresult = mysql_query("SELECT * FROM msgs");
// User Table
$USERresult = mysql_query("SELECT * FROM members");
while($MSGrow = mysql_fetch_array($MSGresult))
{
while($USERrow = mysql_fetch_array($USERresult))
{
mail($USERrow['email'],$MSGrow['subject'],$MSGrow['body']);
$throttle += 1;
if ($throttle > $batch ) { sleep($pause); $throttle = 0;}
}
mysql_data_seek($USERresult,0);
}
mysql_close($con);
?>
这篇关于PHP,电子邮件和Cron的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文