Introduction to PHP Email Form
In this article, we will learn about the PHP Email Form. Communication plays a great role in any application. Event-based notification is very common in any online communication. There are various types of action based (event-based) communication as per the PHP language is concerned. PHP email is one of the mediums of the communication we can use in our application.
We can use the PHP email function is our PHP code file or the application as per the business requirements. This is one of the basic requirements. We can see the various open form in any website or application like – contact us form, signup form, login form, inquiry form is any application. Now, one the basis of one of the form submissions someone needs to notify. If we submit a contact us form, it should go in an email to the admin or any other person who will take care of theses contact us an email.
Syntax
There is nothing to deal with the standard PHP email form, it could be any sort of form with the email enabled facility. But yes, it will be working along with the form submission to notify its user and the admin. Sending an email using the PHP is simple enough and the syntax can be found very easily from any of the internet sources.
Here is the syntax for sending emails in PHP:
mail("TO EMAIL","EMAIL Subject","EMAIL MESSAGE");
- mail: mail is the PHP predefined function we can use to send the email.
- TO EMAIL: To email is the email address to whom we want to send the email.
- EMAIL Subject: This is an email subject is the subject/header of the email.
- EMAIL MESSAGE: This is the message itself we want to send to that specified email id.
The moment the user submits any specified form we can capture the details then we can use this email function to notify the end receiver.
There are various other ways of sending an email using PHP. There is a plugin named PHP MAILER. This PHP Mailer comes up with various added features in addition to the normal PHP email function. Using this PHP Mailer, we will have various other features we can use like Sender email address, CC address, BCC address, file as an attachment, the sender IP address, Hostname, etc.
How does PHP Email Form Works?
Since this article is about the form-driven email, we need to have a working form in PHP before applying the email notification on that. So, make things fully functional, we have to follow the below steps:
- Making a simple form with the required fields. We can use the HTML, CSS, jQuery, and PHP to make sure we are all sets with the form processing.
- We can beautify the form as per our need using the HTML and CSS.
- After form submission, we have to validate the fields then we can use the email function to send the email to the required email address. There will not be much imparted at the end-user. The moment the user submits the form, it will send the email after having the correct details and also user will be notified about the form has been submitted successfully.
We will see the details example in the example section of this article.
Examples to Implement PHP Email Form
Below are the examples to implement an Email Form in PHP:
Example #1
Code:
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<?php
if(isset($_POST['submit'])){
$name = $_POST['name']." ".$_POST['lname'];
$email = $_POST['email'];
$mobile_no = $_POST['mobile_no'];
$subject = "Form submission Received";
$subject2 = "Thank you for contacting us";
$message = "Name: ".$name ." <br>Email:". $email ." <br>Phone:" . $mobile_no. "<br> Message is here:<br>" . $_POST['message'];
$message2 = "Thank you for contacting us, one of our representatives will contact you soon!";
mail($email,$subject2,$message2);
mail('[email protected]',$subject,$message);
}
?>
<div class="container-fluid">
<div class="container inner">
<div class="col-lg-12"><h1>Contact Us</h1>
<div class="col-xl-6 col-lg-6 col-md-6 col-sm-6 mb-4 float-left">
<p>Need a helping hand? Please reach out to us using the below form: </p>
<div class="col-lg-12" style="padding:0;">
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-6 col-sm-6 mb-4 float-left">
<p id="error_msg" style="display: none;"></p>
<form class="contact-form" id="Contact_frm" autocomplete="off" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" name="name" placeholder="First name">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" name="lname" placeholder="Last name">
</div>
<div class="form-group">
<label>E-mail Address</label>
<input type="text" class="form-control" name="email" placeholder="E-mail">
</div>
<div class="form-group">
<label>Phone Number</label>
<input type="text" class="form-control" id="mobile_no" maxlength="10" name="mobile_no" placeholder="Phone number">
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" name="message" placeholder="Message"></textarea>
</div>
<button type="submit" name="submit" class="btn contact-btn">Submit</button>
</form>
</div>
</div>
</div>
</div>
</body>
<style>
.form-group{margin:10px; clear:both}
</style>
</html>
Output:
As per the above code, we will receive 2 emails.
- The first email goes to the email entered in the form itself.
- And the 2nd will goes to [email protected]
Example #2 – Setting up from Email Address
In the email function, we can set the from email as well. Let’s see the example code for the same. Here, focusing on the PHP part only the other HTML part remains the same as in the above example code.
Code:
<?php
if(isset($_POST['submit'])){
$name = $_POST['name']." ".$_POST['lname'];
$email = $_POST['email'];
$mobile_no = $_POST['mobile_no'];
$subject = "Form submission Received";
$subject2 = "Thank you for contacting us";
$message = "Name: ".$name . " <br>Email:" . $email . " <br>Phone:" . $mobile_no. "<br> Message is here:<br>" . $_POST['message'];
$message2 = "Thank you for contacting us, one of our representatives will contact you soon!";
mail($email,$subject2,$message2);
$headers = "From: " . "[email protected]" . "\r\n";
$headers .= "Reply-To: ". strip_tags($email) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail('[email protected]',$subject,$message,$headers);
}
?>
Output:
Conclusion
We have various way ways of using the PHP email. PHP Mailer is one of the most popular ones. PHP mailer is one of the highly recommended over the normal PHP email function. We should use the filter or sanitization feature available in PHP to validate the email address before sending the email to that specified email address. Various organizations prefer AMAZON Services to send the email over any other available medium as it always delivers the email in the inbox.
Recommended Articles
This is a guide to the PHP Email Form. Here we discuss the introduction, syntax, and working of Email Form in PHP along with different examples and code implementation. You can also go through our other related articles to learn more –