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.
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:
|1||to||Mandatory||This parameter indicates the receiver of the firstname.lastname@example.org|
|2||subject||Mandatory||Contains the subject of the email. New line characters are not allowed||Meeting Schedule
|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: email@example.com”. "\r\n”.”CC: firstname.lastname@example.org"|
|5||parameters||Optional||Here, you can pass additional flags to the email server.
This field is rarely used
<?php $to = "email@example.com"; $subject = "Account Activation"; $message = "Dear customer, your account has been activated."; $headers = "From: firstname.lastname@example.org" . "\r\n" . "CC: email@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
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.