A step-by-step guide on how to send emails in PHP using the PHP mail() function. Discover the best practices for effective email delivery in your PHP applications with examples:
In this tutorial, you will learn what the PHP mail function is, PHP mail parameters, examples of sending emails using PHP, securing emails, and frequently asked questions (FAQs) related to this topic.
Please note that we have used PHP version 7 in all examples.
Let’s begin!
=> Series of Simple PHP Tutorials
Table of Contents:
Sending Emails Using PHP

What is PHP mail() Function
The mail() function is used to send emails in PHP. It enables users to send emails directly from a script.
Syntax:
mail (to, subject, message, headers, parameters);
PHP Mail Parameters
The following table shows the parameters we pass to the mail() function. Some parameters are compulsory, while others are not.
| Parameter | Data Type | Compulsory/Optional | Description | |
|---|---|---|---|---|
| 1 | To | String | Compulsory | Receiver(s) of the email. |
| 2 | Subject | String | Compulsory | The subject of the email. |
| 3 | Message | String | Compulsory | Message to be sent. Each line should be separated with a \r or \n. Lines should not be over 70 characters. |
| 4 | Headers | Array or string | Optional* | Additional headers like From, Cc, and Bcc. They should be separated with a \r or \n. *The email must contain a From header when sending an email. |
| 5 | Parameters | String | Optional | Additional parameters. |
Examples of Sending Emails with PHP
Given below are some examples of sending emails using PHP. You can practice these examples by running the following programming code.
Plain Text Email
<?php
//message
$msg = "First line\nSecond Line";
//header
$header = "From: myuser@test.com";
//use the wordwrap() function if lines are over 70 characters
$msg = wordwrap($msg, 70, "\r\n");
//send email
$email = mail('user@test.com', 'My Subject', $msg, $header);
if($email == true){
echo "Message sent successfully!";
}else{
echo "Message send failed!";
}
?>
Output:
Message sent successfully!
Email with Extra Headers
<?php
$to = 'user@test.com';
$subject = 'My Subject';
$msg = 'Hello!';
$headers = 'From: myuser@test.com' . "\r\n" .
'Cc: anotheruser@test.com';
$email = mail($to, $subject, $msg, $headers);
if($email == true){
echo "Message sent successfully!";
}else{
echo "Message send failed!";
}
?>
Output:
Message sent successfully!
Email with Extra Headers as an Array (PHP 7.2.0 and above)
<?php
$to = 'user@test.com';
$subject = 'My Subject';
$msg = 'Hello!';
$headers = array(
'From' => 'myuser@test.com',
'Cc' => 'anotheruser@test.com'
);
$email = mail($to, $subject, $msg, $headers);
if($email == true){
echo "Message sent successfully!";
}else{
echo "Message send failed!";
}
?>
Output:
Message sent successfully!
HTML Email
<?php
//message
$msg = '
<html>
<head>
<title>My Title</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
';
$header = "From: sender@test.com \r\n";
//the content-type header must be set to send an HTML email
$header .= "Content-type: text/html";
$email = mail('receiver@test.com', 'My Subject', $msg, $header);
if($email == true){
echo "Message sent successfully!";
}else{
echo "Message send failed!";
}
?>
Output:
Message sent successfully!
Securing Emails
Emails can be intercepted by malicious users during the transmission process. Therefore, email transmission via Hypertext Transfer Protocol Secure (HTTPS) is recommended as it encrypts messages before sending.
Frequently Asked Questions
1. Can you send emails with PHP?
Yes, you can send emails with PHP.
2. What function is used to send emails in PHP?
The mail()function is used to send emails in PHP.
3. What is the syntax of the mail() function?
The following text shows the syntax of the mail() function.
mail (to, subject, message, headers, parameters);
4. Is PHP mail function safe?
Usually, it is considered safe if used with Hypertext Transfer Protocol Secure (HTTPS).
5. Show how to send an email in PHP using an example.
You can send an email in PHP as shown below:
<?php
$msg = “My message”;
$header = “From: sender@test.com”;
//send email
mail(‘receiver@test.com’, ‘My Subject’, $msg, $header);
?>
Conclusion
The mail() function is used to send emails in PHP. There are five email parameters: to, subject, message, additional headers, and additional parameters.
Some parameters are compulsory, while others are optional. It is recommended to send emails via Hypertext Transfer Protocol Secure (HTTPS).






