如何向php中的所有用户发送电子邮件?

[英]How to send an email to all users in php?


Lets say, I have 1000 subscribed users in mysql database. If I want to send an email to all users, what should i do?

可以说,我在mysql数据库中有1000个订阅用户。如果我想向所有用户发送电子邮件,我该怎么办?

EDIT:

What is (everybody talking) cron jobs? Can give example?

(每个人都在谈论)cron工作是什么?能举个例子吗?

10 个解决方案

#1


I'd suggest creating a database table with all the emails that need to be sent. This can be done fairly rapidly. From there, set up a cron job of sorts that takes the top row, emails it, and deletes the row. This way you can parallelize the chore and the user doesn't have to wait for the emails to be sent before their response page loads.

我建议创建一个包含所有需要发送的电子邮件的数据库表。这可以相当快速地完成。从那里,设置一个cron作业,排在第一行,通过电子邮件发送,删除行。通过这种方式,您可以并行处理杂项,用户无需等待在响应页面加载之前发送电子邮件。

#2


Use a combination of a for loop and the mail function such as:

使用for循环和邮件功能的组合,例如:

$rs = mysql_query('SELECT email from emailList LIMIT 0, 10');

while(list($email) = mysql_fetch_row($rs))
{
  // Send Email
  mail($email, 'Your Subject', 'Your Message');
}

But as everyone says donot run it all in one go. I seriously doubt any server would handle the load of making 1000 email requests. Run a cron job that runs such that your code upon each run retrieves lets say 30 - 50 emails and sends them off.

但正如每个人都说donot一气呵成。我严重怀疑任何服务器都会处理1000个电子邮件请求的负担。运行一个运行的cron作业,以便每次运行时检索到的代码可以发送30 - 50封电子邮件并将其发送出去。

#3


The best thing to is write a cron job in the language of your choice that executes the task when the server is usually at its calmest. If you want to use PHP, still use a cron job. Don't do it while responding to a page request WTF-style.

最好的办法是用您选择的语言编写一个cron作业,当服务器通常处于最平静状态时执行该任务。如果要使用PHP,仍然使用cron作业。在响应页面请求WTF风格时不要这样做。

#4


I would suggest doing what everyone else has said, create a seperate database table containing the message, subject, and e-mail address. I would also use PHPMailer (http://phpmailer.codeworxtech.com/) to send the e-mails through SMTP.

我建议做其他人说过的事情,创建一个包含邮件,主题和电子邮件地址的单独数据库表。我还会使用PHPMailer(http://phpmailer.codeworxtech.com/)通过SMTP发送电子邮件。

#5


detach the mail sending code from the creation code (as stefan mai suggested). write all the addresses/subjects/bodies into a table, and process the table later.

从创建代码中分离邮件发送代码(如stefan mai建议的那样)。将所有地址/主题/主体写入表中,稍后处理该表。

then, run the cron-job only every x minutes and only send y mails. if you send too many mails at once, you might get flagged as spammer faster than you think. remove the mailing-task from the table only if sending succeeded.

然后,每隔x分钟运行一次cron-job,只发送y邮件。如果您一次发送过多邮件,您可能会比您想象的更快地被标记为垃圾邮件发送者。仅在发送成功时才从表中删除邮件任务。

  • do this if you got to send bulk mail (e.g. newsletters)
  • 如果您必须发送批量邮件(例如新闻通讯),请执行此操作

  • don't do this if the mail should be sent out instantly (like registration confirmation) - users hate to wait
  • 如果邮件应该立即发送(如注册确认),请不要这样做 - 用户不愿意等待

use a library like phpmailer or swiftmailer.

使用像phpmailer或swiftmailer这样的库。

#6


You could also try using PHP List.

您也可以尝试使用PHP列表。

Install PHP List, export your DB of email addresses and import it into PHP List as a CSV. Then you don't have to worry about about managing user's, etc and you get click tracking.

安装PHP列表,导出您的电子邮件地址数据库并将其作为CSV导入PHP列表。然后,您不必担心管理用户等,并获得点击跟踪。

#7


You can begin by setting a query to get each email for each user, something along the lines of:

您可以首先设置查询以获取每个用户的每封电子邮件,其内容如下:

SELECT email FROM users;

Once you have that stored in a variable you can use a combination of foreach() and mail() to send an email to each user.

存储在变量中后,您可以使用foreach()和mail()的组合向每个用户发送电子邮件。

#8


i would create a table with all users and emails and an status. Setup 2 or 3 Cronjobs to process this table.

我会创建一个包含所有用户和电子邮件以及状态的表。设置2或3个Cronjobs来处理此表。

In your php script fetch 30 entrys from the db (limit 30) and process them with an foreach loop. After each run change the status of the "email" or delete the entry from the db.

在你的php脚本中,fetch 30来自db(限制30)并使用foreach循环处理它们。每次运行后更改“email”的状态或从db中删除条目。

With this way you can be sure that your server is able to handle the system load.

通过这种方式,您可以确保您的服务器能够处理系统负载。

#9


Should every email be unique to each user? If not, do not send a new mail for each user, add a BCC (Blind carbon copy) to a single mail. This will make your smtp server happy.

每封电子邮件都应该是每个用户唯一的?如果没有,请不要为每个用户发送新邮件,将BCC(盲目抄送)添加到单个邮件中。这将使您的smtp服务器满意。

#10


Write a PHP script that handles the send mail the way you'd like (Stefan Mai's answer is a great way to do this). Design the script so that it logs it's work, and doesn't print anything when it's running (this leaves you a way to find errors later). Assuming you're running Linux you can setup cron to run the script at the appointed time.

编写一个PHP脚本,以您希望的方式处理发送邮件(Stefan Mai的答案是一个很好的方法)。设计脚本以便它记录它的工作,并且在它运行时不打印任何东西(这使你有办法在以后找到错误)。假设你正在运行Linux,你可以设置cron来在指定的时间运行脚本。

Edit your cron jobs by typing crontab -e into a terminal session. You then enter something like this (this will run the job every night at midnight).

通过在终端会话中键入crontab -e来编辑您的cron作业。然后你输入这样的东西(这将在每晚午夜运行工作)。

 0 * * * * wget -O - -q -t 1 http://www.example.com/mymailer.php
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2009/06/10/13ced8a935646b866e3bc17d86610034.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告