Home » PHP Mail Function – PHP Send Email Using mail() Function
Php

PHP Mail Function – PHP Send Email Using mail() Function

Are you looking for a technique to send an email in PHP? In this article, I will show you how you can send an email using PHP. Generally, you have to use the PHP Mail Function to do it. PHP Send Email Using mail() Function is the golden key to send emails very easily.

Let’s discuss the definition and syntax of mail() function and how we can implement in PHP projects. Just read this article till the end. I am sure you will get your solution in this tutorial.

Basically, this function uses the inbuilt SMTP service to perform an action. Another advantage is there is no need for a server connection.

PHP Mail Function – PHP Send Email Using mail() Function

PHP Mail Function - PHP Send Email Using mail() Function

First of all, I will show you how to write the PHP mail function inside your code.


<?php

// Receiver's Email
$to = "info@johnpatel.com";

// Subject Line
$subject = "Subject Test";

// Email body message
$txt = "This is testing!";

// PHP mail finction
mail($to,$subject,$txt,$headers);

?>

Definition:

PHP Send Email Using mail() Function will allow you to send emails directly from the coding/script.

Now you will think what do you mean directly? Directly means you don’t have to use any third party script to send email in PHP. PHP mail() function is inbuilt function of PHP programming language. It will connect to the server directly. Even more, no need to use SMTP details to use it. Because mail() function automatically uses Simple Mail Transmission Protocol (SMTP) to send Email PHP.

Syntax:


mail('to', 'your-subject', 'text-message', $headers, 'additional-parameters');

Now, let’s take deep information of all the parameters which are used in mail() function.

Parameters Description
to Email receiver’s Email address. Note: It’s a mendetory field.
your-subject It specifies the subject of the Email. Note: It’s a Required field and can not contain newline. You have to write subject in single line.
text-message It is also a required field. It defines the text message or body message of the email. You can write your text message in this parameter. (Must not exceed 70 characters in a single line) Even more, you can write HTML code in this field. But first of all, you must have to write HTML header code in mail function. And it is our next parameter.
$headers It is optional field. Mainly, it is use to define From, Cc, and Bcc. Also, it helps to allow html code in ‘text-message’. The additional headers is separated with a CRLF (\r\n)
additional-parameters It is also an optional field. You can write additional things in this field.

Above all, parameters are very important in mail() function. Without these parameters, you can’t send the email to anyone.

Now, Let’s take some examples to show you how to implement this function inside coding.

1. Send email in PHP with Extra Header

You can send email to two different person with following code snippet.


<?php
$to = "info@example.com";
$subject = "Test subject";
$txt = "This is test";
$headers = "From: noreply@example.com" . "\r\n" .
"CC: second-person@example.com";

mail($to,$subject,$txt,$headers);
?>

2. PHP Send Email with HTML Code

<?php
$to = "info@example.com";
$subject = "Test HTML Email";

$html_message = "
<html>
  <head>
    <title>Test HTML Email</title>
  </head>
<body>
  <p>This is testing HTML Email.</p>
  <table>
    <tr>
     <th>Name</th>
     <th>Age</th>
    </tr>
    <tr>
      <td>John</td>
      <td>27</td>
    </tr>
  </table>
</body>
</html>
";

// Must set content-type when sending HTML email. It allows HTML code.
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// Additional headers
$headers .= 'From: <noreply@example.com>' . "\r\n";
$headers .= 'Cc: second-person@example.com' . "\r\n";

mail($to, $subject, $html_message ,$headers);
?>

I hope you are now a clear vision of PHP Mail Function and how to send email using mail() function very easily. If you like this tutorial, please share it.