2

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"?

random
  • 9,774
  • 10
  • 66
  • 83
samoyed
  • 881
  • 4
  • 13
  • 25

5 Answers5

1

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.

binaryLV
  • 9,002
  • 2
  • 40
  • 42
  • It will work faster ... much faster. One request containing 500 e-mail addresses, and the single e-mail body, is infinitely faster than 500 requests - with the full e-mail - containing 1. – Jeff Parker Feb 24 '11 at 13:22
1

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.

Community
  • 1
  • 1
Andrei Draganescu
  • 387
  • 1
  • 2
  • 10
  • What's the difference in transporting email to the mail server between using `mail()` and "phpMailer via SMTP"? Doesn't `mail()` eventually use the same SMTP? :) – binaryLV Feb 24 '11 at 13:30
  • not necessarily *the same* smtp. usually mail uses the local mail server, unless you can access php.ini and change that. with a library such as pear or phpmailer you can use for example google's smtp. its not about the protocol but because local unknown servers are treated as unsafe by spam filters. – Andrei Draganescu Feb 24 '11 at 13:58
  • @binaryLV: phpMailer knows the proper SMTP headers to use to "look like" genuine e-mail, whereas someone crafting their mail by hand using `mail()` and no particular expertise will end up generating a fairly bare-bones e-mail that spam filters will pick apart. Indeed, such spam filters were largely designed for such a case -- for someone doing a quick, easy, cheap mass mailing. – Lightness Races in Orbit Oct 28 '12 at 13:04
0

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.
Russell Dias
  • 70,980
  • 5
  • 54
  • 71
0

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

Tariq
  • 147
  • 1
  • 9
0

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.

Danny Hiemstra
  • 1,188
  • 1
  • 9
  • 13