Table of Contents
Syntax of PHP mail function
mail($from,$subject,$message,$headers);
mail() Parameters
- to : This is used to Receiver email id, or receivers email ids of the mail.
- subject : Subject of the email to be sent.
- message : Message to be sent.
- additional_headers : This will use to add extra headers (From, Cc, and Bcc).
Form Layout Design for Sending Mail
Form Layout Design Code
index.html
<html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <form action="mail.php" method = "POST"> <div class = "jumbotron"> <h1 align = "center">Send Mail Using PHP mail() Function</h1> <h4 align = "center">TechJunkGigs</h4> </div> <div class = "container"> <div class="row"> <div class="col-md-6"> <div class="form-group"> <input type="text" class="form-control" name="name" placeholder="first name" required=""> </div> </div> <div class="col-md-6"> <div class="form-group"> <input type="email" class="form-control" name="email" placeholder="Email" required=""> </div> </div> </div> <div class="form-group"> <textarea class="form-control" name="message" rows="8" placeholder="Message"></textarea> </div> <div class="center-content"> <input type="submit" name = "submit" value="SUBMIT NOW" class="btn btn-lg"> </div> </form> </body> <html>
I’m using bootstrap for layout design, you can just copy whole code and paste in your code and use it or go through W3School for understanding layout design through bootstrap
Sample Code To Send Mail Using PHP
mail.php
<?php if(isset($_POST['submit'])){ $to = "Jaxxxxxx@xxxx.com"; // this is your Email address $from = $_POST['email']; // this is the sender's Email address $first_name = $_POST['first_name']; $last_name = $_POST['email']; $subject = "Form submission"; $subject2 = "Copy of your form submission"; $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message']; $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message']; $headers = "From:" . $from; $headers2 = "From:" . $to; mail($to,$subject,$message,$headers); mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly."; header('location:index.html'); } ?>
in this mail.php file you will find two mail() function used, one for sending and other for receive the submission copy of mail.
I hope this tutorial helped you to learn sending mail through PHP using mail() function plugin. To get the latest news and updates follow us on twitter & facebook, subscribe to our YouTube channel. And If you have any query then please let us know by using comment form.
siddharth says
Hey,
Very informative article. I like the way you have explained it in detail.
Thank you! keep sharing such articles.
Jamaley Hussain says
Hi Siddharth,
I’m glad that you like it.
Thank you for stopping and commenting here.
Jamaley Hussain
Ya This is an amazing Post. I don’t have knowledge of Php more. Thanks for the codes. Appreciated
Hi Ravi,
It will be glad for me that you like the post.
Thanks for the stopping and commenting here.
Jamaley
hey,
very nice article thanks for sharing
Hi, Gurteg
Thanks for visiting and commenting here.
It’s nice article, lots of learning point you mentioned that is helpful to understand better. thanks for sharing helpful blog post.
Hi Jina,
Glad to know that you like the post. thanks for stopping and commenting here.
nice article thanks for sharing