In this post, you will learn to send Email in PHP Using PHPMailer | Gmail SMTP Phpmailer and how to Receive Emails from HTML forms using PHP.
What is PHP Mailer?
Before we dive into the details, let’s briefly introduce PHP Mailer. PHP Mailer is a robust PHP library that simplifies the process of sending emails through PHP scripts. It offers an intuitive and feature-rich interface for sending plain text and HTML emails, complete with attachments and more.
Steps – Send HTML Form via PHP Mailer
Let’s start with a detailed outline that will guide us to Send HTML Forms via PHP Mailer through this extensive guide. Here are the important steps to Send an HTML Form via PHP Mailer on HTML form submission:
- Download and install PHP Mailer from GitHub
- Setup your HTML Contact Form
- Include PHP Mailer in your HTML code
- Configure PHP Mailer
- Process form data to send email
- Compose the Email
- Handle Success and Error messages
- Send the Email
- Video Tutorial – Send HTML Form via PHP Mailer
- Now that we have our roadmap, let’s explore each section in detail to Send HTML Forms via PHP Mailer.
Step-by-Step Video Tutorial:
How to Send HTML Form to Email Using PHP Mailer
Sending HTML forms via email is a seamless process when you have PHP Mailer at your disposal. Here’s a step-by-step guide to achieving, how to send HTML form via PHP Mailer:
Prerequisites
Before diving into implementation, ensure that you have the following prerequisites in place:
A Web Server with PHP Support: You’ll need a web server, such as Apache or Google, with PHP support to run your PHP Mailer script.
Email Server Access: Ensure you have access to an email server or SMTP server to send emails, such as Gmail.
Basic HTML and PHP Knowledge: Familiarize yourself with HTML to create the form and PHP to process it.
Getting Started
Let’s start by downloading PHP Mailer and setting up our HTML form to Send HTML Form via PHP Mailer:
1. Download and install PHP Mailer
Begin by downloading the PHP Mailer library. You can obtain it from the official GitHub repository or use Composer if you prefer package management.
To Download the PHP Mailer manually from the GitHub library Click Here!
If you want to install the PHPMailer using Composer. First of all, simply install the Composer Setup on your computer and run the following command in your terminal:
composer require phpmailer/phpmailer
Alternatively, if you’re not using Composer, you can download PHPMailer as a zip file, and then copy the contents of the PHPMailer folder into one of the include_path directories specified in your PHP configuration and load each class file manually:
2. Set Up Your HTML Form
Create an HTML form on your website where users can submit their information. Ensure that your form fields have appropriate name attributes.
HTML Form Source Code
I have created a simple HTML form to send web form data to email using PHPMailer. I will collect this form data from the user and send it to my email address using PHPMailer.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
<style>
body{
background-color: aliceblue;}
.contact {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
transition: 1s;
}
.contact .input-box input,
textarea {
width: 100%;
height: 35px;
padding: 20px;
letter-spacing: 1px;
outline: none;
border: 1px solid rgba(0, 0, 0, 0.61);
/* border: 2px solid rgb(238, 224, 243); */
border-radius: 5px;
margin: 0;
margin-bottom: 10px;
box-shadow: 0 1px rgba(0, 0, 0, 0.233);
resize: none;
box-sizing: border-box;
}
.contact .input-box input:focus {
border: 1px solid rgb(0, 128, 255);
}
.contact .input-box textarea:focus {
border: 1px solid dodgerblue;
}
.contact .input-box textarea {
height: 100%;
}
::placeholder {
color: rgba(0, 0, 0, 0.521);
letter-spacing: 0;
font-size: 15px;
}
.contact .submit-btn input {
width: 100%;
height: 35px;
border: none;
/* background-color: rgba(17, 135, 253, 0.952); */
border: 1px solid rgb(0, 128, 255);
;
background-color: transparent;
border-radius: 3px;
color: rgb(0, 128, 255);
font-size: 20px;
font-family: serif;
padding: 0 8px;
box-shadow: 0 3px 3px rgba(0, 0, 0, 0.116);
border-radius: 5px;
outline: none;
cursor: pointer;
transition: 0.3s ease-in-out;
}
.contact .submit-btn input:hover {
background-color: rgb(0, 128, 255);
color: white;
}
h2 {
color: rgb(0, 128, 255);
text-align: center;
font-size: 30px;
}
</style>
</head>
<body>
<div class="contact">
<h2>Contact Now</h2>
<form action="mail.php" method="post">
<div class="input-box">
<input type="text" name="name" placeholder="Your name">
</div>
<div class="input-box">
<input type="email" name="email" placeholder="Email Address">
</div>
<div class="input-box">
<textarea name="message" cols="25" rows="7" placeholder="Type your message"></textarea>
</div>
<div class="submit-btn">
<input type="submit" name="send" value="Send Message">
</div>
</form>
</div>
</body>
</html>
3. Include PHP Mailer
In your PHP script, include the PHP Mailer library using require or require_once. as shown in the following script:
4. Configure PHP Mailer
Set up PHP Mailer with your email server details, including the SMTP host, username, password, and port. Make sure to use valid credentials and a secure connection.
If you are using a Gmail SMTP server, first of all, login to your Gmail ID, turn on 2-step authentication and, generate an App Password. To generate an App Password in your Gmail account follow this link.
5. Process Form Data
In your PHP script, retrieve the form data submitted by the user using $_POST or $_GET, depending on your form’s method attribute. In this tutorial, we have received the following form data from the user:
6. Compose the Email
Use PHP Mailer to compose your email. You can format it as plain text or HTML, depending on your preference. Here is the complete PHPMailer source code to send HTML form via PHP Mailer:
PHPMailer Source Code
Copy the below source code and change the data according to your preferences:
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//required files
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
//Create an instance; passing `true` enables exceptions
if (isset($_POST["send"])) {
$mail = new PHPMailer(true);
//Server settings
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'your email address'; //SMTP write your email
$mail->Password = 'app password'; //SMTP password
$mail->SMTPSecure = 'ssl'; //Enable implicit SSL encryption
$mail->Port = 465;
//Recipients
$mail->setFrom( $_POST["email"], $_POST["name"]); // Sender Email and name
$mail->addAddress('example@gmail.com'); //Add a recipient email
$mail->addReplyTo($_POST["email"], $_POST["name"]); // reply to sender email
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = $_POST["subject"]; // email subject headings
$mail->Body = $_POST["message"]; //email message
// Success sent message alert
$mail->send();
echo
"
<script>
alert('Message was sent successfully!');
document.location.href = 'index.php';
</script>
";
}
?>
7. Handle Success and Error messages
Implement error handling to deal with scenarios where the email-sending process might fail. Display a success message to the user upon successful submission.
8. Send the Email
Use the send method to send the email. Ensure to implement error handling to deal with scenarios where the email-sending process might fail.
9. Testing Your Setup
After implementing the above steps, thoroughly test your setup. Submit test data through your HTML form to ensure that emails are being sent correctly to the specified email address.
Conclusion
In conclusion, PHP Mailer is a powerful tool that simplifies the process of sending HTML forms to email addresses. By following the steps outlined in this guide, you can enhance user interaction on your website and efficiently collect user information, feedback, and inquiries. Remember to handle errors gracefully and ensure the security of user data during transmission.
Now, let’s address some frequently asked questions (FAQs) related to PHP Mailer and email functionality.
Top comments (0)