PHP is a popular way to create dynamic web apps and send transactional emails. Learn how you can send different types of emails using the PHP mail() function as well as with MailerSend’s email API. Show PHP is a modern and powerful open-source scripting language with a large, active community. It’s widely used in web development, including for the purpose of sending emails from websites and apps. There are several ways to send emails in PHP, but not all offer the same functionality or are optimized for sending best practices! This guide will teach you how to set up PHP and configure your server to send and receive emails. We'll also show you actual code examples for different use cases to help you get started. You'll be able to start sending emails in PHP from your web app in no time. Ready? Let's go! Why use PHP to send emailsPHP is a fast, reliable and flexible way to send emails from a web server, making it the ideal solution for transactional email. Additionally, using PHP to send emails allows you to take advantage of the many email libraries and integrations built around it. These libraries usually provide advanced solutions such as email validation, spam protection and email tracking, making it easier to manage your transactional email. Methods for sending emails in PHPThere are a few methods that you can use to send emails with PHP which can be divided into two categories: Using the built-in PHP mail() function and using email libraries. 1. Using the PHP mail() functionPHP's built-in mail() function is one of the simplest ways to send emails directly from the web server itself. It just takes three mandatory parameters: the email address, email subject and message body—and sends it to the recipient. In order to send emails using this function, you need to make sure that your web server is properly configured. This involves installing a mail server such as Sendmail, Postfix or Qmail, and setting it up to send emails from your website. If you use Linux or Unix, you can use the Sendmail or Qmail programs to send emails. If you use Windows, you can use the Sendmail program. Let’s take a look at how to configure Sendmail. You’ll first need to correctly configure the php.ini file with details of how you send email.
For Linux or Unix, you’ll need to set the sendmail_path parameter to point to the executable file so that PHP knows where to find the Sendmail application. It will look like this:
Notice the Windows parameters are left blank. Now you’re ready to start sending emails. But there is an easier and faster way to send emails in PHP with added functionality, which involves setting up an SMTP server. We’ll get to this later! PHP mail() function limitationsThe PHP mail function is the most basic way to send an email from PHP. Due to its limitations, it’s mostly suited to sending simple text-based notifications in local environments. Its biggest drawback is that, as emails are sent from your web server, your deliverability will be affected due to the potential for spam and blocklisting. There are a few reasons for this:
This is why for transactional email sending, a service like MailerSend is recommended. MailerSend’s SMTP server will enable you to overcome these issues, although we suggest using the email API to get additional functionality and core features, which we will discuss in more detail further along in this article. Further limitations include:
It doesn’t let you add file attachments. So embedding any images or files in an HTML email template will not be possible without an external mail package. You'll also need to rely on external libraries if you want to work from the localhost server Parameters and syntaxThe following code shows how to use the PHP mail function to send an email and its common parameters:
Here's what each parameter means:
Sending plain text emails using the PHP mail() functionTo send a plain text email with PHP via the mail() function, three of the above-mentioned parameters must be supplied. These are to, subject and message. Headers and parameters are optional. Here’s an example with the inclusion of the From and Reply-to headers:
Sending HTML emails using the PHP mail() functionYou can also use the mail() function in PHP to send HTML emails. However, in order to send an HTML message, you need to include the appropriate Content-type header in the email.
The following code shows how to send an HTML email with the function:
Sending emails with attachments using the PHP mail() functionAlthough the PHP mail() function is somewhat limited, it does allow you to send attachments using the MIME-version header. To do so, you’ll first need to define the Content-type header as multipart/mixed. Next, your text and attachments must be defined within a boundary. Here’s a simple code example to send an email with a single attachment:
Sending emails from a form using the PHP mail() functionIt’s common practice for businesses to include a contact form on their website. To create a form that uses the PHP mail() function to send an email when the form is submitted, you’ll first need to create the form. This step is pretty easy, and you can make your form as simple or as fancy as you like! Here’s an example of an HTML contact form:
The next step is to create the sendmail.php file that will send the form submission to an email address when someone completes and submits the contact form. Here is an example of a simple plain text email:
2. Using email librariesA more advanced, secure and modern way of sending emails in PHP is by using mail packages and integrations or by using email API. The most popular email packages are PHPMailer, Mailer and PEAR. Recommended: Send email in PHP using MailerSendSending emails in PHP using the basic methods can be challenging, especially if you want more functionality and custom options. MailerSend’s email API is the best solution for this. The email API is without a doubt the most powerful option. Not only have we made it easy to implement, but you can also call on every endpoint of the MailerSend app from your project and enjoy access to all features. Take advantage of , suppression list management, professionally designed responsive templates, and sender authentication using SPF, DKIM and DMARC security protocols. The official SDK supports PHP 7.4 and PHP 8 and uses PHP Standard Recommendations like HTTP Client and HTTP message interfaces. In order to get started with sending emails in PHP using MailerSend, we suggest you install the implementations first with the following command:
After that, you can install our official :
0 To help get you get the ball rolling, check out the following code for sending emails in PHP that you can copy and paste into a PHP file:
1 If you weren't planning to send HTML content that's OK! The code still works for text-only content.
2 Before you start emailing your recipients, don't forget to test your emails! Make sure to send test emails to your own internal addresses and double-check that the formatting is correct and that the server connection details are properly configured. The MailerSend PHP SDK also offers a variety of other methods you can use depending on your preferences. For instance, you can use powerful email templates, tags and variables that are available through the MailerSend email API. Below, you can find examples: Sending a template-based email, sending an email with an attachment, and personalizing emails using variables. Send a template-based email in PHP:
3 Send email in PHP with an attachment:
4 There are plenty of PHP code examples available at your disposal, including how to manage messages, bulk emails, recipients, webhooks, tokens and more. To have a complete view of these, please visit the official SDK for PHP page and get more details on how to use the email API. MailerSend’s powerful email API allows you to start sending emails quickly and securely. Using PHPMailerPHPMailer is the most popular library for sending emails from PHP applications. It provides a very easy-to-use interface and integrates with the SMTP server of your choice. The PHPMailer library lets you send emails from localhost with an SMTP server. Its functionality allows you to send both HTML emails and plain text emails. While this is a good solution, sending emails with SMTP is largely used in legacy apps so might have security issues plus, it doesn’t allow as many customization options as with an API. In order to use PHPMailer, you'll first need to install it using Composer. To do so, open up a terminal and run the following command:
5 Once PHPMailer is installed, you can start sending emails. The first step is to create a new instance of the PHPMailer class:
6 Next, you'll need to set up your email server settings. These settings include the SMTP server address, the SMTP port number, the username and password. You can find these settings on your email provider's website. For the purpose of this example, we'll be using MailerSend's SMTP server, which is a secure and reliable way to send emails. Once you have the server settings, set them in the PHPMailer class:
7 Once the settings are configured, you can send an email by calling the send() method:
8 This will send the email! If you want to send a HTML email, you can use the html() method:
9 Alternatively, you can add attachments to your email by using the addAttachment() method:
0 You can send emails with attachments by including the filename and the contents of the attachment in the send() method:
1 To add recipients to your email, you can use the addAddress() method:
2 This will add the email address "[email protected]" to the recipient list. You can also add multiple recipients by separating them with commas:
3 This will add the email addresses "[email protected]" and "[email protected]" to the recipient list. If you wanted to check for any potential message errors (we recommend you do!), instead of directly calling the send() method and sending the email, use the following condition in your script:
4 This will display an error message if your script fails to send and a success message if it made it through. Note: The PHPMailer library provides a variety of other methods that you can use to send emails. For more information, please refer to the PHPMailer documentation. You’re now ready to get started with sending emails in PHP! The right method will depend on your needs and preferences, but for advanced functionality and increased deliverability and security, MailerSend’s email API has your back! We’re curious, what will you use to send your PHP emails? Let us know in the comments. Ready to send your emails in PHP?MailerSend helps you integrate powerful email functionality to your web apps via SMTP or API. Join MailerSend for free today and get 3,000 emails/month. Editor's note: This article was originally published in 2021 and has been updated to include additional information and examples. |