html form for send a mail

Send an email through HTML Form using PHPMailer in PHP

Fist of All we will create a HTML Form

[html]
<form role="form" action="sendmail.php" method="post">
<input name="name" required="required" type="text" placeholder="Enter Your Name" />
<input name="email" required="required" type="email" placeholder="Email ID"/>
<input name="mobile" required="required" type="text" placeholder="Enter Mobile No"/>
<textarea name="message" placeholder="Enter Any Message"></textarea>
<input name="submit" type="submit" value="Send a Mail" /></form>
[/html]


Preview will be look like this

html form for send a mail

Now we will create a file sendmail.php for send a mail in PHP

[php]
<?php

require ("class.phpmailer.php");

if(isset($_POST[‘submit’])){
$name=$_POST[‘name’]; // Get Name value from HTML Form
$mobile=$_POST[‘mobile’]; // Get Mobile No
$email=$_POST[’email’]; // Get Email Value
$message=$_POST[‘message’]; // Get Message Value

$mail = new PHPMailer();

$mail->IsSMTP();
$mail->Host = "websapex.com"; // Your Domain Name

$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Username = "info@websapex.com"; // Your Email ID
$mail->Password = "DT~-RQyJlaFV"; // Password of your email id

$mail->From = "info@websapex.com";
$mail->FromName = "WebsApex Team";
$mail->AddAddress ("info@websapex.com"); // On which email id you want to get the message
$mail->AddCC ($email);

$mail->IsHTML(true);

$mail->Subject = "Enquiry from Website submitted by $name"; // This is your subject

// HTML Message Starts here

$mail->Body = "
<html>
<body>
<table style=’width:600px;’>
<tbody>
<tr>
<td style=’width:150px’><strong>Name: </strong></td>
<td style=’width:400px’>$name</td>
</tr>
<tr>
<td style=’width:150px’><strong>Email ID: </strong></td>
<td style=’width:400px’>$email</td>
</tr>
<tr>
<td style=’width:150px’><strong>Mobile No: </strong></td>
<td style=’width:400px’>$mobile</td>
</tr>
<tr>
<td style=’width:150px’><strong>Message: </strong></td>
<td style=’width:400px’>$message</td>
</tr>
</tbody>
</table>
</body>
</html>
";
// HTML Message Ends here

if(!$mail->Send()) {
// Message if mail has been sent
echo "<script>
alert(‘Submission failed.’);
</script>";
}
else {
// Message if mail has been not sent
echo "<script>
alert(‘Email has been sent successfully.’);
</script>";
}

}
?>
[/php]


Now you are ready to send a mail through HTML form in PHP by using PHPMailer.


For Download the PHPMailer Function Click here [ult_buttons btn_title=”Download PHPMailer Function” btn_link=”url:https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F1UWrOmlrw1D4knOZHSg4Y7OJ773yaXR8c%2Fview||target:%20_blank|” btn_align=”ubtn-inline” btn_size=”ubtn-small” btn_title_color=”#ffffff” btn_bg_color=”#1e73be” btn_hover=”ubtn-fade-bg” btn_bg_color_hover=”#000000″ btn_title_color_hover=”#ffffff” icon_size=”32″ btn_icon_pos=”ubtn-sep-icon-at-left”] and extract the files in root folder.

 

38 Comments

  1. sandeep saini
    September 12, 2018

    code ko live site par kaise apply krna hai… plz help me

    • admin
      February 4, 2019

      Hello Sandeep,

      This code is for live site. You will have to just check the port and SMTP enable with your hosting provider. And change the value on Line: 17 and 18.
      $mail->SMTPAuth = true;
      $mail->Port = 587;

      Please Enter your Username and Password on Line: 19 and 20
      $mail->Username = “info@websapex.com”; // Your Email ID
      $mail->Password = “DT~-RQyJlaFV”;

  2. suman
    November 26, 2018

    sir this code is not working in my case

    • admin
      February 4, 2019

      Hello Suman,

      Please check the port and SMTP enable with your hosting provider. And change the value on Line: 17 and 18.
      $mail->SMTPAuth = true;
      $mail->Port = 587;

  3. qchecksupport
    January 14, 2019

    Works great but I need two additional functions:
    1. How do you do a redirect to another web page in the same domain upon submit
    2. How to upload data from the form in csv format to folder on the server

    • admin
      February 4, 2019

      Hello QcheckSupport,

      1. Please check the Line No. 62 – if(!$mail->Send()) { Here You can write the code for redirecting either through JavaScript or PHP using header
      2. I will publish the tutorial For uploading the file through php form soon. Please Subscribe our YouTube Channel. So, when I will update you will get noified.

  4. Rohit
    March 31, 2019

    Submission failed. after submitted the form

  5. Mihir
    June 4, 2019

    Code is not working in Localhost

    • admin
      June 19, 2019

      Hello Mihir,

      It is working on server as well on localhost. Please check once with configuration or email id password.

  6. Chris
    June 13, 2019

    Thaaaank youuu!
    I’ve been dealing with a php mailer contact form all day, couldn’t get the Reply-To address to match the one entered by the user (instead, it kept replying to my own admin mail address). I just couldn’t figure out which cryptic piece of code to put in which field, but the $_POST[’email’]; bit finally worked.

  7. Edward Muss
    June 17, 2019

    For me it worked after adding

    $mail->SMTPSecure = “tls”;

    • Thanh
      October 14, 2020

      metoo but i have error :Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in C:\xampp\htdocs\3iMBQO_IWcY\class.phpmailer.php on line 1918

  8. Maisarah
    June 24, 2019

    Hi Admin,

    I have an error on HTML page. When I clicks on the “Submit” button, the system will reload user to prompt window either to save or open the “sendmail.php” file

    • admin
      July 9, 2019

      Please check action in form. Have you put action=”sendmail.php” or it’s just blank. If you want to submit the form and PHP code on another page then you will have to write that file name in action.

  9. Toshia Nitz
    September 13, 2019

    This really answered my problem, thank you!

  10. clovis
    November 28, 2019

    hey code are running correctly but when i submit it gives me the alert of “Submission failed” but there no mail i’m receiving

    • admin
      November 28, 2019

      You are not getting mail because some error and you can check in alert it showing “Submission failed” not “Successfully Message Sent”. So, please SMTP setting of your mail server.

  11. anirudha
    February 18, 2020

    Actually it’s working fine
    but when I took trial with emai id it emailing to user and host both
    please submit the solution

    • admin
      February 19, 2020

      Hello Anirudha,

      Please check Line: 24 – $mail->AddAddress (“info@websapex.com”);
      Enter email where you want to get email.

      Line: 25 – $mail->AddCC ($email);
      This is for copy same email to someone else. You can remove cc email if you don’t want to send email to user.

  12. Rajeev
    February 25, 2020

    Hello,

    I have copied the same code and done the required changed like I mentioned my Domain, email, password. After submitting the form, am getting a alert of “Submission failed”.

    Can you please assist me on this issue or let me know how can I print exact error.

    • admin
      February 25, 2020

      Hello Rajeev,

      for print error you can check through this variable under else condition

      $mail->ErrorInfo;

      • Rajeev
        February 26, 2020

        Hello,

        Thanks for your response:
        should the syntax like this:
        if(!$mail->Send()) {
        // Message if mail has been sent
        /* echo ”
        alert(‘Submission failed.’);
        “; */
        $mail->ErrorInfo;
        }
        else {
        // Message if mail has been not sent
        echo ”
        alert(‘Email has been sent successfully.’);
        “;
        }

        • admin
          February 26, 2020

          Yes, Right.

          • admin
            February 28, 2020

            Hello Rajeev,

            You are doing something wrong in Mail SMTP Setting.
            Please check carefully from Line 14th to 27th

            Still the problem same then comment 14th Line – $mail->IsSMTP();

            After this same issue is coming then contact your hosting provider for the same.

  13. sneha gadhave
    March 17, 2020

    I have used this code for a website but there is a spamming issue we are getting so many spam mails.

  14. Paige
    April 2, 2020

    Hi, I have followed your method perfectly, I have commented the line – $mail->IsSMTP(); and all my credentials are correct. I get the prompt saying “Email has been sent successfully”, however, I get no email in my email or junk. My email is a hotmail email and I am using this – $mail->Host = “ssl://smtp.live.com”; as the domain name. Please help, thanks.

    • admin
      April 7, 2020

      Line: 24 – $mail->AddAddress (“info@websapex.com”); // On which email id you want to get the message
      Line: 25 – $mail->AddCC ($email);

      On Line 24 enter email id on which you want to receive the mail
      On Line 25 enter another email id on which you want to receive the mail

  15. sham
    May 24, 2020

    Thanks a ton for the awesome code.
    I changed the port and smtp details and it worked like a charm

  16. Sourav
    June 9, 2020

    how to take multiple recipients email from db

    • admin
      June 9, 2020

      Hello Sourav,

      Write the code for fetch the data from database and start loop on Line: 25 ($mail->AddCC ($email);). How many email ids will be found through fetch query it will print multiple AddCC and sent multiple recipients at the same time.

  17. Samrat
    July 3, 2020

    the code is not working in live server its getting html 500 error

    • admin
      August 24, 2020

      Code is working fine. Something is missing in your code

  18. Tarun
    July 7, 2020

    I want to send message from Gmail id and get message Gmail id. please inform me. how it possible.

    • admin
      August 24, 2020

      You will have to enter gmail smtp details for authorize phpmailer to send mail from gmail.

      For getting mail on gmail just put gmail id in To on which email you want to get the mail.

  19. Hello
    July 24, 2020

    I cannot thank you enough for the post.Thanks Again. Great.

  20. Yasmine Africa
    September 22, 2020

    Cool blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog jump out. Please let me know where you got your design. Appreciate it

  21. Dion Lezer
    September 28, 2020

    Perfect just what I was searching for! .

Leave a Reply