PHP Email Form

PHPz
Release: 2024-08-29 13:09:01
Original
1044 people have browsed it

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.

Syntax

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");
Copy after login
  • mail: mail is the PHP predefined function to send the email.
  • TO EMAIL: To email is the email address to whom we want to send the email.
  • EMAIL Subject: This email subject is the subject/header of the email.
  • EMAIL MESSAGE: We want to send this message to that specified email id.

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.

How Does PHP Email Form Work?

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:

  • Making a simple form with the required fields. We can use HTML, CSS, jQuery, and PHP to ensure we are all set with the form processing.
  • We can beautify the form as needed using HTML and CSS.
  • After the 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 to 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 that 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 examples of implementing 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>
Copy after login

Output:

PHP Email Form

As per the above code, we will receive 2 emails.

  • The first email goes to the email entered in the form itself.

PHP Email Form

  • And the 2nd will goes to [email protected]

PHP Email Form

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);
}
?>
Copy after login

Output:

PHP Email Form

Conclusion

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!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template