Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opinion on sending emails from php

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.
like image 902
Michal M Avatar asked Jan 27 '10 08:01

Michal M


People also ask

How send mail in PHP explain in detail?

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters );

Does PHP allows you to send emails directly from a script?

The mail() function allows you to send emails directly from a script. PHP mail is the built-in PHP function that is used to send emails from PHP scripts. It's a cost-effective way of notifying users of important events.

How many emails can PHP mail send?

Thus, it is possible to send 20, 2000 or 20000 e-mails, with the mail function of PHP, without risk of being blocked.


2 Answers

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)

like image 177
Jan Hančič Avatar answered Sep 26 '22 01:09

Jan Hančič


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)
like image 43
sfrench Avatar answered Sep 27 '22 01:09

sfrench