Send mass emails from PHP

1.9k Views Asked by At

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!

4

There are 4 best solutions below

2
On BEST ANSWER

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...

2
On

A class like Swiftmailer has options to do mass email.

1
On

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.

0
On

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.