I am creating a system, where a list of thousands of emails will be sent periodically, I know that the mail()
function in PHP is quite heavy, specially if calling it too many times simultaneously.
Roughly the way my system works , is that I create queue of emails in MySQL and send them in batches of 25 using mail()
, removing from the table the top 25 sent. And I wait 2 seconds between each set of 25.
Is this too much effort for the server or I can push it a bit further?
Lets say 50 per second? Or there is a better way of sending many emails in less time without sacrificing Server performance.
I have a dedicated server without any mail()
call limit.
There are other factors to consider besides performance, but the short answer is: there are better options. Amazon SES and MailChimp are the two I know about have heard positive feedback on.
Look at j08691's answer regarding the performance, but other issues with using mail()
for this purpose include:
Scalability (you will hit a wall that no SMTP server can handle eventually, and you're already thinking about it)
Integrity - You are much more likely to get flagged as spammy when rolling your own mass mailer, especially using mail
as it uses the local sendmail by design.
Cost/Benefit and ROI - the reliable mass-mailers get it right and for a competitive rate. At some point you are paying yourself less per hour to maintain your mail server when it crashes, getting off of black lists, writing the email layout by hand, general upkeep, etc etc than you would paying for the mass mailer service.
Overall, the big issue is that you have to do all the work yourself and you're likely to get flagged as SPAM for the benefit of not paying for a service that will be able to send hundreds of emails a second versus a hundred a minute when PHP isn't busy doing everything else it handles for your web app.
Personal anecdote (not an endorsement for SES, just mass-mailers) : We had a client that sent 100k+ emails per campaign, with 1 - 3 campaigns per day minimum. They started complaining that the clients were getting emails about "daily deals" 2 days late. It wasn't because the Mailer library was slow (even this app avoided using plain mail
), it was that it couldn't be sure to send all of the emails for every campaign before the email was irrelevant. We switched them over to SES (with some optimizing on our end, but not much), and they could clear a campaign in under an hour.
From the PHP manual:
Note:
It is worth noting that the mail() function is not suitable for larger
volumes of email in a loop. This function opens and closes an SMTP
socket for each email, which is not very efficient.
For the sending of large amounts of email, see the » PEAR::Mail, and »
PEAR::Mail_Queue packages.
Try using PHPMailer. i used it to send about 100 mails everyday without any problem