Author: Manuel Lemos
Last month viewers: 758
Package: MIME E-mail message sending
An alternative solution consists in using your ISP or a third party SMTP server to relay your messages.
This article explains how to send e-mail with PHP relaying your messages to SMTP servers of Gmail, Hotmail or Yahoo.
However, there are situations on which using the mail function is inconvenient and may prevent that your messages reach the destination.
That is the case for instance when the hosting company limited the number of messages that your site can send, or the IP address of the server does not have a reverse DNS record (PTR), or for some reason the IP address is black listed.
Therefore, you may need to find an alternative way for sending your e-mail messages from PHP.
As explained in another article, sending e-mail messages relaying them to an SMTP server is a slow method, especially if you need to send newsletters or other types of messages to many recipients.
However, if you do not have a better alternative, this is a perfectly valid method for sending your e-mail messages.
PHP can act as an e-mail client and benefit of that possibility by using capable a SMTP client class.
The MIME message comes with a sub-class specialized in sending messages via SMTP. It requires using a separate SMTP class for establishing the connection to the SMTP server and sending the messages.
To use these classes to send messages to e-mail providers that support SMTP relay, certain measures need to be taken.
First you need to have an account in those services and make sure that SMTP relay support is enabled for that account.
All of those providers require authentication, i.e. the SMTP client needs to send the user name and password of the e-mail account.
There are several authentication mechanisms that can be used to identify the user account. The SMTP class needs to use the SASL library (Simple Authentication Support Library) to use an authentication mechanism supported by the server.
Each e-mail provider requires specific setting values to configure aspects of the SMTP protocol. These configuration values are set by the means of different variables of SMTP sub-class of the MIME message class. Here follows the list of the most common configuration variables:
smtp_userAccount of the user to authenticate. Usually it is the e-mail address of the account. For services like Yahoo, you have to set the From: message header to the e-mail address of the account. Gmail sets the From header to the account address if you set it to a different address.
smtp_passwordPassword of the user account
smtp_hostAddress of the SMTP server host to relay the messages
smtp_portTCP port that should be used to connect to the SMTP server. Some e-mail providers support more than one port to work around blocks that certain ISP impose.
timeoutTime that the class should wait for a response from the server. If set to 0, it will wait forever. If your ISP is blocking the port of the remote SMTP server you want to connect, the class will fail when it reaches the timeout period. So, do not set it to 0. If it reaches the timeout because the port is blocked, you may need to try a different port or a different SMTP server.
smtp_sslOption to determine whether a secure connection should be established using SSL protocol. This feature requires at least PHP 4.3 with the OpenSSL extension enabled.
smtp_start_tlsOption to determine whether a secure connection should be set after the connection is already established using TLS protocol. This feature requires at least PHP 5.1 with the OpenSSL extension enabled.
Here follows the configuration values for each of the e-mail providers:
PHP Send Email SMTP Gmail Optionssmtp_host: smtp.gmail.com
smtp_port: 465 or 587
PHP Mail Hotmail Optionssmtp_host: smtp.live.com
smtp_port: 25 or 587
Yahoo SMTP Relay Options with Mail Plus (currently a paid service)smtp_host: smtp.mail.yahoo.com
All you need to do is to include the smtp_mail.php script and replace all calls to the mail function by calls to smtp_mail(). The parameters are the same, so you do not need to change them.
You may need change that script to set the required configuration values to use a given SMTP service provider.
You can download here the complete ZIP archive with scripts to use the Gmail, Hotmail, Yahoo or any other SMTP server, or use the PHP composer tool to install the package with instructions on the download page.
These tips should be sufficient to help you solve the problem of delivering messages using remote SMTP servers. If you still have difficulties, feel free to post a comment to this article. If you liked this article, share with your PHP developer friends.
You need to be a registered user or login to post a comment
Login Immediately with your account on: