使用 PHP 发送大量电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1118154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending mass email using PHP
提问by Alan
I am currently writing a music blog. The administrator posts a new article every 2-3 days. Once the administrator posts an article, a mass email will be sent to around 5000 subscribers immediately.
我目前正在写一个音乐博客。管理员每2-3天发布一篇新文章。管理员发布文章后,将立即向大约 5000 名订阅者发送群发电子邮件。
What is the best way to implement the mass mail feature?
实现群发邮件功能的最佳方式是什么?
Does the following function work?
以下功能有效吗?
function massmail()
{
$content = '...';
foreach ($recipients as $r) {
$_content = $content . '<img src="http://xxx/trackOpenRate.php?id='.$r.'">';
mail($r, 'subject', $_content);
}
}
Another question: If all 5000 subscribers are using Yahoo Mail, will Yahoo treat it as a DDOS attack and block the IP address of my SMTP server?
另一个问题:如果所有 5000 个订阅者都在使用 Yahoo Mail,那么 Yahoo 是否会将其视为 DDOS 攻击并阻止我的 SMTP 服务器的 IP 地址?
采纳答案by Extrakun
First off, using the mail() function that comes with PHP is not an optimal solution. It is easily marked as spammed, and you need to set up header to ensure that you are sending HTML emails correctly. As for whether the code snippet will work, it would, but I doubt you will get HTML code inside it correctly without specifying extra headers
首先,使用 PHP 附带的 mail() 函数不是最佳解决方案。它很容易被标记为垃圾邮件,您需要设置标题以确保您正确发送 HTML 电子邮件。至于代码片段是否会起作用,它会起作用,但我怀疑您是否会在不指定额外标题的情况下正确地在其中获取 HTML 代码
I'll suggest you take a look at SwiftMailer, which has HTML support, support for different mime types and SMTP authentication (which is less likely to mark your mail as spam).
我建议你看看SwiftMailer,它支持 HTML,支持不同的 mime 类型和 SMTP 身份验证(它不太可能将你的邮件标记为垃圾邮件)。
回答by bumperbox
I would insert all the emails into a database (sort of like a queue), then process them one at a time as you have done in your code (if you want to use swiftmailer or phpmailer etc, you can do that too.)
我会将所有电子邮件插入数据库(有点像队列),然后像您在代码中所做的那样一次处理一个(如果您想使用 swiftmailer 或 phpmailer 等,您也可以这样做。)
After each mail is sent, update the database to record the date/time it was sent.
每封邮件发送后,更新数据库以记录发送的日期/时间。
By putting them in the database first you have
首先将它们放入数据库中,您就拥有了
- a record of who you sent it to
- if your script times out or fails and you have to run it again, then you won't end up sending the same email out to people twice
- you can run the send process from a cron job and do a batch at a time, so that your mail server is not overwhelmed, and keep track of what has been sent
- 您发送给谁的记录
- 如果您的脚本超时或失败并且您必须再次运行它,那么您最终不会向人们发送两次相同的电子邮件
- 您可以从 cron 作业运行发送过程并一次执行一批,这样您的邮件服务器就不会不堪重负,并跟踪已发送的内容
Keep in mind, how to automate bounced emails or invalid emails so they can automatically removed from your list.
请记住,如何自动退回电子邮件或无效电子邮件,以便它们可以自动从您的列表中删除。
If you are sending that many emails you are bound to get a few bounces.
如果您发送那么多电子邮件,您肯定会收到一些退回邮件。
回答by Michiel Buddingh
This is advice, not an answer: You are much, much better off using dedicated mailing list software. mailmanis an oft-used example, but something as simple as mlmmjmay suffice. Sending mass mails is actually a more difficult task than it actually appears to be. Not only do you have to send the mails, you also have to keep track of "dead" addresses to avoid your mail, or worse, your mailserver, being marked as spam. You have to handle people unsubscribing for much the same reason.
这是建议,而不是答案:您最好使用专用的邮件列表软件。 mailman是一个经常使用的例子,但像mlmmj这样简单的东西可能就足够了。发送群发邮件实际上比看起来更困难。您不仅必须发送邮件,还必须跟踪“死”地址以避免您的邮件,或者更糟的是,您的邮件服务器被标记为垃圾邮件。出于同样的原因,您必须处理退订的人。
You can implement these things yourself, but particularly bounce handling is difficult and unrewarding work. Using a mailing list manager will make things a lot easier.
您可以自己实现这些东西,但尤其是反弹处理是一项困难且没有回报的工作。使用邮件列表管理器将使事情变得容易得多。
As for how to make your mail palatable for yahoo, that is another matter entirely. For all its faults, they seem to put great stock in SPFand DomainKey. You probably will have to implement them, which will require co-operation from your mail server administrator.
至于如何让你的邮件对雅虎来说是可口的,那完全是另一回事。尽管存在所有缺点,但他们似乎在SPF和 DomainKey 中投入了大量资金。您可能必须实施它们,这需要您的邮件服务器管理员的合作。
回答by usoban
You may consider using CRON for that kind of operation. Sending mass mail at once is certainly not good, it may be detected as spam, ddos, crash your server etc.
您可以考虑使用 CRON 进行此类操作。一次发送大量邮件肯定不好,它可能会被检测为垃圾邮件、ddos、服务器崩溃等。
So CRON could be a great solution, send 100 mails at once, then wait a few minutes, next 100, etc.
所以 CRON 可能是一个很好的解决方案,一次发送 100 封邮件,然后等待几分钟,下 100 封等等。
回答by Nir Levy
Do notsend email to 5,000 people using standard PHP tools. You'll get banned by most ISPs in seconds and never even know it. You should either use some mailing lists software or an Email Service Providerdo to this.
不要使用标准 PHP 工具向 5,000 人发送电子邮件。您将在几秒钟内被大多数 ISP 禁止,甚至永远不会知道。您应该使用一些邮件列表软件或电子邮件服务提供商来做到这一点。
回答by Conrad
Why don't you rather use phplist? It's also built on top of PHP Mailer and a lot of industry leaders are using it. I've used it myself a couple of times to send out bulk mails to my clients. The nice thing about phplist is that you can throttle your messages on a domain level plus a time limit level.
你为什么不宁愿使用phplist?它也建立在 PHP Mailer 之上,许多行业领导者都在使用它。我自己使用过几次向我的客户发送大量邮件。phplist 的好处是您可以在域级别和时间限制级别上限制您的消息。
What we've also done with a couple of internal capture systems we've got was to push our user base to the mailling list and then have a cron entry triggering a given mail each day. The possibilities are endless, that's the awesome thing about open source!
我们还使用我们拥有的几个内部捕获系统完成的工作是将我们的用户群推送到邮件列表,然后每天都有一个 cron 条目触发给定的邮件。可能性是无限的,这就是开源的美妙之处!
回答by bucabay
Also the Pear packages:
还有梨包:
http://pear.php.net/package/Mail_Mimehttp://pear.php.net/package/Mailhttp://pear.php.net/package/Mail_Queue
http://pear.php.net/package/Mail_Mime http://pear.php.net/package/Mail http://pear.php.net/package/Mail_Queue
sob.
哭泣。
PS: DO NOT use mail() to send those 5000 emails. In addition to what everyone else said, it is extremely inefficient since mail() creates a separate socket per email set, even to the same MTA.
PS:不要使用 mail() 发送那 5000 封电子邮件。除了其他人所说的之外,它的效率极低,因为 mail() 为每个电子邮件集创建了一个单独的套接字,即使是同一个 MTA。
回答by Krishna Ghodke
You can use swiftmailer for it. By using batch process.
您可以使用 swiftmailer。通过使用批处理。
<?php
$message = Swift_Message::newInstance()
->setSubject('Let\'s get together today.')
->setFrom(array('[email protected]' => 'From Me'))
->setBody('Here is the message itself')
->addPart('<b>Test message being sent!!</b>', 'text/html');
$data = mysql_query('SELECT first, last, email FROM users WHERE is_active=1') or die(mysql_error());
while($row = mysql_fetch_assoc($data))
{
$message->addTo($row['email'], $row['first'] . ' ' . $row['last']);
}
$message->batchSend();
?>
回答by jespir
There is more into it aside from using a software. If you could create a bulk emailer program that sends intermittently. Say if you will send 5,000 recipients, create a loop that would send 38 lists per sending then pause for 10 seconds. I have an actual experience sending 500 manually per days for the past weeks and so far i have good results.
除了使用软件之外,还有更多内容。如果您可以创建一个间歇性发送的批量电子邮件程序。假设您将发送 5,000 个收件人,创建一个循环,每次发送将发送 38 个列表,然后暂停 10 秒。在过去的几周里,我有过每天手动发送 500 个的实际经验,到目前为止我有很好的结果。
Another consideration are the content of your email. Nowadays it is a standard that you need to put your physical office address and the "unsubscribe" opt-out. These are factors that majority of recipient emails servers are checking. If you don't have these they will classify you as spammer.
另一个考虑因素是电子邮件的内容。如今,您需要输入实际办公地址并选择退出“取消订阅”已成为标准。这些是大多数收件人电子邮件服务器正在检查的因素。如果您没有这些,他们会将您归类为垃圾邮件发送者。
Mailchimp is my best recommendation to use if you want a paid service provider in sending to your email subscriber NOT sending unsolicited or cold email marketing.
如果您希望付费服务提供商向您的电子邮件订阅者发送而不是发送未经请求的或冷的电子邮件营销,Mailchimp 是我最好的推荐使用。
Hope it helps.
希望能帮助到你。

