Knowledge Base

How to send e-mail messages using PHP

This article discusses several methods for sending e-mail messages from PHP scripts.

You cannot use external SMTP servers to send e-mail messages if you have one of the following hosting packages:
  • Web hosting (Lite, Swift, or Turbo)
  • Reseller hosting
For these hosting packages, you must use A2 Hosting servers. All other hosting packages are not subject to this restriction, and can use external SMTP servers to send e-mail messages.

Method #1: Using the mail() function

The easiest way to send an e-mail message in a PHP script is to use the built-in mail() function. The mail() function has several required parameters:

  • Message recipient
  • Message subject
  • Message body

Additionally, there are two optional parameters that you should use to set the “From” e-mail address. It is important to set the “From” address correctly so replies, bounce notifications, and other messages return to the appropriate address. If the “From” address is not set, messages sent from your web site display a return address of [email protected], where example.com represents the A2 Hosting server where your web site is hosted.

The following sample code demonstrates how to send an e-mail message with the “From” address set correctly. It also demonstrates how to set the Content-Type header so international (Unicode) characters are displayed correctly. In your own code, replace [email protected] with the intended message recipient, and [email protected] with the return e-mail address:

<?php
    mail("[email protected]",
        "This is the message subject",
        "This is the message body",
        "From: [email protected]" . "\r\n" . "Content-Type: text/plain; charset=utf-8",
        "-f[email protected]");
?>
  • Although the built-in mail() function is easy to use, it has some limitations. For example, it does not support SMTP authentication. SMTP authentication enables you to set the envelope headers correctly, which can help prevent mail servers from marking your messages as spam. See the following methods for ways to send messages from PHP using SMTP authentication.
  • To view the official documentation for the mail() function, please visit http://php.net/manual/en/function.mail.php.

Method #2: Using the PEAR Mail class

Although the PEAR Mail class requires more configuration than the mail() function, it is also much more powerful. With the PEAR Mail class, you can send e-mail messages using SMTP authentication, and specify many other e-mail settings. To do this, follow these steps:

  1. Install and configure the PEAR Mail package on your account. For information about how to do this, please see this article.
    Remember that after you install the PEAR Mail package using cPanel, you must configure the include_path setting in a custom php.ini file.
  2. The following code snippet demonstrates one way to send an e-mail message using the PEAR Mail class. You will have to modify the settings for your own application, particularly the username and password SMTP authentication parameters:
    <?php
        require 'Mail.php';
    
        // Define basic e-mail parameters:
        $recipient = '[email protected]';
        $headers['From'] = '[email protected]';
        $headers['Reply-to'] = '[email protected]';
        $headers['To'] = '[email protected]';
        $headers['Subject'] = 'This is the message subject';
        $headers['Date'] = date('r');
        $headers['Message-Id'] = '<' . uniqid() . '@example.com>';
        $headers['Content-Type'] = 'text/plain; charset=utf-8';
        $body = 'This is the message body';
    
        // Define SMTP authentication parameters:
        $smtp_params['host'] = 'localhost';
        $smtp_params['auth'] = true;
        $smtp_params['username'] = 'username';
        $smtp_params['password'] = 'password';
    
        // Create a Mail class instance with the above parameters, and then send the message:
        $message =& Mail::factory('smtp', $smtp_params);
        $message->send($recipient, $headers, $body);
    ?>
  • In the code example above, the Content-Type header is set so Unicode characters display correctly. You can change this header value to any other character set that you want.
  • To view the official PEAR Mail documentation, please visit http://pear.php.net/manual/en/package.mail.mail.php.

Method #3: Using PHPMailer

PHPMailer is a popular e-mail implementation that provides another way to send messages from PHP scripts. It supports many features, including SMTP authentication, and makes formatting e-mail messages correctly much easier.

For more information about PHPMailer, please visit http://phpmailer.worxware.com and https://github.com/PHPMailer/PHPMailer.