- Using PHP to send e-mail messages
- Method #1: Using the PHP mail() function
- Method #2: Using the PEAR Mail class
- Method #3: Using PHPMailer
- Article Details
- How to send Emails in PHP?
- PHP built-in mail function ()
- Syntax and parameters
- Sending HTML email using PHP mail() function
- Simple Transmission Protocol (SMTP)
- PHP mailing packages
Using PHP to send e-mail messages
This article discusses several methods for sending e-mail messages from PHP scripts.
- Web hosting (Startup, Drive, Turbo Boost, or Turbo Max)
- Reseller hosting
- Managed WordPress hosting
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:
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:
[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 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.
[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'] = ''; $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); ?>
- 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.
Article Details
How to send Emails in PHP?
PHP is one of the most popular web-development languages and a popular way to create dynamic web apps. In this article we’re going to help you painlessly configure the mail function in your application. So let us start! There are two basic ways of sending emails with PHP: a built-in mail function and external mail packages. To read the full article, check out Mailtrap’s blog: How to Send Emails in PHP?
PHP built-in mail function ()
- create simple HTML/text messages without attachments and images
- send emails via localhost and Xmapp
- include several recipients with “$to” parameter.
It is suitable for simple, mostly text-based notifications in your local environment. If you need to communicate with your app’s users, it is better to install an external mailer package.
If you are still committed to the PHP built-in mail function() and are ready to accept the challenge, let’s take a look at the basic code and its main parameters.
Syntax and parameters
The PHP mail syntax is pretty simple:
It uses the following parameters:
- “$to” = your message recipient(s). The email address format may be user@example.com or User user@example.com. In general, it needs to comply with RFC 2822.
- “$subject” = your message’s subject
- “$message” = the body of your message. Lines should be separated with a CRLF (\r\n). Each line should not exceed 70 characters.
- “[$headers]” = additional recipients of your message, which can be included in CC or BCC.
Note that headers are optional, except for the “from” header: it must be specified, otherwise, you will receive an error message like Warning: mail(): “sendmail_from” not set in php.ini or custom “From:” header missing.
You can use additional headers to change the mail “From” address and set the “Reply to” address.
For more details and additional parameters, refer to the PHP documentation.
Sending HTML email using PHP mail() function
The body of the message can be written in HTML. However, as we’ve mentioned above, it should be simple. In the PHP mail function(), the HTML part will look like this:
$message = ' Here are the cases requiring your review in December:
Case title Category Status Due date Case 1 Development pending Dec-20 Case 1 DevOps pending Dec-21
';
It’s important to remember that to send HTML mail, you need to set the Content-type header:
$headers[] = ‘MIME-Version: 1.0’;
$headers[] = ‘Content-type: text/html; charset=iso-8859-1’;
Simple Transmission Protocol (SMTP)
Where do I specify the SMTP settings? This is a fair question. Go to the PHP installation folder and configure them in the “php.ini” file. But this will only work for localhost or Xmapp like solutions because as we have already mentioned, PHP mail function does not support SMTP authentication and doesn’t allow sending messages via external servers.
There are some other, rather haphazard options but we won’t promote them here. Alternatively, we recommend using external PHP mail packages for sending emails via an external SMTP server.
PHP mailing packages
As we have already mentioned, the native PHP mail() function has limited functionality when it comes to mass sending. For example, it is not designed for creating engaging email templates that may boost your next campaign or sending a large volume of emails.
But since PHP is still one of the most popular programming languages, it also doesn’t lack resources for sending mass emails. We can highly recommend several plugins, such as Pear Mail and Swift Mailer
Pear Mail is a class that provides multiple interfaces for sending emails (which is stated in their documentation).
Here is what you can do with Pear Mail:
- create complex HTML/text messages with attachments and inlined images (with Mail_Mime class)
- send emails via PHP’s built-in mail() function, a sendmail program, or SMTP server
- send multiple emails from a queue (with Mail_Queue class).
Swift Mailer
Swift Mailer is another popular package for sending emails in PHP. It is feature-rich, well covered by documentation, and pretty straightforward in use.
Here is what you can do with Swift Mailer:
- create complex HTML/multipart templates
- add attachments and embed images
- send emails via authenticated SMTP, sendmail, Postfix, or your own transport
- use additional plugins.
Besides that, Swift Mailer offers enhanced security and handles large attachments and images with low memory usage.
And finally, PHPMailer, which is the classic and the most popular email sending library for PHP. It deserves a separate article and a tutorial. You will find it here.
Here is what you can do with PHPMailer:
- create complex HTML/multipart templates
- add attachments and embedded images
- send emails via authenticated SMTP.
PHPMailer is protected against header injection attacks and automatically validates emails.
In this article, we have described the basic PHP email sending principles, syntax, and parameters. Moreover, we have reviewed the main ways of sending emails with PHP: its built-in mail function and the most popular external mail packages. PHPMailer and Swift Mailer are standard libraries for PHP email sending today, and PEAR Mail is still widely used.
Choose your option according to your current needs and preferences and test your emails beforehand. For email experiments — create an account at Mailtrap, a fake SMTP server. It imitates a real SMTP server and traps your test email in the virtual inboxes. Have a try!
Check the full Sending emails with PHP article at Mailtrap.io to get more details on email packages and examples!