xrcwrn xrcwrn - 3 years ago 112
PHP Question

Unable to send email in php

I am trying following code to send mail
but it is showing

Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

and gmail is informing me that Sign-in attempt prevented

require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'sss@gmail.com';
$mail->Password = '*******';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->From = 'abc@gmail.com';
$mail->FromName = 'asdf ';
$mail->addAddress('abc@gmail.com', 'sadf ');

$mail->WordWrap = 50;

$mail->Subject = "Using PHPMailer";
$mail->Body = "Hi Iam using PHPMailer library to sent SMTP mail from localhost";

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

echo "Message has been sent";

How to resolve above problem?

Answer Source

Since you are getting email from Google, it describes the email is trying to send but it is blocked by Google. Do the following steps.

I hope this helps.

  1. Check if IMAP is enabled
  2. Check here and enable less secure apps
  3. Display Unlock Captcha
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download