1

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

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

    $mail = new PHPMailer;

    $mail->isSMTP();
    $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->isHTML(true);

    $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;
       exit;
    }

    echo "Message has been sent";
    ?>

How to resolve above problem?

xrcwrn
  • 5,339
  • 17
  • 68
  • 129

5 Answers5

2

You need set permission for send mail with gmail.

- Login Google Account
- Go Privacy Page
- Allow third party apps

After try this code:

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "tls";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the GMAIL server
$mail->Username   = "yourusername@gmail.com";  // GMAIL username
$mail->Password   = "yourpassword";            // GMAIL password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");


if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
rdn87
  • 739
  • 5
  • 18
2

Try the following steps:

  • enable debug mode to catch possible errors

    $mail->SMTPDebug = 1;
    
  • enable SMTP authentication

    $mail->SMTPAuth = true;
    
  • also check for SSL support in php configuration file (php.ini)

    extension=php_openssl.dll
    
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
1

change the following in your code

isSMTP() to IsSMTP() , addAddress() to AddAddress() & isHTML() to IsHTML().

and yes check the ports also. sometoimes port also off which do not let connection to be established.

Hope it will work!

nerdyDev
  • 376
  • 3
  • 15
1

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
Nikhil
  • 1,450
  • 11
  • 24
1

I think you have to enable POP and IMAP in you gamil. Try this

  • Sign in to Gmail
  • Click the gear in the top right.
  • Select Settings.
  • Click Forwarding and POP/IMAP.
  • Select Enable IMAP.
  • Click Save Changes.

<?
$account="email_address@gmail.com";
$password="accountpassword";
$to="mail@subinsb.com";
$from="email_address@gmail.com";
$from_name="Name";
$msg="<strong>This is a bold text.</strong>"; // HTML message
$subject="Database Backup";
/*End Config*/

include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth= true;
$mail->Port = 465; // Or 587
$mail->Username= $account;
$mail->Password= $password;
$mail->SMTPSecure = 'ssl';
$mail->From = $from;
$mail->FromName= $from_name;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->addAddress($to);
if(!$mail->send()){
 echo "Mailer Error: " . $mail->ErrorInfo;
}else{
 echo "E-Mail has been sent";
}
?>
krishnakumar
  • 118
  • 1
  • 1
  • 11