How To Send Email Using PHP
Shows how to send an email using PHP either directly using the email server installed on the system or via SMTP using other email services.
Email is one of the most important factors in having effective business communication. One can also use emails to host marketing campaigns to reach the appropriate audience. Apart from these uses, there are several other benefits of using email as the communication channel including keeping a record of all the messages.
In this tutorial, we will discuss the parameters involved in sending an email and the possible ways to send an email using PHP.
Basics of Email
This section explains the basics of sending an email by explaining the parameters involved in an email. We can include the below-listed parameters while sending an email.
From — The mandatory field to specify the sender email address. It must be a valid email address to send an email.
Reply-To — It’s an optional parameter to accept email replies. If not specified, the From email address will be used to send replies.
To — It’s the most important and mandatory parameter to send an email. We can include either single or multiple receiver email addresses to send an email either to single or multiple receivers.
// To - Format - RFC 2822 - Single Receipient
Receipient Name
// OR
Receipient Email
// To - Format - RFC 2822 - Multiple Receipient
Receipient 1 Name , Receipient 2 Name
, Receipient 3 Name
// OR
Receipient 1 Email, Receipient 2 Email
// OR
Receipient 1 Name , Receipient 2 Email
Cc — Carbon Copy — Similar to To, we can specify either single or multiple recipients to receive a copy of the email in order to keep them notified about the communication.
Bcc — Blank Carbon Copy — We can also involve recipients who will receive a copy of the email without mentioning them to the recipients involved in both To and CC list. The recipients involved in both To and CC list will never know that the same email is also sent to the recipients mentioned in the BCC parameter.
Subject — Though it’s not a mandatory field, one must include the subject in order to hint the recipients about the content involved in the email. A short and descriptive subject can tell the recipient about the communication context.
The subject can be of multiple lines separated with a CRLF (\r\n). Each line must not have more than 70 characters.
Message — The actual message to be sent to the recipients mentioned in To, CC, and BCC parameters. Though we can send an email without a message, it’s not a preferred way to send an email.
Send Email using mail() function
We can use the PHP function mail() to send an email as shown below. It expects that the appropriate Mail Server is installed on the system listening on port 25 to send the email or PHP is configured to use an SMTP server.
Also, make sure that the fields are validated and sanitized before passing them to the mail() function to avoid errors.
// Mail Function
$from = "";
$to = "";
$subject = "";
$message = "";
$headers = "";
$parameters = null;
mail( $to, $subject, $message, $headers, $parameters );
// Example
$from = "nick.douglas@example.com";
$replyTo = "nick.douglas@example.com";
$to = "john.dick@example.com";
$cc = "ricky.singh@example.com";
$bcc = "roy.kumar@example.com";
$subject = "Hello Guest";
$message = "Thanks for joining us.";
$headers = [ "From: $from", "Reply-To: $replyTo", "Cc: $cc", "Bcc: $bcc" ];
mail( $to, $subject, $message, implode( '\r\n', $headers ) );
// OR - PHP 7.2.0 or greater
mail( $to, $subject, $message, $headers );
This is the easiest way to send an email using PHP.
The mail function will throw an error, in case it does not find a mail server or PHP is not configured to use an SMTP server.
In absence of email server or SMTP configuration, the error should look like the one as shown below.
PHP Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in E:\PracticePHP\email.php on line 11
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in E:\PracticePHP\email.php on line 11
PHP Stack trace:
PHP 1. () E:\PracticePHP\email.php:0
PHP 2. mail() E:\PracticePHP\email.php:11
Call Stack:
0.0078 398696 1. () E:\PracticePHP\email.php:0
0.0090 399464 2. mail() E:\PracticePHP\email.php:11
Done.
Configure PHP to use SMTP Server
In case the email server is not installed on the system or installed on a different system, you must configure PHP to use an SMTP server. The php.ini file must be updated to configure an SMTP server. You can add/modify the below-listed settings to configure the SMTP server.
; must have
SMTP =
smtp_port = 25
; optional — required in case server requires authentication
auth_username =
auth_password =
SMTP Host — Update the SMTP setting to specify the server host. It must be a valid server having a mail server installed on it.
SMTP Port — The default port to send mail via SMTP is 25. The ports 587(MSA) or 465(SMTP) can be used to send emails securely.
Auth Username — The username required to identify the valid user.
Auth Password — The password required to authenticate the user.
After applying the above-mentioned configurations, restart the webserver. If the provided details are correct, we can send emails using the mail() function of PHP with appropriate mail server configuration. In this way, we can send emails without installing our email server.
In case you are using the secure ports 587 or 465, you might get an error in case the OpenSSL module is not enabled for PHP. It can be done on Windows by following How To Configure PHP OpenSSL Module On Windows.
You also need to install and configure Sendmail on windows systems. You can follow How To Use Sendmail On Windows To Send Email Using PHP to configure Sendmail on Windows.
Standard Services Configuration
You can also use the standard email services including Gmail, Yahoo, and Outlook to send an email. Below listed are the configurations to be applied to your php.ini file to send emails using your Gmail, Yahoo, or Outlook as mentioned below.
; Gmail configurations
SMTP = smtp.gmail.com
; You can also specify port 587
smtp_port = 465
auth_username =
auth_password =
; Yahoo configurations
SMTP = smtp.mail.yahoo.com
smtp_port = 465
auth_username =
auth_password =
; Outlook configurations
SMTP = smtp.live.com
smtp_port = 465
auth_username =
auth_password =
You must take special care while sending emails using free accounts since there are limitations in sending emails using these services. These services might block your account in case you exceed the specified limit, hence avoid using personal email accounts.
Gmail limits to a maximum of 100 recipients at a time with a limit of 500 messages per day. Similarly, Yahoo also applies a limit of 100 recipients and 500 messages per day.
You might also need to keep the 2-step verification off if you’re using it.
Also, you can follow How To Use Sendmail On Windows To Send Email Using PHP to configure Sendmail on Windows.
Send Email using PHPMailer and SwiftMailer
We can also send emails using the standard libraries available in PHP. The most popular among these libraries are PHPMailer and SwiftMailer.
Summary
In this tutorial, we have discussed the parameters to be considered while sending an email. I have also mentioned the different ways to send an email with and without the SMTP server. You can follow the tutorials Send Email Using PHPMailer and Send Email Using Swift Mailer to send emails using these libraries.
PHP Mail Configuration
In the previous lesson, we used mail() to send mail in PHP. That lesson assumes that your PHP installation is configured for sending mail. If your system isn’t configured for sending mail, all is not lost — you can change the configuration.
The php.ini File
The php.ini file is where you configure your PHP installation. This is the file you need to edit in order to configure PHP to send mail.
You need to ensure that the php.ini file contains details of the mail server that should be used whenever your application sends mail.
To check/change your PHP mail configuration:
- Open your php.ini file (if you don’t know where this is, see below)
- Search for the line that reads [mail function]
- Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP.
- Save/close the php.ini file
- Restart your web server
Here’s an example of what the mail settings could look like when you first open the php.ini file:
If you’re using a UNIX based system, you will need to change the line that reads ;sendmail_path = . You will also need to remove the semicolon from the start of this line (semicolons indicate that the line is a comment). For example, sendmail_path = /usr/sbin/sendmail .
If you’re using a Windows system, you should change the line that reads SMTP = localhost to include your mail server (or your ISP’s mail server). You could leave it at localhost if you’re using your own local SMTP server. If you aren’t using your own local SMTP server, you will need to enter a mail server that you have access to (such as your ISP’s mail server). For example, SMTP = mail.earthlink.net .
You should also set a default «From» email address by changing the line that reads ;sendmail_from = [email protected] . For example, sendmail_from = [email protected] .
Don’t Know Your php.ini Location or sendmail_path Path?
If you don’t know where your php.ini is, or what your sendmail_path setting should be, read on.
Your php.ini may be located here: /private/etc/php.ini . And there’s a chance that your sendmail path will be at /usr/sbin/sendmail ). Having said this, each PHP installation is different, so you should check using phpinfo() .
Fortunately, this is easy to do.
phpinfo() is used to view your PHP configuration details. You can do this by creating a .php file with the following line on it: . When you run this in your browser, you will see a full list of PHP configuration variables. Simply search for the lines that contain php.ini and sendmail_path to see the values you need to use.