Login   Register  
PHP Classes
elePHPant
Icontem

Sending e-mail using SMTP servers of Gmail, Hotmail or Yahoo with PHP - MIME E-mail message sending package blog

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  All package blogs All package blogs   MIME E-mail message sending MIME E-mail message sending   Blog MIME E-mail message sending package blog   RSS 1.0 feed RSS 2.0 feed   Blog Sending e-mail using ...  
  Post a comment Post a comment   See comments See comments (18)   Trackbacks (0)  
>> Next: The Fastest Ways to D...

Author: Manuel Lemos

Posted on:

Package: MIME E-mail message sending

Sometimes it is not possible or it is inconvenient to send e-mail with scripts using the regular PHP mail() function.

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.




Contents

* Trouble sending messages using PHP mail() function

* Sending messages relaying on an external SMTP server

* Sending messages via Gmail, Hotmail or Yahoo SMTP servers

* Quick replacement for the mail() function

* Other difficulties


* Trouble sending messages using PHP mail() function

In most hosting environments, the PHP mail() function is sufficient to send e-mail messages without problems.

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.


* Sending messages relaying on a remote SMTP server

An alternative solution is to send the messages relaying to a remote SMTP server, for instance of your Internet service provider or any other type of e-mail service provider.

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.

http://www.phpclasses.org/blog/package/14/post/1-Sending-mes ...

However, if you do not have a better alternative, this is a perfectly valid method for sending your e-mail messages.


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

http://www.phpclasses.org/smtpclass

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.

http://www.phpclasses.org/sasl

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_host: smtp.gmail.com
smtp_port: 465 or 587
smtp_ssl: 1

- Hotmail

smtp_host: smtp.live.com
smtp_port: 25 or 587
smtp_start_tls: 1

- Yahoo Mail Plus (currently a paid service)

smtp_host: smtp.mail.yahoo.com
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.

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

Login Immediately with your account on:

Facebook ConnectGmail or other Google Account
Hotmail or Microsoft Windows LiveStackOverflow
GitHubYahoo


Comments:

7. test - Shridhar Shenoy (2014-06-04 11:39)
tes... - 0 replies
Read the whole comment and replies

6. prueba de corrreo - Miguel Cespedes (2014-05-13 16:57)
prueba... - 0 replies
Read the whole comment and replies

5. How to sent attachment - Ashique Majeed (2013-10-13 07:36)
attachment... - 1 reply
Read the whole comment and replies

4. smtp.live.com refuses connection ???? - M F (2013-06-03 12:59)
smtp.live.com refuses connection ????... - 8 replies
Read the whole comment and replies

3. quanghits - thienquanghit (2012-05-05 07:49)
bichdao... - 0 replies
Read the whole comment and replies

2. SMTP - svikram (2011-08-05 18:58)
Smtp problem... - 1 reply
Read the whole comment and replies

1. Error: This computer address - Alessio Dev (2009-04-29 17:29)
This computer address: $email_message->localhost ???... - 1 reply
Read the whole comment and replies


>> Next: The Fastest Ways to D...


  Post a comment Post a comment   See comments See comments (18)   Trackbacks (0)  
  All package blogs All package blogs   MIME E-mail message sending MIME E-mail message sending   Blog MIME E-mail message sending package blog   RSS 1.0 feed RSS 2.0 feed   Blog Sending e-mail using ...