How to Send Email Using PHPmailer and Gmail
Sending emails using PHPmailer is the best way to overcome errors when using the mail() function.
How to Enable Gmail's Less Secure Apps
2. Activate Less Secure Apps by accessing the following link Less Secure Apps or Here.
3. Change the status to ON.
Creating a PHPMailer Email Script
1. After downloading the PHPmailer library, please upload it to the website folder and extract it. Then remove the files in the "PHPMailer-master" folder to the main folder (website). Download PHPMailer Disini
<html>
<head>
<title></title>
</head>
<body>
<h2>Kirim Email</h2>
<form method=”POST” action=”kirim.php”>
<table>
<tr>
<td>Nama :</td>
<td><input type=”text” name=”nama” size=”30″></td>
</tr>
<tr>
<td>Email :</td>
<td><input type=”email” name=”email” size=”30″></td>
</tr>
<tr><td>Subjek :</td>
<td><input type=”text” name=”subjek” size=”30″></td>
3. create a file kirim.php in the folder of your website. And fill in the following mailer php script :
<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
require_once “library/PHPMailer.php”;
require_once “library/Exception.php”;
require_once “library/OAuth.php”;
require_once “library/POP3.php”;
require_once “library/SMTP.php”;
$mail = new PHPMailer;
//Enable SMTP debugging.
//$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = “tls://smtp.gmail.com”; //host mail server
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = “temailanda@gmail.com”; //nama-email smtp
$mail->Password = “password-gmail”; //password email smtp
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = “ssl”;
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = “temailanda@gmail.com”; //email pengirim
$mail->FromName = “Ini adalah PHPmailer”; //nama pengirim
$mail->addAddress($_POST[’email’], $_POST[‘nama’]); //email penerima
$mail->isHTML(true);
$mail->Subject = $_POST[‘subjek’]; //subject
$mail->Body = $_POST[‘pesan’]; //isi email
$mail->AltBody = “PHP mailer”; //body email
if(!$mail->send())
{
echo “Mailer Error: ” . $mail->ErrorInfo;
}
else
{
echo “Message has been sent successfully”;
}
?>
5. Congratulations PHPmailer has been successfully created. In this guide, PHPmailer has been integrated with forms and gmail. Because you cannot copy the script, please download the script directly below..