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
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.
sandeep saini
code ko live site par kaise apply krna hai… plz help me
admin
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”;
suman
sir this code is not working in my case
admin
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;
qchecksupport
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
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.
Rohit
Submission failed. after submitted the form
Mihir
Code is not working in Localhost
admin
Hello Mihir,
It is working on server as well on localhost. Please check once with configuration or email id password.
Chris
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.
Edward Muss
For me it worked after adding
$mail->SMTPSecure = “tls”;
Thanh
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
Maisarah
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
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.
Toshia Nitz
This really answered my problem, thank you!
clovis
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
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.
anirudha
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
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.
Rajeev
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
Hello Rajeev,
for print error you can check through this variable under else condition
$mail->ErrorInfo;
Rajeev
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
Yes, Right.
Rajeev
Hello,
am getting the below error:
Mailer Error: SMTP Error: The following recipients failed: sunmoonaccessories@oymyacon.in, writeforrajeev@gmail.com
SMTP server error: relay not permitted
can you assists me to get this issue resolved.
admin
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.
sneha gadhave
I have used this code for a website but there is a spamming issue we are getting so many spam mails.
Paige
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
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
sham
Thanks a ton for the awesome code.
I changed the port and smtp details and it worked like a charm
Sourav
how to take multiple recipients email from db
admin
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.
Samrat
the code is not working in live server its getting html 500 error
admin
Code is working fine. Something is missing in your code
Tarun
I want to send message from Gmail id and get message Gmail id. please inform me. how it possible.
admin
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.
Hello
I cannot thank you enough for the post.Thanks Again. Great.
Yasmine Africa
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
Dion Lezer
Perfect just what I was searching for! .