-3

I am using Gmail SMTP for sending emails in php. The code is working fine in one of my client website. When I am using the same code for another client website, I am getting an error like "Could not authenticate, SMTP connect() failed.

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting"

. I don't have knowledge in server settings. Both websites are on different servers. Please check the below code.

<?php
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->Host = 'smtp.gmail.com';

//$mail->isSMTP();
$mail->SMTPDebug = 2;

$mail->SMTPAuth = true;                               
$mail->Username = 'example@gmail.com';                
$mail->Password = 'password';                          
$mail->SMTPSecure = 'ssl';                           
$mail->Port = 465;  // for tls 587

$mail->Subject = 'Test Email';

$mail->isHTML(true);

$mail->Body = 'Message from test website';

$mail->setFrom('mahmood.bts@gmail.com');


$mail->addAddress('test@companyname.com');
$mail->addCC('info@companyname.com');

if($mail->send())
    echo "mail is sent";

    else 
      echo $mail->ErrorInfo;

 ?>

I used php mail function instead of SMTP, Then also I am unable to send emails. Please help me.

Nimesh Patel
  • 796
  • 1
  • 7
  • 23

1 Answers1

1

You should allow sending emails from an untrusted app.

Google may block sign-in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safe.

To disable this security feature:

  • Sign in to Gmail
  • Click here to access Less Secure App Access in My Account.
  • Next to “Allow less secure apps: OFF”, select the toggle switch to turn ON.
Sergey Podgornyy
  • 663
  • 10
  • 19
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/21986973) – taras Jan 21 '19 at 13:10
  • 1
    @taras Ok, I will include the most import content from the link in my answer – Sergey Podgornyy Jan 21 '19 at 13:12