Back to the Top
HOW DO I SEND BULK EMAIL WITH PHP?

This seems to be a common request around here and I didn't find enough info in the 37 posts relating to bulk email. I thought I would start a thread to point out the pitfalls and try to provide solutions. I hope you'll over look any shortcomings because I'm just trying to make an informative thread and get answers. These are the pitfalls I've found:

1 - MAIL() DOESN'T WORK
mail() is a basic way to send email using PHP. If it doesn't work on your server then you need to configure PHP properly. Talk to your sysadmin or read here or tell your sysadmin to read the php documentation:
http://php.net/mail

If your sysadmin is mean or some network situation prevents you from using the php mail() command, you will need some code to let you send mail some other way. An excellent tool which connects to an SMTP server just like your mail client is PHPMailer:
http://phpmailer.sourceforge.net/

QUESTION: Can anyone recommend a class like PHPMailer which supports queuing of large numbers of emails? I think PHPList does but it's totally overkill if you're just trying to queue up some emails within an existing site.

2 - EMAIL IS BOUNCING
Assuming the email gets sent from my server it is inevitable that some will bounce. In that case, an email server (usually the desination server) will generate a bounce message or Delivery Status Notification which gets sent to the email listed in the RETURN-PATH header of your mail message. You should probably also set FROM. How do you do this? It usually depends on how you sent the mail in the first place. Check the docs on your mail-sending method. If you're hankering for a more technical read, you can read RFC 3461.

QUESTION: How does one process the bounces? Obviously you need to be able to check the email account where all the bounces go and correlate messages there with addresses in your send list. [NEED MORE INFO]

3 - EMAIL DISAPPEARS
Some mail you send never arrives. No errors, no trace. In this case you need to make sure your server is actually sending it by checking the mail logs (ask your sys admin). If it is in fact sent by your server then it may be getting SPAM FILTERED. This is a tough situation and should be solved on a case by case basis. Two very helpful things you can do are to make sure the email address in your RETURN-PATH and FROM headers in your messages match the domain returned from a reverse lookup of your mail server's IP address. If this is impossible, check into setting up a Sender Policy Framework. If that doesn't help, call the domains that are blocking your email ask them why.

QUESTION: Other suggestions? Did I miss anything?

4 - TOO MANY EMAILS, SCRIPT IS TIMING OUT
PHP scripts usually have a time limit. This can be a definite problem if you have a lot of emails to send. Depending on your PHP setup, you might just be able to add this line to the top of your email-sending script:

set_time_limit(0);

HOWEVER, if your server is in safe mode, that won't work. Talk to your sys admin - s/he might change the PHP ini file and put a longer value.

5 - SCRIPT STILL TIMING OUT
If you can't get your mean old sys admin to change the value or if you have more emails to send than can be sent in a day, then you have a significant problem that requires pretty special treatment like a faster email server, a faster connection for your server, or something fairly drastic. Try putting a detailed post in the coding forum.

    oh...and

    6 - MY SCRIPT IS HACKED! HELP I'M AN OPEN RELAY!
    This has happened to me once and was due to the fact that bots/hackers had found my script and were bypassing my form and posting directly to my form handling script. Generally this was accomplished on my sites by injecting CC or BCC email headers into a form where a user entered a return email address. I was able to prevent this in most cases by screening the form input data for line breaks in the email address.

    I would highly recommend the use of PHPMailer or some other class in this case to prevent these sorts of things because the developers are on guard against such things.

      2 years later
      sneakyimp;10781635 wrote:

      Back to the Top

      5 - SCRIPT STILL TIMING OUT
      If you can't get your mean old sys admin to change the value or if you have more emails to send than can be sent in a day, then you have a significant problem that requires pretty special treatment like a faster email server, a faster connection for your server, or something fairly drastic. Try putting a detailed post in the coding forum.

      One way to get around this kind of issue is to setup a que of some sort. I had to setup a script that sends out 200 emails at a time. After the script is done with the first 200 emails, it calls itself and processes the next 200 and so forth until it sends all emails in the que.

        9 months later

        Lately I've run across issues where PHPMailer doesn't work. PEAR mail appears to have more thorough SMTP implementation.

          Write a Reply...