Sending Emails using Authenticated SMTP

The code that is required to send an email via SMTP is reasonably straightforward. Admittedly it isn't quite as easy as calling the mail() function and it does require a few additional values but if we write a function that takes almost the same parameters as the mail function we can make it almost as easy to use.

Here's an smtpMail function where the first four parameters it wants are exactly the same as the built in PHP mail function. The only difference apart from the name of the function is the fifth parameter. For the smtpMail function to work the fifth parameter needs to be a comma separated list of all the email addresses that the email is to be sent to (that is all the addresses that you have included in the To, Cc and Bcc headers. The only difference in the other parameters is that this function does not strip out the Bcc header the way the mail function does so you need to leave that out and just have those addresses specified in the fifth parameter.

We do have a further four additional values that need to be set but those will be fixed values and so can be defined inside the function. The first two of these are the SMTP server address and the port that SMTP is running on. If the mail server is running on the local computer then specifying for the address will work and if it is running on the standard smtp port then the port number is 25. Your hosting provider will be able to tell you if either of these values needs to be different (eg. some hosts run smtp on port 26 instead of 25). The other two extra values specify the email address and password of an email account that you have on this hosting account. These are used to authenticate that you are allowed to send emails from the server because you have an email account there and that email address is also recorded as the origin of the email should anyone wish to trace where the email came from. Note that the from address displayed in the actual email itself can be different from this address.

The following code shows the complete function. The $resp field makes it easy to see what is happening during the email send process as it effectively logs both the messages sent to smtp and the responses received back from smtp. If you were to echo that value after returning from the function and then view the source of the resultant page then you would be able to see the entire communication between the function and the smtp server.

function smtpMail($to, $subject, $body, $head, $toaddr) {
$SmtpServer = '';
$SmtpPort = 25;
$SmtpUser = '';
$SmtpPass = 'mypassword';
$resp = '';
if ($stmplnk = fsockopen ($SmtpServer, $SmtpPort)) {
$cmd = "EHLO ".$HTTP_HOST."\r\n";
fputs($stmplnk, $cmd);
$cmd = "auth login\r\n";
fputs($stmplnk, $cmd);
fputs($stmplnk, base64_encode($SmtpUser)."\r\n");
fputs($stmplnk, base64_encode($SmtpPass)."\r\n");
$cmd = "MAIL FROM: <".$SmtpUser.">\r\n";
fputs ($stmplnk, $cmd);
$resp.= $cmd.fgets($stmplnk,1024)."\r\n";
$toad = explode(',', $toaddr);
foreach ($toad as $t) {
$cmd = "RCPT TO: <".$t.">\r\n";
fputs ($stmplnk, $cmd);
$resp.= $cmd.fgets($stmplnk,1024)."\r\n"; }
$cmd = "DATA\r\n";
fputs($stmplnk, $cmd);
$cmd = "To: <".$to.">\r\nSubject: ".$subject."\r\nDate: ".date(DATE_RFC2822)."\r\n".$head."\r\n\r\n".$body."\r\n.\r\n";
fputs($stmplnk, $cmd);
$cmd = "QUIT\r\n";
fputs ($stmplnk, $cmd);
return $resp;

The function starts by performing an fsockopen call to establish a communication link with SMTP. It then does an EHLO call to establish that it wants to send an email. SMTP the acknowledges that the connection has been established. We need to read each of the responses from SMTP before we can send the next command. We next need to establish that we are authorised to send an email by performing an auth_login and sending both the user and password.

With the connection established the next step is to advise the origin of the email by sending a MAIL FROM command. This needs to be an email address that exists on the current hosting so we may as well use the same one we used to log in.

The next step is to identify where the email is to be sent. We do this by sending one or more RCPT TO commands, one for each email address that the email is to be sent to. This is where the list of all the To, Cc and Bcc addresses comes in as the function will create a RCPT TO for each email address in the list.

Note that the calls so far simply identify the origin and intended recipients of the email. None of this is part of the actual email itself. To send the actual email we need to next do a DATA call to tell SMTP that we are about to supply the content of the email. We then send the actual content of the email which will consist of the To address, the subject, any other headers (from, cc etc) and then the body of the email. We tell SMTP that the email is complete by sending a line containing a single period after the last line of the email. If for any reason you need the email to contain a single period on a line by itself you need to specify it as two periods.

Once the complete email has been provided we then indicate that we have finished with the connection by sending a QUIT command. We can then close the connection to SMTP.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow