How to Setup the PHP mail() Function

How to Setup the PHP mail() Function

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.

Prerequisites

  • 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:

NoFieldMandatory/OptionalDescriptionExamples
1toMandatoryThis parameter indicates the receiver of the emailinfo@example.com

sales@example.com

2subjectMandatoryContains the subject of the email. New line characters are not allowedMeeting Schedule

Payment details

Account Activation

3messageMandatoryThe actual body of the email goes hereDear customer, your account has been activated.
4headersOptionalUsed to include additional email headers such as: From, Cc, Bcc. These values should be separated with ‘rn’From: billing@example.com”. “rn”.”CC: accounting@example.com”
5parametersOptionalHere, you can pass additional flags to the email server.

This field is rarely used

‘-fbilling@example.com’
<?php
$to = "customer@example.com";
$subject = "Account Activation";
$message = "Dear customer, your account has been activated.";
$headers = "From: billing@example.com" . "rn" .
"CC: accounting@example.com";
mail($to,$subject,$message,$headers);
?>

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

www.example.com/mail.php

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.

Conclusion

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:

FastComet
$1.79 /mo
Starting price
Visit FastComet
Rating based on expert review
  • User Friendly
    4.7
  • Support
    5.0
  • Features
    4.8
  • Reliability
    4.5
  • Pricing
    5.0
IONOS
$1.00 /mo
Starting price
Visit IONOS
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.3
Hostinger
$2.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7

How to Install the Roundcube Mail Client using the cPanel Hosting Control Panel

This article will show users how to install an email client that can be accessed
3 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Configure PHP Versions and Options on cPanel

This guide will show new webmasters about switching to different PHP versions an
2 min read
Avi Ilinsky
Avi Ilinsky
Hosting Expert

How To Install Apache, MySQL & PHP on an Ubuntu 18.04 VPS or Dedicated Server

This article shows users how to setup PHP, Apache web server and MySQL database
4 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Encrypt Email Using cPanel

This article will help you configure email encryption on your cPanel web hosting
2 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top