Sending Emails Using PHP mail() Function

By Vijay

By Vijay

Image
I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated July 4, 2025
Edited by Kamila

Edited by Kamila

Image
Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

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

Sending Emails Using PHP

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.

ParameterData TypeCompulsory/OptionalDescription
1To String CompulsoryReceiver(s) of the email.
2Subject String CompulsoryThe subject of the email.
3Message String CompulsoryMessage to be sent. Each line should be separated with a \r or \n. Lines should not be over 70 characters.
4Headers Array or stringOptional*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.
5Parameters String OptionalAdditional 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).

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

READ MORE FROM THIS SERIES:



Leave a Comment