All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Send HTML Form Data To Email Using PHP

Last Updated : Mar 11, 2024

How To Send HTML Form Data To Email Using PHP

In this tutorial we will show you the solution of how to send HTML form data to email using PHP, by using phpmailer via php we can easily achieve result.

PHPMailer is a code library and used to send emails safely and easily via php code from a web server.

When sending emails directly by php code requires familiarity to SMTP standard protocol and related issues and vulnerability about email injection for spamming.

Step By Step Guide On How To Send HTML Form Data To Email Using PHP :-

Here we used concept of PHPMailer code package those available in web browsers.

Phpmailer concept when we need means from any authenticated gmail account we can send email. Phpmailer has three php files.

PHPMailer.php supports several way of sending email messages like mail(), sendmail, qmail and direct dispatch from SMTP servers.

SMTP.php supports Sendmail() or other configured mail script on the server. On a hosted server the SMTP settings would have already been set.

Exception.php handles any type of php exceptions when errors occur. Sendmail.php code also available we need to edit some detail (i.e) our SMTP server gmail address and password, here when submit button submitted by user all html form data collected for send as email.

<?php include 'sendemail.php'; ?>
<!DOCTYPE html>
    <title>html form data to email</title>
    <!--alert messages start-->
    <?php echo $alert; ?>
    <!--alert messages end-->
        <form class="contact" action="" method="post"><br><br>
          <input type="text" name="name" class="text-box" placeholder="Your Name" required><br><br>
          <input type="email" name="email" class="text-box" placeholder="Your Email" required><br><br>
          <textarea name="message" rows="5" placeholder="Your Message" required></textarea><br><br>
          <input type="submit" name="submit" class="send-btn" value="Send">
    <!--contact section end-->
    <script type="text/javascript">
      window.history.replaceState(null, null, window.location.href);


use PHPMailer\PHPMailer\PHPMailer;
require_once 'phpmailer/Exception.php';
require_once 'phpmailer/PHPMailer.php';
require_once 'phpmailer/SMTP.php';
$mail = new PHPMailer(true);
$alert = '';
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
    $mail->Host = '';
    $mail->SMTPAuth = true;
    $mail->Username = ''; // Gmail address which you want to use as SMTP server
    $mail->Password = 'youremailpassword'; // Gmail address Password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = '587';
    $mail->setFrom(''); // Gmail address which you used as SMTP server
    $mail->addAddress(''); // Email address where you want to receive emails (you can use any of your gmail address including the gmail address which you used as SMTP server)
    $mail->Subject = 'Message Received (Contact Page)';
    $mail->Body = "<h3>Name : $name <br>Email: $email <br>Message : $message</h3>";
    $alert = '<div class="alert-success">
                 <span>Message Sent! Thank you for contacting us.</span>
  } catch (Exception $e){
    $alert = '<div class="alert-error">
  1. <?php include 'sendemail.php'; ?> wherever you want to include block of code we can just include the file by ‘include()’ or ‘require()’ statements. In php file we can easily embed HTML code to format them and make them more user-friendly.
  2. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in.
  3. The<html> tag is used to indicate the beginning of HTML document.
  4. As above shown <head> tag contain information about webpage and if need any external file those links are declared here. <title> tag is used for set the webpage title.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If your not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. <?php echo $alert; ?> in php prints the alert message for user clarification by ‘echo’. Echo used for print the statement in php.
  8. <center> tag used for align the whole contents into center of webpage. Within <form> tag form elements are defined with ‘post’ method for collects user inputs from html form.
  9. <input> tag type contains information of user name, mail id and submittion. <textarea> tag used for more line of text description it contain user message.
  10. In <script> tag window.history contains browser session history, a list of all the pages visited in current frame or window. replaceState() method modifies the current history entry and replaced with parameters of state object and URL.
  11. After submittion user filled details in form is replaced with empty and redirect to same web page.
  12. In sendmail.php file ‘use’ means tells a class to inherit a trait and it gives an alias to a namespace. Require_once used to embed PHP code from another file. Here we embeds three files of PHPMailer.
  13. ‘$mail’ refers new PHPMailer object. Variable ‘$alert’ initialized with null. If(isset($_POST['submit'])) is checking whether submit is set or not by ‘if’ condition. If yes means it stores user inputs of name, mail, message by post method to respective variables.
  14. Within try block SMTP server details are collected those are pointed by ‘$mail’. Here we need to edit username, password, setFrom and addAddress by using our SMTP gmail account details.
  15. If the mail sent successfully success alert message will show on display or catch block throws the error.
  16. Notes: we need server, internet and in our gmail account turn on the ‘less secure’ option for achieve successful result.
  17. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

I hope this tutorial on how to send HTML form data to email using PHP helps you and the steps and method mentioned above are easy to follow and implement.

In conclusion now we are learn using PHPMailer we can send html form data to email. In PHPMailer package we can study more concepts and simplified to use depends on our need.

Today’s all website needs email facilities so we have to know how to handle.

Most of dynamic websites need to send type of ways like with attachments, with form details, with popups or documents(doc,xl,ppt).

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪