5

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!

mirabilos
  • 5,123
  • 2
  • 46
  • 72
Rob
  • 53
  • 4

4 Answers4

1

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

smentek
  • 2,820
  • 1
  • 28
  • 32
  • I realise a cron job is a good way to do it. But I don't want to necessarily do it at 3pm every day. I want to be able to go into the backend, write the email and click send. Is there a way to create a one off cron job in php? (although I think that doesn't fit under the term "cron job") – Rob Apr 05 '10 at 13:24
  • 1
    Cron can be used in many ways. You may for example have a simple model (one table in databse) called: emailsToSend (kind of simple buffer). Fields in database for model like: id, email_id, created_at, sent. Every record in emailsToSend is an event: "sending an e-mail'. You set cron to fire your script every 5 minutes. Script: 1. checks if there are any records (e-mails to send). 2. Takes last 10 or them (by date of creation), 3. Send them and mark tham as send (simple boolean flag for this) 4. Ends. Script is fired by cron, so you will not have to do it from browswer... – smentek Apr 05 '10 at 14:02
0

A class like Swiftmailer has options to do mass email.

Frank Heikens
  • 117,544
  • 24
  • 142
  • 135
  • Like I said i'm already using Swift Mailer. However, from what I can tell if I add 80,000 people via bcc it's just going to loop through that 80,000 people. I don't really want to leave my browser open while it does that. It will also probably cause a server timeout. So I need another way of doing so. If Swift Mailer is able to do this I haven't been able to find it in the documentation. – Rob Apr 05 '10 at 13:26
  • In a normal situation you use a cron job at your server which starts your script. – Klaaz Mar 16 '12 at 11:12
0

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('user@example.com', '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.

SethCoder
  • 66
  • 1
  • 5
  • You could also modify it to send X amount of emails then wait X amount of time before doing the next batch. – SethCoder Apr 05 '10 at 17:04
0

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.

Alex
  • 5,759
  • 1
  • 32
  • 47