Introduction: Why Use PHP Mailer for Sending Emails?
When you need to send emails in PHP, you might want to use PHP Mailer for its flexibility and SMTP support. Unlike the basic mail()
function in PHP, PHP Mailer offers better control and security, including SMTP authentication, HTML messages, and more. In this guide, we’ll walk you through how to send mail using PHP Mailer, with code examples and a form styled using Bootstrap.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
Step 1: Download PHP Mailer
First, download PHP Mailer from this link. After downloading, extract the files into your project directory. You’ll use these files to set up your email functionality.
Step 2: HTML Contact Form
Let’s create a simple contact form using HTML and Bootstrap for styling:
<form class="contact-form" action="php/contact.php" method="post">
<div class="row">
<div class="col-sm-6 form-group name">
<input type="text" class="form-control" name="name" placeholder="Name">
</div>
<div class="col-sm-6 form-group email">
<input type="email" class="form-control" name="email" placeholder="Email">
</div>
</div>
<div class="form-group comment">
<textarea class="form-control" name="comment" placeholder="Message"></textarea>
</div>
<div class="button-box text-center">
<button type="submit" class="btn btn-default progress-button btn-submit">
<span class="button-label">Send</span>
</button>
</div>
</form>
Step 3: Adding jQuery AJAX to Handle Form Submission
Next, use jQuery and AJAX to handle the form submission and pass data to the PHP script:
$(document).ready(function () {
//Contact Form
function contactForm() {
$('.btn-submit').on('click', function (e) {
var $this = $(this);
e.preventDefault();
$.ajax({
url: 'php/contact.php',
type: 'POST',
data: $this.closest('.contact-form').serialize(),
success: function (data) {
if ($(data).is('.send-true')) {
$this.addClass('loading').delay(650).queue(function () {
$this.addClass('success').addClass('loaded').dequeue();
});
} else {
$this.addClass('success');
}
}
});
});
}
});
Step 4: PHP Code to Send Mail Using PHP Mailer
Here’s how you can send an email with PHP Mailer. First, make sure you include the necessary files and use the correct SMTP details:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comment'])) {
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
try {
//Server settings
// $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'sopu.me'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'saif@sopu.me'; //SMTP username
$mail->Password = '*******'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom($_POST['email'], 'Mailer');
$mail->addAddress('contact@rahadkabir.net', 'Joe User'); //Add a
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = $_POST['name'];
// $mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->Body = '<html>
<head>
<title>Your Site Contact Form</title>
</head>
<body>
<h3>Name: <span style="font-weight: normal;">' . $_POST['name'] . '</span></h3>
<h3>Email: <span style="font-weight: normal;">' . $_POST['email'] . '</span></h3>
<div>
<h3 style="margin-bottom: 5px;">Comment:</h3>
<div>' . $_POST['comment'] . '</div>
</div>
</body>
</html>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
} else {
echo '<span class="text-danger">All fields must be filled!</span>';
}
Why Use PHP Mailer Over Raw PHP?
- SMTP Authentication: Ensures secure mail sending.
- HTML Email Support: Enables styled emails and custom content.
- Ease of Use: PHP Mailer simplifies sending emails compared to PHP’s
mail()
function.
Conclusion
Using PHP Mailer is a great way to send emails securely and efficiently from your PHP application. It’s easy to set up, supports SMTP, and allows for HTML emails, making it far superior to the basic mail()
function in raw PHP.
Explore More: 10 JavaScript Tricks and Practices You Need To Know, 6 Step of Version Control: Basic Git Command for Beginners, Creating an Extension for Chrome: A Step-by-Step Guide, Create a WordPress Custom Query with WP_Query, Master PHP Namespace: 3 Powerful Ways to Organize Your Code Like a Pro