Sending messages via Gmail, Hotmail or Yahoo SMTP servers

Sending messages via Gmail, Hotmail or Yahoo SMTP servers

Several popular e-mail providers, like Gmail, Hotmail and Yahoo, now allow sending messages from remote e-mail clients via their SMTP servers.

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_user

Account 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_password

Password of the user account

– smtp_host

Address of the SMTP server host to relay the messages

– smtp_port

TCP 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.

– timeout

Time 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_ssl

Option 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_tls

Option 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:

– Gmail

smtp_port: 465
smtp_ssl: 1

– Hotmail

smtp_port: 25 or 587
smtp_start_tls: 1

– Yahoo Mail Plus (currently a paid service)

smtp_port: 25
* Quick replacement for the mail() function

If you have a PHP application written using the mail function but you need to change it to relay messages to an SMTP service, the MIME message class comes with a replacement function in the smtp_mail.php script named smtp_mail() .

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.
* Other difficulties

This 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.



  1. May 3, 2009 at 7:45 pm

    Hi, nice post. I have been thinking about this issue,so thanks for writing. I’ll definitely be coming back to your blog.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: