PHP Classes

Sending messages to many recipients via SMTP in PHP - SMTP E-mail sending class package blog

Recommend this page to a friend!
  All package blogs All package blogs   SMTP E-mail sending class SMTP E-mail sending class   Blog SMTP E-mail sending class package blog   RSS 1.0 feed RSS 2.0 feed   Blog Sending messages to m...  
  Post a comment Post a comment   See comments See comments (9)   Trackbacks (0)  

Author:

Viewers: 1,417

Last month viewers: 472

Package: SMTP E-mail sending class

Once in a while, PHP developers ask me how to send messages via SMTP to many recipients using the same connection.

This article explains how to do it properly and warns about the eventuality of having your messages classified as spam when it is not done the right way.

It also gives tips of how to improve the performance on delivering newsletters to many recipients without having your messages confused with spam.




Loaded Article
Contents

* Sending messages to many recipients
* The headers do not matter to SMTP servers
* Do not use the SMTP class alone
* Avoid using Bcc headers
* Optimizing deliveries to many recipients
* SMTP is the slowest method to queue messages
* Other reasons that may prevent your message to reach the recipients


* Sending messages to many recipients

Sending messages to multiple recipients is very easy. As you may see in the test_smtp.php example script there is a call to the SendMessage function.

phpclasses.org/browse/file/31.html

The second argument of the SendMessage function should be an array with the addresses of one or more recipients. Each entry of the array should be a string single address. Do not put multiple addresses in each address string, nor include the recipient names there.

It does not matter if the recipient addresses are in the To:, Cc:, Bcc: or any other headers that usually contain recipient addresses. All recipient addresses should be extracted from the headers and passed to the SendMessage function in an array, one address per array entry.


* The headers do not matter to SMTP servers

Not everybody is aware of this, but the fact is that SMTP servers do not process the message headers to extract sender or recipient addresses. That has to be done by the e-mail sending application.

Therefore, it does not matter if you put headers with the recipient addresses in the message. What matters is the recipient list that you pass to the SendMessage function.

That function will send individual RCPT TO: commands to the server, so it knows where you want to send the message. If a recipient is rejected, the SendMessage function will fail and aborts sending the message.


* Do not use the SMTP class alone

Yes, that's right. Do not use this class just by itself to send messages! Let me explain.

The problem is that sending e-mail messages is not a trivial matter. Internet e-mail standards are complicated. If you do not send messages that comply with the e-mail standards, chances are that your messages may be discarded. Malformed messages may be considered spam.

If you really need to send messages using an SMTP server, a better solution is to use this class in conjunction with the MIME message class.

phpclasses.org/mimemessage

Not only the MIME message is capable of composing and sending messages compliant with the e-mail standards, but it also makes it easy to extract the list of recipients from the headers, hide the Bcc: headers from the actual messages, set the recipients properly, and skips invalid recipients without aborting the deliveries.

This way it is safer to make your messages reach your recipients avoiding to get them caught by spam filters.


* Avoid using Bcc: headers

When you need to send a message to many recipients, for instance a newsletter, an easy way to do it is to put all recipients in Bcc headers. That way, you can send a single message to all the newsletter subscribers and avoid that they discover each other addresses.

The problem with this approach is that some mail systems, notably Hotmail, consider spam messages on which the recipient address is not in a visible header.

Since Bcc headers are removed before the message is sent, to avoid having your messages being classified as spam, you need to set the recipients address in To: or Cc: headers.

However, since we do not want the newsletter recipients to see each other addresses, the remaining solution is to send separate messages to each recipient.


* Optimizing deliveries to many recipients

Sending separate messages to each recipient takes more time and CPU to queue messages to many recipients, but at least your newsletters are not considered spam.

The MIME message class mentioned above provides means to improve the performance of delivering messages to many recipients. You just need to call the SetBulkMail function before starting the deliveries.

Among other optimization tricks, the MIME message can reuse the same SMTP connection to send messages to different recipients. It can also cache message body content, if the message body does not change for each recipient.

All these tricks can provide significant performance improvements. Look at the test_personalized_bulk_mail.php example script to see how it works.

phpclasses.org/browse/file/2604.htm ...


* SMTP is the slowest method to queue messages

Despite a common belief, if you need to send messages to many recipients, queueing them via SMTP is the slowest method.

If you can, inject messages directly in the message queue using sendmail or compatible programs (Qmail, Postfix, Exim, etc.). That is what the PHP mail() function does when running PHP on Linux or other UNIX like systems.

The reason why some people believe that SMTP is faster than using the PHP mail function, thus sendmail, is because by default when you queue a message, sendmail attempts to deliver the message right away. This may take a long while.

The solution for this is to pass sendmail a switch option that makes it just drop the messages in the local mail server queue and deliver it later, instead attempting to deliver it right away. The MIME message package comes with a specialized sub-class that passes the right switches when using sendmail for bulk deliveries.

If you use Qmail or Postfix, they work this way by default, so you do not need to pass any option switch.

Dropping the messages in the local message server queue is much faster than using SMTP connections, because there is a lot of needless overhead when using SMTP (TCP) connections to the local machine.

SMTP is only recommended when you do not have a local mail server like sendmail or compatible, and you need to use your ISP SMTP server or some other remote mail server as Gmail, Hotmail, Yahoo, etc..

Another article will be written soon to let you know how to use those remote mail servers to send messages with these classes.


* Other reasons that may prevent your message to reach the recipients

Sending messages to many recipients in PHP, while assuring that the messages really reach the destinations, was a topic of a talk that I gave several times some time ago.

The talk explains several reasons that may prevent messages from reaching the destination, including those mentioned in this article.

The slides of that talk are listed in the MIME message package page. You can go here to view the English version:

- Sending E-mail that reaches the destination
phpclasses.org/browse/video/3/packa ...

Here you may find the Portuguese version of the slides:

- Enviando E-Mail Que Chega Ao Destino usando PHP
phpclasses.org/browse/video/2/packa ...

Here you can see the video of the of the talk in Portuguese:

phpclasses.org/browse/video/1/packa ...


Do you have more questions about these and other tips to solve mail sending delivery problems? Feel free to post a comment here.



You need to be a registered user or login to post a comment

1,616,107 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

3. Hiding recipients - Marek Strop (2010-11-26 21:42)
How to hide recipients in case BCC shouldn’t be used... - 3 replies
Read the whole comment and replies

2. cc header - evon (2009-04-21 06:35)
multiple recipient... - 2 replies
Read the whole comment and replies

1. Hotmail... - Elissaios (2009-04-09 14:05)
Hotmail is the problem... - 1 reply
Read the whole comment and replies



  Post a comment Post a comment   See comments See comments (9)   Trackbacks (0)  
  All package blogs All package blogs   SMTP E-mail sending class SMTP E-mail sending class   Blog SMTP E-mail sending class package blog   RSS 1.0 feed RSS 2.0 feed   Blog Sending messages to m...