从 PHP 发送大量电子邮件

发布于 2024-08-27 12:27:47 字数 403 浏览 10 评论 0原文

我的网站上有 80,000 个用户,最近我放弃了一直在使用的论坛脚本,自己构建了一些非常简单的东西,但效果也同样好(对于我的简单网站来说,论坛脚本过于臃肿且资源密集) )

我唯一失去的是向所有会员群发电子邮件的能力。

所以我想自己想出一个脚本来完成它。环顾四周(包括这里的问题)后,我决定使用 Swift Mailer 将是一个好主意。

然而,我已经阅读了所有文档,但不知道如何发送“一次 100 个”,而且我不知道如何去做。

简单来说。我有一个管理面板,其中的表单有两个输入“主题”和“消息”。当我单击“提交”时,发送 80,000 封电子邮件而不会使服务器崩溃或被标记为垃圾邮件的最安全方法是什么?

我使用的是相当强大的专用服务器,因此不会遇到与共享服务器相关的问题。

预先感谢您的任何建议!

I've got 80,000 users on my site and i've recently turned away from the forum script i've been using and built something very simple myself that works just as well (the forum script was too bloated and resource intensive for my simple site)

The only thing i've lost is the ability to mass email all my members.

So i'm looking to come up with a script to do it myself. After looking around (including questions on here) I decided using Swift Mailer would be a good idea.

However i've been through all the documentation and can't see how to send say "100 at a time" and i'm not sure how to go about it.

To put it simply. I have an admin panel with a form with two inputs "subject" and "message". When I click submit what is the safest way for me to send 80,000 emails without crashing my server or being marked as spam?

I'm on quite a beefy dedicated server so don't have the problems associated with shared servers.

Thanks in advance for any advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷情 2024-09-03 12:27:47

安全的选择是一封又一封地发送电子邮件。我通常每 10 分钟发送不超过 10 封电子邮件。您只需要由 cron 触发的简单脚本即可。

一次发送多封电子邮件是一回事,但让它们全部传递并通过服务器过滤器是另一回事......

Safe option is to send emails one after another. I usually send no more than 10 e-mails evey 10 minutes. Simple script fired by cron is all you need.

Sending many emails at once is one thing but have them all delivered and passed by servers filters is diffrent thing...

孤单情人 2024-09-03 12:27:47

Swiftmailer 这样的类可以选择批量发送电子邮件。

A class like Swiftmailer has options to do mass email.

温柔戏命师 2024-09-03 12:27:47

这是我的想法...假设您托管在 Linux 类型的机器上。这当然是在不知道你的代码的情况下的最低限度。
在服务器上创建一个名为 sendmails.php 的文件

<? 
loop through email addresses however you do it
{
 usleep(250000); // sleep for quarter of a second 
 mail('[email protected]', 'My Subject', 'message');
}
?>

保存它,然后在另一个文件 startemails.php 中您可以在浏览器中打开

<?
system("&php sendmails.php");
?>

即使服务器超时,系统调用仍应完成其工作。使用此方法应在大约 6 小时内发送 80,000 封电子邮件。
更改睡眠时间以增加或减少睡眠时间。

Here's my idea... Assuming you are hosted on a linux type box. This is of course at the bare minimum without knowing your code.
Create a file on the server called sendmails.php

<? 
loop through email addresses however you do it
{
 usleep(250000); // sleep for quarter of a second 
 mail('[email protected]', 'My Subject', 'message');
}
?>

Save it, then in another file startemails.php you can open in your browser

<?
system("&php sendmails.php");
?>

Even if the server times out, the system call should still complete its work. 80,000 emails should send over about 6 hours using this method.
Change the time in usleep to take more or less time.

耳钉梦 2024-09-03 12:27:47

为此,您需要一个守护进程,而不是 Cron,而 Swiftmailer 无法轻松做到这一点。
问题是这样的:你可以让 Swiftmailer 有一个 Cronjob 触发器,假设每 5 分钟触发一次,但是如果它还没有完成发送 10000 封邮件,会发生什么?它可能会启动另一个进程,因此您可能最终会遇到许多进程试图在队列中发送相同的文件。

我使用一种解决方法并创建了一个简单的 PHP 守护进程(bash 脚本也可以正常工作),它不断检查队列中是否有电子邮件;如果是这样,它将启动 Swiftmailer 并发送 1 封电子邮件。 (将 swiftmailer 限制设置为 1)。然后守护程序脚本等待 0.5 秒,并再次检查。

如果需要,Swiftmailer 可以处理多个队列(您需要为每个队列启动第二个守护进程)。

不幸的是,Swiftmailer 没有“send/”文件夹,所以一旦发送,它们就消失了。因此,如果出现错误,您不能简单地将文件从“send/”移回到队列中以重新发送。

Instead of a Cron you'd need a daemon process for this, and Swiftmailer can't easily do that atm.
The problem is this: you could have a Cronjob trigger Swiftmailer lets say every 5minutes, but then what would happen if it isn't finished with sending 10000 mails yet? it would probably start another process, so you'd possibly end up with many processes trying to send the same files in the queue.

I use a workaround and created a simple PHP daemon (a bash script would work fine too) that continuously checks if there are emails in the queue; if so it starts the Swiftmailer and sends 1 email. (set the swiftmailer limit to 1). The daemonscript then waits for 0.5 seconds, and checks again.

Swiftmailer can handle multiple queues if needed (you'd need to start a second deamon process for each queue).

Unfortunately Swiftmailer doesnt have a 'send/' folder, so once they are send they're gone. So in case there was an error you can not simply move the files from 'send/' back into the queue to re-send.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文