Send Form Data Through Email Using PHPMailer in PHP

In this article, we going to see how to send form data through email using PHPMailer in PHP. PHPMailer helps us to send and manage emails without Mail Server in our Hosting. This is the proper way to send and receive emails, also it protects from spam email through SMTP configuration.
This allows you to configure all types of SMTP provides eg. Mailgun, Gmail SMTP, or any other provides which you like to use.
Below a few lines of code help you to integrate easily with your project. When you click on the Submit button then the vales are pass through mail.php file.
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>Php Mail</title> </head> <body> <form action="mail.php" method="post"> First Name:<br><br/> <input type="text" name="fname"><br><br/> Last name:<br><br/> <input type="text" name="lname"><br><br> Email ID:<br><br/> <input type="text" name="email"><br><br> Mobile No:<br><br/> <input type="text" name="mobile"><br><br> <input type="submit" value="Submit"> </form> </body> </html> |
In the mail.php you need to configure your SMTP provider details below you have example.
- $mail->Host
- $mail->Port
- $mail->SMTPSecure
- $mail->Username
- $mail->Password
- $mail->addAddress(‘[email protected]’, ‘name’);
Then with the help of the PHPMailer library, we going to send the form data via email.
Note: You need to include PHPMailerAutoload.php from PHPMailer library
mail.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<?php // require_once 'mailerClass/class.php'; require_once 'mailerClass/PHPMailerAutoload.php'; $mail = new PHPMailer(); //Tell PHPMailer to use SMTP $mail->isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 0; //Ask for HTML-friendly debug output $mail->Debugoutput = 'html'; //Set the hostname of the mail server $mail->Host = 'smtp.gmail.com'; //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission $mail->Port = 587; //Set the encryption system to use - ssl (deprecated) or tls $mail->SMTPSecure = 'tls'; //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication - use full email address for gmail //Password to use for SMTP authentication $mail->Password = "youremailpassword"; //Set who the message is to be sent from //Set an alternative reply-to address //$mail->addReplyTo('[email protected]', 'First Last'); //Set who the message is to be sent to //Set the subject line $mail->Subject = 'Php Mail Check'; //Read an HTML message body from an external file, convert referenced images to embedded, //convert HTML into a basic plain-text alternative body //$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); $mail->Body = "First Name: <b>\"" . $_POST['fname'] . " <br> Last Name: " . $_POST['lname'] . "<br> Email ID : " . $_POST['email'] . " <br> Phone No: " . $_POST['mobile'] . "\"</b>"; //Replace the plain text body with one created manually $mail->AltBody = 'This is a plain-text message body'; $mail->isHTML(true); //Attach an image file //$mail->addAttachment('images/phpmailer_mini.png'); //send the message, check for errors if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } header("Location: index.php"); ?> |
Conclusion:
I hope this article will help you to send form data through email using PHPMailer in PHP. If you have any doubts about this topic let me know in the comment section. If you like this article share it with your friends.
Download
PHPMailerSend EmailSend Form Data Through EmailSMTP
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]