views:

160

answers:

5

I'm preparing a website that will send email notifications to registered users. From my experience I know, that sending emails is somewhat a painful process for PHP, especially when we're talking about thousands. One of my websites sends email every now and then to 1000-1500 people. It takes around 5mins for PHP to accomplish that, so we run it overnight when the server load is the lowest. I'm using native mail() function without any SMTP. This runs fine on a dedicated server, but I'm not a big fan of this solution.

I want to be able to send similar amounts at any time without risking the server going down (and it to be blacklisted).
I've read, that ideal solution is to send emails in batches (say of 20) every couple of minutes from a script that's triggered by Cron. This seems to me like a really reasonable idea, but... What if I don't have access to Cron (not all hosting providers give access to it) and website isn't popular enough to be able to trigger the script on page load?

I'm insisting on using my server to do the mailing and not any external solution.

PS. I found solutions like these: http://www.mywebcron.com/ but is this any good?


EDIT

Just to add:

  • I'm using CodeIgniter,
  • rate at which emails are sent from my current server is usually 0.2sec per email.
+2  A: 

Use a PHP mailer class such as PHPmailer or SwiftMailer, you can send mails directly trough SMTP that way, which will be much faster. And yes sending large quantities of emails is best done via cron so you send X emails every minute. You will avoid server overload that way. If you can't create cron jobs on your server I suggest you switch your hosting provider, otherwise the website you linked is your only viable alternative (but you are depending on some third party this way, which isn't really cool)

Jan Hančič
+1  A: 

If you can't use a periodic job, you might want to look into a queuing solution like Gearman.

What you would want to do is push all of your emails into the queue and have 1 or more long-running workers that pick jobs off the queue. If you want to add delay into the system, just add a sleep in there as well.

Some really basic pseudocode:

#wherever you launch the jobs from
for each user
  gearman.push(user.generateEmail())


#in your consumer script
while true
  message = gearman.consume()
  message.send()
  sleep(5)
sfrench
+1  A: 

Check out the Mysteries of Asynchronous Processing by Padraic Brady Part 1, 2 and 3. Part 3 deals with eMails specifically.

Gordon
This looks like a really good read. Will have a look.
Michal M
A: 

As an addition to Jan's answer: if you are unable to schedule cron jobs on your server (as is the case with most cheap hosting solutions), there are some pure php alternatives to run scheduled jobs: phpjobscheduler is one of those alternatives.

Powertieke
A: 

There is a CodeIgniter wrapper for SwiftMailer; it's in the wiki, give it a shot:

http://codeigniter.com/wiki/CI_SWIFT_MAILER/

gyo