Question

I'm improving PHP mailing list code that uses mail() in a loop while iterating through all subscribers. The script used to display a "Maximum execution time of 30 seconds exceeded" error which I solved by adding set_time_limit(0);.

Now there's no error but it took about seven minutes to send 100 messages. What are my options?

Will sending just a single message with all subscribers in BCC help or is it the same "behind the scenes"?

Was it helpful?

Solution

Sending to all as BCC will be a lot faster. The code will execute faster and mail() will be executed only once.

This is the quick fix, but as mentioned, a large BCC list is a safe road to the spam folder. However, using mail() is a sure destination to spam too.

If you want to actually improve it, use PHPMailer from SourceForge and send via SMTP (less spam hits) using cron in batches of X emails once.

The PHP docs state:

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 sending large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.

OTHER TIPS

Sending to all subscribers by specifying them in BCC shouold work faster. Though, while it might be a good option for some private environment, I would not suggest to do that in public web pages/systems, as many might consider such email being a spam.

One option would be to send emails via cron job. In that case, it doesn't really matter how much time it takes to send an email, as long as all emails are eventually sent.

Since you're simply asking for options, here are a few:

  • Profile your code, and see why its slow. Maybe you could iron out a few efficiency issues.
  • Relying on the mail() function is usually not advisable, especially since better alternatives like swiftmail or Zend_mail exist. Although these might be overkill for small mailing requirements
  • It could, possibly, be a server related issue? Maybe talk to your server administrator.

Cron Job is better idea. I used this and it works... Profile you code will help you to figure out the problem.

I do recommend sending the mail via BCC because it will be a lot faster. Your script just has to make one connection to the SMTP server and this server will do the rest for you.

I would also take a look at for example PHPMailer or PEAR::Mail. The bad thing about the mail() method for bulk mails is that it will open a new socket to a SMTP server for every email thats send. A good mail library will create a socket connection to the SMTP server once and will then send all the emails and closes it at the end.

To speed it up a bit more you could also look at your SMTP server configuration (if possible) and try to increase the daemons etc. You might also want to take in consideration that some SMTP servers have maximums you can send.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top