Write Review

The PHP mail () function is a built-in PHP feature that allows you to send email directly from a script. Depending on your website or web application, you might need this functionality to provide a better experience for your customers.

For instance, if you are running a subscription-based website, you might feel the need of sending verification messages and welcome messages via emails.  This is where PHP mail() function comes in. You can use it on your coding to send emails automatically without manual input from your side.

Another area where you can possibly use the power of PHP email function is in contact forms. When visitors enter a message from the contact page, you can use the feature to send a copy of the message to your email. This allows you to get the emails on time and reply as fast as possible.

PHP mail() function is enabled by default and you don’t have to tweak the php.ini file to set up the same. So in this guide, we will discuss the general syntax that you should use to invoke the function to send emails from a PHP script.


  • A web server e.g. Apache or Ngnix
  • PHP scripting language.
  • Email server such as SendMail or Postfix.

If you are on a shared hosting, the above packages are installed and enabled by default. However, you will need to install the software applications one by one if you are on a VPS plan.

For more information on these plans (providers, reviews, prices and features) - visit HostAdvice's VPS hosting reviews and Shared hosting reviews.

Step 1: PHP mail() Function Syntax

The basic syntax for PHP mail() function is:

mail (to, subject, message, headers, parameters);

The below table summarizes the meaning of each parameter:


No Field Mandatory/Optional Description Examples
1 to Mandatory This parameter indicates the receiver of the email info@example.com


2 subject Mandatory Contains the subject of the email.  New line characters are not allowed Meeting Schedule

Payment details

Account Activation

3 message Mandatory The actual body of the email goes here Dear customer, your account has been activated.
4 headers Optional Used to include additional email headers such as: From, Cc, Bcc. These values should be separated with ‘\r\n’ From: billing@example.com”. "\r\n”.”CC: accounting@example.com"
5 parameters Optional Here, you can pass additional flags to the email server.

This field is rarely used

$to = "customer@example.com";
$subject = "Account Activation";
$message = "Dear customer, your account has been activated.";
$headers = "From: billing@example.com" . "\r\n" .
"CC: accounting@example.com";

Remember to change the values depending on your needs. You can use your own email account (e.g.  Gmail) on the ‘to’  field when testing the script.

Once you have finished editing the file, save it on your website.

Step 2: Testing the Script

You can run the above PHP email script by visiting the URL on a browser. For instance


Replace ‘example.com’ with your domain name and ‘mail.php’ with the correct file path.

If everything was setup correctly, you should receive an email with your message.


That’s all when it comes to setting up and sending an email with PHP mail() function.  We have demonstrated sending emails right from a PHP script with a simple example. However, you can extend the script further depending on your needs. For instance, you can append it at the end of your contact form script to receive emails whenever a visitor sends a message from the contact page.


Check out these top 3 PHP hosting services:

Was this article helpful?