Using PHP to send e-mail messages
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. Other hosting packages have fewer restrictions, and can use some external SMTP servers to send e-mail messages.
Method #1: Using 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 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:
"This is the message subject",
"This is the message body",
"From: [email protected]" . "\r\n" . "Content-Type: text/plain; charset=utf-8",
- 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: Using 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:
- 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.
- 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:
// 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'] = 'server_name';
$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.
To learn to install PHPMailer with this article.
- Viewing e-mail message headers
You can view a message’s raw e-mail headers to help troubleshoot e-mail problems. Additionally, A2 Hosting support staff may ask you to send this information if you are experiencing certain types of e-mail issues. This article describes how to view the e-mail message headers using a variety of client applications.
- PEAR packages
PEAR packages are reusable PHP code libraries that you can use in your own projects. Learn how to install and configure them here.
- PHP include paths
There are several ways you can set the include path in PHP. Learn about them here.
- Sending e-mail with PHPMailer
Learn to send e-mail via SMTP with PHP and PHPMailer.
- Using Git
The popular Git revision control system is installed on A2 Hosting’s servers, and this article provides a brief introduction to using it.
- Using Git with Plesk
Plesk supports integration with the Git version control system. Learn how to manage your repositories in this article.