How to send an email using PHP

Sending emails in PHP is safe and easy. Using the PHP mail function, we can easily send emails from the website forms. But sometimes we may need to mark and send emails with high importance and urgent priority.

But first, look at the php mail function

mail(to, subject, message, headers, parameters);

The php mail function requires three mandatory arguments - to, subject and message whereas two parameters - headers and parameters are optional.

How to set the priority in php mail function?

To send emails with high importance and urgent priority header, we need to add some extra PHP mail headers to set priority "high" / "urgent".

The types we can use either X-MSMail-Priority or X-Priority with the following values: 1 = High, 3 = Normal, 5 = Low

Similarly, we set importance header that accepts value High, Normal, or Low

For example, if you like to use X-MSMail-Priority it will be - X-MSMail-Priority: High or if you like to use X-Priority then X-Priority: 1

  • "X-Priority" (values: highest[1] to lowest[5]),
  • "X-MSMail-Priority" (values: High, Normal, or Low),
  • "Importance" (values: High, Normal, or Low).

You can find more details about message headers (mail and mimes) 

Let's get going.

First thing first. Let's create a form that when submitted, captures form data and send it to the email with a high priority and urgent header.

First, create a form

<title> PHP - Mail function for urgent and high priority </title>
<link href='style.css' rel='stylesheet' type='text/css'>
  <div class='contact-form'>
    <form action='send-mail.php' method='post'>
      <h2>Request call back.</h2>
      <input type='text' name='name' placeholder='Name' required>
      <input type='email' name='email' placeholder='Email' required>
      <input type='text' name='phone' placeholder='Phone'>
      <textarea rows='3' name='message' placeholder='message'></textarea>
      <div class='text-right'>
        <input type='submit' name='submit' value='Submit' class='btn'>

A little bit of CSS styling to make our form look good. 

Let's style our form

html {
  box-sizing: border-box;
*:after {
  box-sizing: inherit;
:root {
  --main-color: #e91e63;
body {
  background: #bbdefb1f;
.contact-form {
  max-width: 35rem;
  margin: 4rem auto;
  background: #fff;
  padding: 1rem 2rem 3rem;
  border-top: 0.75rem solid var(--main-color);
  box-shadow: 0.85rem 0.85rem 2rem #dfd1d6;
  border-radius: 0.75rem;
.contact-form h2 {
  color: #404040;
  font-size: 2em;
  margin: 0.85rem 0;
  text-align: center;
  border-bottom: 0.1rem solid var(--main-color);
  padding-bottom: 0.85rem;
.contact-form input:not(.btn),
.contact-form textarea {
  display: block;
  width: 100%;
  margin: 0.85rem auto;
  padding: 0.85rem;
  border-radius: 0.25rem;
  border: 0.1rem solid #ddd;
.contact-form input:focus,
.contact-form textarea:focus {
  font-style: italic;
  font-weight: bold;
.text-right {
  text-align: right;
.contact-form .btn {
  background: var(--main-color);
  color: #fff;
  padding: 0.55rem 1.5rem;
  border: 1px solid var(--main-color);
  border-radius: 1.5rem;
  font-size: 1.5rem;
.contact-form .btn:hover {
  background: #dd1559;


And this is JSFiddle demo for our basic form-


Now the most important part is processing the form with the php script to send an email with all the input data.

Send email using php mail function

$email_to=''; // web admin address - emails sent to this address
$sent_from=''; // must be the domain where the webpage/form is hosted.
$email_subject='New Enquiry'; // set email subject
$thankyou_page='/thank-you.php'; // redirect users to thank you page after submitting the form
  $name=$_POST['name']; // Name input by user in the form
  $email=$_POST['email']; // Email input by user in the form
  $message=$_POST['message']; // Message input by user in the form
  $phone=$_POST['phone']; // Phone no. input by user in the form
  // Creating the message for sending mail
  $msg="Message : 
  Name : ".$name."
  Email : ".$email."
  Phone no. : ".$phone."
  Message : ".$message;
  $headers="from: ".$sent_from."\n". //creating headers
  "reply-to: ".$email."\n". //creating headers
  "X-Priority: 1\n". //headers for priority
  "Priority: Urgent\n". //headers for priority
  "Importance: high"; //set importance
  if(mail($email_to,$email_subject,$msg,$headers)) {  // sending the email
     echo "<script>alert('Mail sent!');</script>"; // Show success alert
     echo '<script>javascript:location.href="'.$thankyou_page.'"</script>';  //Send the people to thank-you page.
  else {
     echo "<script>alert('Mail was not sent. Please try again later');</script>"; // Show error 

That is it.

The above code php code snippet will take all the form data and then send an email with high importance and urgent priority.