Sending e-mail messages using PHP

Learn how to set up mail messages from PHP scripts with this guide that includes instructions for several methods, including PEAR Mail Class and the PHPMailer.

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

🚧

Important

You cannot use external SMTP servers to send e-mail messages if you have one of the following hosting packages:

  • Web hosting (Startup, Drive, Turbo Boost, or Turbo Max)

  • Reseller hosting

  • Managed WordPress hosting

For these hosting packages, you must use hosting.com servers. Other hosting packages have fewer restrictions, and can use some external SMTP servers to send e-mail messages.

Method #1: Use the PHP mail() function

The easiest way to send an e-mail message in a PHP script is to use the built-in PHP mail() function. The PHP 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 hosting.com 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",
        "[email protected]");
?>

📘

Note

  • Although the built-in PHP 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 PHP mail() function, please visit http://php.net/manual/en/function.mail.php.

Method #2: Use the PEAR Mail class

Although the PEAR Mail class requires more configuration than the PHP 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.

    🚧

    Important

    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 server_name, 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'] = 'ssl://server_name';
    $smtp_params['port'] = '465';
    $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);
?>

📘

Note

  • 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: Use 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 information about how to install PHPMailer, please see this article.

Related Articles