How to use PHPMailer


#1

Hello everybody!

I know most of you send out emails via PHP using the built in mail() function.
But, did you know that PHPMailer exists?

Never fail sending any email.
How? PHPMailer.

Let’s dive right into the tutorial…

First,

Install the PHPMailer files from this github repo

1

2

image

Then,

Upload it to your website and extract it

1

Login to the file manager at files.000webhost.com

2

Drag and drop the ZIP onto the file manager then hit the Upload button

3

Hit on the ZIP file once, then right click and hit Extract

Define . as your location to extract the ZIP in the current directory

And hit Extract

After that,

Rename the extracted folder to mail

Now, let’s setup

Enter the mail>src directory, then move all the files back to the mail directory

image

Now go back to the mail directory, and delete everything Selected in the image below

The script is now ready

To be used in a PHP file!

1

Enable less secure apps in Gmail
https://support.google.com/accounts/answer/6010255?hl=en

2

Copy and paste this code onto the file which should send the email

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require $_SERVER['DOCUMENT_ROOT'] . '/mail/Exception.php';
require $_SERVER['DOCUMENT_ROOT'] . '/mail/PHPMailer.php';
require $_SERVER['DOCUMENT_ROOT'] . '/mail/SMTP.php';

$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = "smtp.gmail.com"; // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->Port = 587; // TLS only
$mail->SMTPSecure = 'tls'; // ssl is deprecated
$mail->SMTPAuth = true;
$mail->Username = 'youremail@gmail.com'; // email
$mail->Password = 'PASSWORD'; // password
$mail->setFrom('system@cksoftwares.com', 'CKSoftwares System'); // From email and name
$mail->addAddress('to@address.com', 'Mr. Brown'); // to email and name
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body"); //$mail->msgHTML(file_get_contents('contents.html'), __DIR__); //Read an HTML message body from an external file, convert referenced images to embedded,
$mail->AltBody = 'HTML messaging not supported'; // If html emails is not supported by the receiver, show this body
// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file

if(!$mail->send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
}else{
    echo "Message sent!";
}

Change the lines which have comments next to them according to your needs.

And enable IMAP in your gmail settings

Then complete this
https://accounts.google.com/DisplayUnlockCaptcha

And finally, enjoy!


Mail from my site don't works
SMTP Server and password
#4

If you encounter any issues, report them here