X

How to Send Mail Using PHP

How to Send Mail Using PHP, In this post we are going to learn how to send mail using mail() function through PHP. Mailing is very important functionality in any web application, Send mail through PHP is very easy using  mail()  function.mail() function is used to send mail from web server.You can send mail as a plain text or with html element.mail() function has features to send mail to multiple recipients using PHP email headers.You can add in mail header like From, Cc, and BCC  using PHP mail headers.

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.

 

Categories: HTML PHP
Jamaley Hussain: Hello, I am Jamaley. I graduated from Staffordshire University and have always been passionate about Computers, Technology, and Generative AI. Currently, I work as a Senior Data Scientist (AI/ML) and I’m also the founder of TechJunkGigs, a platform dedicated to helping programming students with tutorials on Machine Learning, Data Science, Python, LLM, RAG, Generative AI, and NLP. What started as a blog has now evolved into a valuable resource for students, and I'm committed to sharing knowledge to help them stay updated with industry trends
Related Post