Skip to content
Advertisement

Sending a mail using PHPMailer with Gmail Account

I am trying to send an email from my website using the PHPMailer library. All I did so for was:

  1. I downloaded PHPMailer 5.2-stable from this link.

  2. I uploaded the following files on my hosting: class.phpmailer.php, class.smtp.php and PHPMailerAutoload.php

  3. Then I created a file name contact.php and wrote the following code in it:

    require 'phpmailer/PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail->SMTPDebug = 1;  
    
    $mail->SMTPAuth = true; 
    $mail->IsSMTP();
    $mail->Host = "smtp.gmail.com";
    $mail->Username = "*****************"; //My Email Address
    $mail->Password = "*****************"; //My Email's Address
    $mail->SMTPSecure = "tls";   
    $mail->Port = 578; 
    
    $mail->AddAddress('*****************');
    $mail->From = "*****************";
    $mail->FromName = "Website Contact Form - " . "Website Name";
    $mail->Subject = "New Message from Contact Form";
    
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';    
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail ->isHTML(true);
    $message = NULL;
    if(!$mail->Send()) {
       $message = "Mailer Error: " . $mail->ErrorInfo;
    }else{
       $message = "Message sent!";
    }
    

But after running this file, I get the following error:

2021-06-25 11:44:54 SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP connect() failed.

I tried several emails including and email address directly made from my hosting service, a Gmail account and a Google Workspace email (Formerly called, G suit). All of the failed producing the mentioned error.

Finally, I think it is worth to mention that my hosting service does not support PHP’s mail() function and that’s why I needed to use SMTP.

Update: After contacting the host and trying a lot, while I am sure that I enter the correct username and password, I get the following error:

2021-06-27 12:45:19 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP u21sm391388lfu.60 - gsmtp
2021-06-27 12:45:19 CLIENT -> SERVER: EHLO empiya.com.tr
2021-06-27 12:45:19 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [77.245.159.9]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-06-27 12:45:19 CLIENT -> SERVER: STARTTLS
2021-06-27 12:45:19 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2021-06-27 12:45:19 CLIENT -> SERVER: EHLO empiya.com.tr
2021-06-27 12:45:19 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [77.245.159.9]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-06-27 12:45:19 CLIENT -> SERVER: AUTH LOGIN
2021-06-27 12:45:19 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2021-06-27 12:45:19 CLIENT -> SERVER: [credentials hidden]
2021-06-27 12:45:19 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2021-06-27 12:45:19 CLIENT -> SERVER: [credentials hidden]
2021-06-27 12:45:20 SERVER -> CLIENT: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu534-5.7.14 vrVLkxbm7_GwFssilR38zKpR_BuczuSM3RIslh77zk6d-DzAo1btUW7FKO8f69tyzO7Fn534-5.7.14 6jydZV9BH6qfEQfpcOfh1z-_V5nn7beFbI0Ekuto0gyBede5j-6dniX9mG6jLWRp>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 u21sm391388lfu.60 - gsmtp
2021-06-27 12:45:20 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu534-5.7.14 vrVLkxbm7_GwFssilR38zKpR_BuczuSM3RIslh77zk6d-DzAo1btUW7FKO8f69tyzO7Fn534-5.7.14 6jydZV9BH6qfEQfpcOfh1z-_V5nn7beFbI0Ekuto0gyBede5j-6dniX9mG6jLWRp>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 u21sm391388lfu.60 - gsmtp
SMTP Error: Could not authenticate.
2021-06-27 12:45:20 CLIENT -> SERVER: QUIT
2021-06-27 12:45:20 SERVER -> CLIENT: 221 2.0.0 closing connection u21sm391388lfu.60 - gsmtp
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.

Advertisement

Answer

  1. Make sure that “less secure app access” setting is turned on for your account. https://support.google.com/accounts/answer/6010255?hl=en#zippy=%2Cif-less-secure-app-access-is-on-for-your-account

  2. Secondly make sure that your host allows to use outgoing port 578, or you can even request them to enable outgoing port.

  3. Try using ssl instead of tls, which is more secure and reliable.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement