Setting Guide on How to Send Email Using PHPmailer and Gmail

How to Send Email Using PHPmailer and Gmail

Sending emails using PHPmailer is the best way to overcome errors when using the mail() function.


In addition to overcoming the shortcomings of the mail() function. The PHPmailer library will also make it faster for developers to code email delivery. Because it only needs to call from the PHPmailer library. 
Apart from being used for mail hosting, PHPmailer can also be used for gmail SMTP. On this occasion, we will discuss PHPmailer settings



How to Enable Gmail's Less Secure Apps


1. login to gmail
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 







2. Next create a file index.php and fill it with the following script:


<!DOCTYPE html>
<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>
  </tr>
  <tr>
   <td>Pesan :</td>
   <td><textarea name=”pesan” cols=”32″ rows=”5″></textarea></td>
  </tr>
  <tr>
   <td></td>
   <td><input type=”submit” name=”kirim” value=”Kirim”></td>
  </tr>
 </table> 
</form>
</body>
</html>

 3.  create a file kirim.php in the folder of your website. And fill in the following mailer php script : 


Here's the 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”;
}

?>

 

4. After all the settings above are appropriate, then you can access the web address for email testing to prove that PHPmailer is working properly..

If the debug section is enabled, the following message will appear :
$mail->SMTPDebug = 3; (hilangkan tanda // untuk mengaktifkan debug)


 

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..

Download Script PHPMailer Gmail

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.