How to use PHPMailer

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
$mail->SMTPOptions = array(
                    'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true
                    )
                );
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!

1 Like

If you encounter any issues, report them here

Using gmail smtp
Works on localhost (using Xampp)
Shows following error when using on 000webhost:
Mailer Error : SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Kindly Help!!

Can you screenshot the setup you’ve got? :slight_smile:

Check this out:

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

include '../../credentials.php';

   
//PHP MAILER
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 = $hostname; // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->Port = 587; // TLS only 587
$mail->SMTPSecure = 'tls'; // ssl is deprecated
$mail->SMTPAuth = true;
$mail->Username = $email; // email
$mail->Password = $password; // password
$mail->setFrom($email, 'Sumit Sahay'); // From email and name
$mail->addAddress($email_to, $first_name." ".$last_name); // to email and name
$mail->AddReplyTo($email_from, $first_name." ".$last_name);
$mail->Subject = $email_subject;
$mail->msgHTML($email_message); //$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

 $mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

   
if(!$mail->Send()){
    echo "Mailer Error : " . $mail->ErrorInfo;
}else{
    echo "</div>";
    echo "Email Sent";
}
?>

I’ll ask you to just give it a little wait for the moment as there are updates taking place I am just wondering if this is a side effect

Hello Guys, I followed this guide, am I missing something? I’m getting this message:

2019-10-24 21:19:02 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP i5sm3876626ywe.110 - gsmtp
2019-10-24 21:19:02 CLIENT -> SERVER: EHLO coto5303.000webhostapp.com
2019-10-24 21:19:02 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [153.92.0.16]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-10-24 21:19:02 CLIENT -> SERVER: STARTTLS
2019-10-24 21:19:02 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2019-10-24 21:19:03 CLIENT -> SERVER: QUIT
2019-10-24 21:19:03 SERVER -> CLIENT:
2019-10-24 21:19:03 SMTP ERROR: QUIT command failed:
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

This is my code:

<?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 = gethostbyname('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 = 'MyEMAIL@gmail.com'; // email $mail->Password = 'MyEmailPsw'; // password $mail->setFrom('system@cksoftwares.com', 'CKSoftwares System'); // From email and name $mail->addAddress('SentToEmail@gmail.com', 'Mr. Any'); // 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!"; } ?>

Thanks :slight_smile:

Looks like a wrong email/password?

I typed this in the code because I didn’t want to make public my personal info,
When running my script I putting the gmail address linked to this function and the pswd I use to login to that acc

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

1 Like

Really weird. Could you try another provider?

Let us know your 000webhostapp URL please :slight_smile:

Hello,

This is the link for testing this
https://coto5303.000webhostapp.com/SMTP_test.php
when loaded it should send me a test email

$mail->Username = ‘MyEmail@gmail.com’; // email
$mail->Password = ‘MyEmailPswd’; // password
$mail->setFrom(‘MyEmail@gmail.com’,); // From email and name
$mail->addAddress(‘MyOtherGmailAcc@gmail.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!”;
}

1 Like

Sorry, Im kinda new to this type of things,
what other providers are recomended?

Thanks :slight_smile:

1 Like

Have you done this task?

  • and this task?

https://accounts.google.com/DisplayUnlockCaptcha

and finally this task?

https://myaccount.google.com/lesssecureapps

I’ve done :frowning:

1 Like

Checkout new.php in your public_html and let me know what is happening now after I’ve added a small piece of code.

```
$mail->SMTPOptions = array(
                    'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true
                    )
                );
```

I’ve tested it with my email once as the TO and it seemed to work?

WOW!!! You guys are really masters on this, it worked!!!

Thank you so much!!!

1 Like

Glad to hear! Sorry you had issues to begin with! :slight_smile: