How to send mail using PHP mail()?

php mail image

PHP mail() is the built in PHP function used to send mails.It allows you to directly send emails using scripts.

syntax

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

The mail() accepts the following parameters.

  1. to – Specifies the receiver or receivers of the email.(Note:this parameter is required for php mail() to run.)
    example- abc@example.com
  2. subject-Specifies the subject of the email.(Note:this parameter is required and cannot contain new line character).
  3. message-Defines the message to be sent.Each line should be separated with CRLF(\r\n) .(Note:this parameter is required and lines should not exceed 70 characters).
  4. headers-Represent additional headers like from, CC and BCC(this parameter is optional).
  5. parameters -Can be used to pass additional parameters to send email program. To set the envelope sender address when using sendmail with the -f sendmail option. (this parameter is optional).
    example-mail(‘abc@example.com’, ‘test mail ‘,’this is a test mail’, ‘null’, ‘-f abc@example.com’);

Code to send simple mail with mail() using html form

first create a html form and save file as form.html or php

<!DOCTYPE html>
<html lang="en">
<head>
<title>Form to Send PHP Mail</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<style>
.frm {
    border: 3px solid grey;
    padding: 33px;
}
</style>
</head>
<body>
<div class="container">
	<div class="row">
    <div class="col-md-4 frm">
  <h2>Form to Send PHP Mail</h2>
  <form action="mail.php" method="post" >
    <div class="form-group">
      <label for="name">Name:</label>
      <input type="text" class="form-control" id="name" placeholder="Enter name" name="name">
    </div>
    <div class="form-group">
      <label for="mail">Email:</label>
      <input type="email" class="form-control" id="mail" placeholder="Enter Email" name="email">
    </div>
    <button type="submit" name ="submit" class="btn btn-default">Submit</button>
  </form>
</div></div></div>

</body>
</html>


now create another file as mail.php to get the submitted data

<?php
$name = $_POST['name'];//store value of name input field on post
$email =$_POST['email'];//store value of email input field on post

$to = "abc@mail.com";//receiver mail address
$subject ="my subject";//subject of the mail
$message ="php test mail";

mail($to, $subject, $message) or die("Error!");;
echo "<script>alert('Thank You! Request Submited  Successfully.');</script>";
?>

Send mail with extra parameters

To send email with extra header we will use the header parameters of the mail function in mail.php file

<?php
$name = $_POST['name'];//store value of name input field on post
$email =$_POST['email'];//store value of email input field on post

$to = "abc@mail.com";//receiver mail address
$subject ="my subject";//subject of the mail
$message ="php test mail";
$headers = 'From: xyz@mail.com' . "\r\n" . //sender mail address
    'Reply-To: xyz@email.com' . "\r\n" . //reply back to
    'Cc: pqrs@mail.com' . "\r\n";
mail($to, $subject, $message, $headers);
?>

Leave a Reply

Your email address will not be published. Required fields are marked *