In this article, we will learn about the PHP Email Form. Communication plays a significant 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 communication mediums we can use in our application.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
We can use the PHP email function as our PHP code file or the application per business requirements. This is one of the basic requirements. We can see the various open form on any website or application, like – contact us form, signup form, login form, and inquiry form is any application. Someone needs to notify us based on one of the form submissions. 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 these contact us an email.
There is nothing to deal with the standard PHP email form; it could be any form with the email-enabled facility. But yes, it will work along with the form submission to notify its user and the admin. Sending an email using PHP is simple enough, and the syntax can be easily found from any internet source.
Here is the syntax for sending emails in PHP:
mail("TO EMAIL","EMAIL Subject","EMAIL MESSAGE");
When 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 multiple other features; we can use the Sender email address, CC address, BCC address, file as an attachment, the sender IP address, Hostname, etc.
Since this article is about form-driven email, we need to have a working form in PHP before applying the email notification on that. So, to make things fully functional, we have to follow the below steps:
We will see the details example in the example section of this article.
Below are examples of implementing an Email Form in PHP:
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.
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:
We have various way ways of using PHP email. PHP Mailer is one of the most popular ones. PHP mailer is 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 an email over any other available medium as it always delivers the email to the inbox.
The above is the detailed content of PHP Email Form. For more information, please follow other related articles on the PHP Chinese website!