How to Send Email From a PHP Script Using SMTP Authentication

Get around the limitations of the PHP mail function With PEAR

A person sets up a PHP script to send mail using SMTP.

Hero Images / Getty

The PHP mail() function is simple, easy to use, and it works great, but it's also extremely inflexible. If you want to specify an SMTP server for the script to use, or use SMTP authentication, you're out of luck. To overcome these draconian limitations, and send email from your PHP script using SMTP authentication, you'll need to turn to a PHP class like PHPmailer or the PEAR Mail package.

Sending Email With the PHP Mail Function

When you use the PHP mail() function, you end up sending email directly from your web server instead of your mail server. If you have a mail server through your web host, or even a mail server with a different host, it's usually better to send mail through that instead.

The problem is that the PHP mail() function doesn't provide any native way to send mail via SMTP. If you want to open up that functionality, you'll need to install an additional PHP class.

Here are some options that work:

  • PHPmailer
  • SwiftMailer
  • Zend_Mail
  • XpertMailer
  • PEAR Mail

We'll show you how to use PEAR Mail, but you can use any class that supports SMTP.

If your web host already has one or more of these classes installed, they probably have tutorial materials that pertain specifically to your situation. In that case, go ahead and use the class that you have access to.

Only use this method if you're using PHP to create your own custom mail forms. If you're using a content management system (CMS) like WordPress, look for a plugin or built-in functionality to send mail via SMTP instead of trying to create your own.

How to Use PEAR to Send Mail Via SMTP

  1. Make sure that your domain is pointed at the Mail Exchange (MX) records of your mail server host, and make note of your mail server name. For example, it may be mail.yourdomain.net, or smtp.yourdomain.net.

  2. Check to see if PEAR Mail is already installed on your mail server.

  3. If PEAR Mail isn't already installed, consult with your web mail host for specific instructions for installing it.

  4. Once PEAR Mail is installed, modify one of the example PHP files in the following sections to fit your needs.

Example PEAR Mail PHP Script For SMTP Mail

You can create your own script from scratch if you like, or modify the following example to your liking. Make sure to enter your web mail server name in the host variable, and use your login information for your web mail host in the username and password fields.

<?php
require_once "Mail.php";
$from = "Sender Name <sender.name@sendersdomain.com>";
$to = "Recipient Name <recipient.name@recipientsdomain.com>";
$subject = " Subject Line Here: ";
$body = " any message you want ";
$host = "yourmailhost.com";
$username = "your username or email";
$password = "your password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>

Example PEAR Mail PHP Script For SMTP Authentication and SSL Encryption

If you want to use SMTP authentication and SSL encryption, you'll have to make a few modifications to the previous example. You'll need to point the host variable to your SSL mail server, and also specify a port number like 25, 465, 587, 2525 or 8025. Contact your web mail host for more information if you can't figure out which port to use.

<?php
require_once "Mail.php";
$from = "Sender Name <sender.name@sendersdomain.com>";
$to = "Recipient Name <recipient.name@recipientsdomain.com>";
$subject = " Subject Line Here: ";
$body = " any message you want ";
$host = "ssl://yourmailhost.com";
$port = "587";
$username = "your username or email";
$password = "your password";
$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>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>