> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sending e-mail messages using PHP

> Discover how to send mail from PHP using methods like PEAR Mail Class and PHPMailer in this concise guide.

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

<Warning>
  **Important**

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

  * Shared web hosting

  * 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.
</Warning>

## 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 [nobody@example.com](mailto:nobody@example.com), 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 [recipient@example.com](mailto:recipient@example.com) with the intended message recipient, and[sender@example.com](mailto:sender@example.com) with the return e-mail address:

```
<?php
    mail("recipient@example.com",
        "This is the message subject",
        "This is the message body",
        "From: sender@example.com" . "\r\n" . "Content-Type: text/plain; charset=utf-8",
        "-fsender@example.com");
?>
```

<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](http://php.net/manual/en/function.mail.php).
</Note>

## 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](/docs/pear-packages).
   > 🚧 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 theme={null}
<?php
    require 'Mail.php';

    // Define basic e-mail parameters:
    $recipient = 'recipient@example.com';
    $headers['From'] = 'sender@example.com';
    $headers['Reply-to'] = 'sender@example.com';
    $headers['To'] = 'recipient@example.com';
    $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](http://pear.php.net/manual/en/package.mail.mail.php).
</Note>

## 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](/docs/sending-e-mail-with-phpmailer).

## Related articles

* [Viewing e-mail message headers](/docs/viewing-e-mail-message-headers)

* [PEAR packages](/docs/pear-packages)

* [PHP include paths](/docs/php-include-paths)

* [Sending e-mail with PHPMailer](/docs/sending-e-mail-with-phpmailer)

* [Using Git](/docs/using-git)

* [Using Git with Plesk](/docs/using-git-with-plesk)
