Wednesday, May 16, 2012

PHP and Sending Emails


As we know, send emails from PHP scripts can sometimes be vital. We see this in practice nearly every day, especially with registration forms. Most of the times, when we register on a new website, we receive an email from the company which requires us to follow a link to verify that it was a person who submitted the request. This is also the case when we are resetting a password. We receive an email with a random password or to follow a link to create a new password.

Several things exist to send emails from PHP. Among them we find the normal mail() function, the sendmail application, and other scripts which allow us to send emails.

SMTP

To send an email, the SMTP (Simple Mail Transfer Protocol) Protocol is used. This protocol uses port 25 for communication. This means that port 25 must not be blocked on our firewall in the first place. Several forms of SMTP servers exist, and we might not have one running on our local machine. There are several setups which we might come across:

  • SMTP Server without authentication
  • SMTP Server with authentication
  • SMTP Server with authentication and SSL


SMTP Servers without authentication are become rarer, due to the fact that these are many times used by spammers and thus were blacklisted by ISPs as spamming engines. SMTP Servers with authentication require a username and a password to allow mail to pass through. Many times, they use the username and password used for the incoming mail server. SSL is a form of encryption which increases the level of security as the messages are not sent in plain text format.

Gmail

One good common SMTP server is the Gmail SMTP Server. This allows users who have an account with Gmail to authenticate and send emails through the server. Gmail uses SSL and this means that it uses port 465 instead of port 25. SSL is not always enabled in PHP, so it is useful to turn it on.

Enabling SSL in PHP

SSL is required by Gmail and other SMTP servers using SSL. This means that we need to enable it in PHP. There are a few easy steps to enable SSL in XAMPP which I am using for my testing environment. The first thing is to stop the Apache service. Then copy libeay32.dll and ssleay32.dll from the php directory to the apache/bin directory, overwriting the existing copies. Then, make sure that there is an entry for the open SSL extension in the php.ini file. The entry should be like this: extension=php_openssl.dll, without the semicolon in the front. Then start the Apache service again. The service should start with SSL support.

PHP Mail Function

The PHP mail() function is a function used to send emails. This takes 5 parameters:

  • to
  • subject
  • message
  • headers (optional)
  • parameters (optional)


This function uses the SMTP configuration found in the php.ini file. This mainly consists of SMTP server, smtp_port, sendmail_from and sendmail_path. The SMTP server takes the address of the SMTP server, the smtp_port takes the port which the SMTP is configured to work on. The sendmail_from indicated the email address which is shown as the from address, while the sendmail_path is the installation path of the sendmail application. The code snippet below shows a sample of the mail function

<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

PHP Scripts

Several additional scripts exist apart from the mail() function in PHP. Several people and groups of people went a step forward and created complex mail functions to add more features to this important task in PHP. During my research to send emails from PHP, due to the fact that mail() function was not working, I found many advanced projects. Each project had its own way to send emails and its own classes which needed importing to the actual PHP function which I was using. As a matter of fact, I ended up using one of these methods.

Pear and Mail() Script

After a lot of problems with other functions and setups, I managed to find a working script. I am not saying in any way that all of the other scripts contained bugs, far from it, but I did not manage to make them work. I used a Pear extension which actually managed to send emails. I downloaded version 1.2.0 of their scripts which I placed in the directory of my PHP file. I imported the file in my PHP file and then was able to call the method. After setting up values for to, from, subject and body, I was able to setup the server part in the code and then call the send function, as shown below:

            $er = error_reporting(0);
     require_once "Mail.php";

$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "danielborgmt";
$password = "mypassword";
$headers = array
     ('From' => $from,
     'To' => $to,
     'Subject' => $subject);
$smtp = Mail::factory('smtp', array
     ('host' => $host,
     'port' => $port,
     'auth' => true,
     'username' => $username,
     'password' => $password));
$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
     echo("<p>" . $mail->getMessage() . "</p>");
}

The first line shows a function which disables errors caused by not following the strict procedures. This function's result ended up in my testing mailbox :)

My Issues with PHP and Sending Emails

I experienced several issues when setting up this application. Most of them were errors due to not understanding exactly how the PHP sendmail function works, and other small issues with settings for the SMTP servers. The list below shows a number of issues I had:

  • did not properly setup php.ini and incorrect path for sendmail
  • incorrect settings in sendmail.ini
  • incorrect mail server address, incorrect username format, incorrect password
  • incorrect server port
  • using SMTP server with Authentication without providing a username and password
  • disabled SSL
  • closed ports on the Windows Firewall


After a whole week of struggling and research, I managed to overcome these difficulties. The picture below shows a sample email I sent from PHP.


Figure 1: Sample Email from PHP Script

Figure 2: Email details

No comments:

Post a Comment