How to Setup SMTP in PHP

SMTP Setup in PHP

If your web application is built on PHP, you are able to deploy the PHP mail function. The function allows you sending the messages directly from your web application. However, some difficulties can be caused by the PHP function of sending emails if the address of a sender is not set properly. Thus, there is an alternative to send messages from the mail server but from the web server. It is recommended to send the emails using SMTP. This is a mechanism to deliver the electronic letters. The SMTP server collects the messages and acts as a source to deliver them to the recipients.

If you need to use an external server to send your emails, you have to set SMTP to the address of the mail server of your ISP. The SMTP address and the outgoing email server will be the same.

How to Setup SMTP in PHP

Unfortunately, the internal mail function configuration for the use of SMTP is available only on Windows. For the other systems, the additional send email drop-ins or packages should be used.

PEAR

The best way to enable SMTP for your PHP is to use a special mail package called PEAR. The name of this package is the abbreviation of “PHP Extension and Application Repository”. You can add PEAR to your web app to enable a certain function including sending the emails via SMTP. PEAR offers more than 600 packages for various functions and needs.

The installation of PEAR is not difficult but it is recommended to learn the process in advance. After the installation, you need to set “include_path” in your “phprc file” to make this installation found in your environment.

Another important point is the use of “/tmp” directory of the server by PEAR. This may fail the installation on a shared server.

PEAR Mail is a flexible tool to set up the desired server of outgoing mail. As a rule, PHP 4 or later already have this package installed.

  • To finalize the setting up, you need to change the following data:
  • host: the name of the outgoing SMTP server.
  • to: the name and email of a recipient.
  • from: the address to be used for sending the email.
  • username: the name of an SMTP user.
  • password: the SMTP authentication password.

PHP mailer

If you are unsatisfied with the work of the PEAR or have some troubles with the installation, you might consider an alternative to this repository. Such an alternative is called PHPmailer. This is a class for PHP to create and transport email. Similar to the PEAR Mail, PHPMailer allows you sending emails via SMTP.

  • PHPMailer might be considered preferable than PEAR for the following reasons:
  • With PEAR Mail, the servers of your hosting service could potentially recognize the socket connection as a brute force attempt because it is cycled for each message.
  • A socket is open only once using PHPMailer. The socket remains open to process messages.
  • You may find many help pages and online examples of the use of PHPMailer.
  • PHPMailer provides an integration with the SMTP servers of Google.

These reasons may change your mind to use PHPMailer instead of PEAR Mail to set up SMTP for the PHP mail service.

Leave a Reply