How to Sending Emails using PHPMailer

in PHP MySQL


In php there is a mail function that helps you send mail, but this function has many limitations in use, specifically hosting rarely allows you to send mail with this function. To handle sending mail, we can use support libraries such as PHPMailer, Swiftmailer… PHPMailer is a very popular, multi-user mailing library. Here I will guide how to send mail with PHPMailer.

Features of PHPMailer

  • Send mail via SMTP protocol.
  • You can easily use your personal email address or your company email to send it to increase the credibility of the mail.
  • Send mail quickly, less error mail is sent directly to the inbox.
  • Can add cc, bcc, attach files.
  • There is a two-way interaction, which means that when a customer responds to the mail, you will receive the mail in your inbox.

Things to do:

– Have a gmail account

– Download PHPMailer library to use

– Configure and send mail using PHP

 

1. First you need a gmail account to handle sending and receiving

You need to edit the Turn on Less secure app access settings

Manager Google Account -> Security -> Turn on access

Gmail security

Gmail security

 

Turn on Less secure app access

Turn on Less secure app access

 

 

2. PHPMailer library

Download full download code below or PHPMailer library here: https://github.com/PHPMailer/PHPMailer

 

3. Configure and send mail using PHP

<?php

// PHPMailer require
require_once __DIR__ . ‘/vendor/phpmailer/src/Exception.php’;
require_once __DIR__ . ‘/vendor/phpmailer/src/PHPMailer.php’;
require_once __DIR__ . ‘/vendor/phpmailer/src/SMTP.php’;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
// Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // detail debug output
$mail->isSMTP();
$mail->Host = ‘smtp.gmail.com’;
$mail->SMTPAuth = true;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->CharSet = “utf-8”; // Can read Japanese, Chinese, etc.
$mail->Encoding = “base64”;

$mail->Username = ‘your-email@gmail.com’; // your gmail email
$mail->Password = ‘your-gmail-password’; // your gmail password

// Sender settings
$mail->setFrom(‘your-email@gmail.com’, ‘Sender Name’);

// Recipient lists
$addreddToList = [‘recipient1-email-address’ => ‘Name Recipient 1’, ‘recipient2-email-address’ => ‘Name Recipient 2’, …];
$ccToList = [‘cc1-email-address’ => ‘Name cc1’, ‘cc1-email-address’ => ‘Name cc2’,…];

foreach ($addreddToList as $key => $emailTo) {
$mail->addAddress($key, $emailTo);
}
foreach ($ccToList as $key => $emailTo) {
$mail->AddCC($key, $emailTo);
}

// Setting the email content
$mail->IsHTML(true);
$subject = “Subject”;
$mail->Subject = $subject;

$mail->Body = ‘Body Content <b>bold format</b>….’;
$mail->AltBody = ‘This is the plain text’;

//Attachments
$mail->AddAttachment(‘/path/file.tar.gz’); // Add attachments
$mail->AddAttachment(‘/path/image.jpg’, ‘reservation_file_name.jpg’); // Optional name

// example:
// $attachfile = ‘attach-file-path/fileName.pdf’;
// $mail->AddAttachment($attachfile, ‘reservation_file_name.pdf’);

if ($mail->send()) {
echo “Email message sent.”;
}

} catch (Exception $e) {
echo “Error: {$mail->ErrorInfo}”;
}
?>

 

Download source code at here: Download code

Tags: , , , ,