How to Send Email From a PHP Script Using SMTP Authentication

Get around the limitations of the PHP mail function With PEAR.

What to Know

  • PHP class options: PHPmailer, SwiftMailer, Zend_Mail, XpertMailer, PEAR Mail.
  • PEAR Mail: Note mail server name > check that PEAR Mail is installed > modify PHP file using examples given.

This article explains how to use SMTP authentication to send email with the PHP mail() function in PEAR Mail.

Sending Email With the PHP Mail Function

When you use the PHP mail() function, you end up sending email directly from your web server rather than 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 built-in 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.

A person sets up a PHP script to send mail using SMTP.
Hero Images / Getty

If your web host already has one or more of these classes installed, it probably has tutorials pertaining to your situation. If so, 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, rather than 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 is not installed, consult with your web mail host for specific instructions to install 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.

require_once "Mail.php";
$from = "Sender Name ";
$to = "Recipient Name ";
$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("

" . $mail->getMessage() . "

");
} else {
echo("

Message successfully sent!

");
}

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.

require_once "Mail.php";
$from = "Sender Name ";
$to = "Recipient Name ";
$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("

" . $mail->getMessage() . "

");
} else {
echo("

Message successfully sent!

");
}
Was this page helpful?