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 = '[email protected]';
    $mail->Password = '*******';
     $mail->Port =  587;
    $mail->SMTPSecure = 'tls';
     $mail->From = '[email protected]';
    $mail->FromName = 'asdf ';
    $mail->addAddress('[email protected]', '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?

6
  • Does the firewall of your host have port 587 outgoing open?
    – Blaatpraat
    Commented Jan 15, 2016 at 9:19
  • Out of curiosity, have you tried port 465 and 25 with ssl?
    – Torxed
    Commented Jan 15, 2016 at 9:19
  • yes both port is showing same problem
    – xrcwrn
    Commented Jan 15, 2016 at 9:21
  • Is above code is correct
    – xrcwrn
    Commented Jan 15, 2016 at 9:28
  • stackoverflow.com/a/27982826/876739 this solved my problem
    – xrcwrn
    Commented Jan 15, 2016 at 9:37

5 Answers 5

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
    
0
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   = "[email protected]";  // GMAIL username
$mail->Password   = "yourpassword";            // GMAIL password

$mail->SetFrom('[email protected]', 'First Last');

$mail->AddReplyTo("[email protected]","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 = "[email protected]";
$mail->AddAddress($address, "John Doe");


if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
2
  • Done still same message
    – xrcwrn
    Commented Jan 15, 2016 at 9:28
  • Ah ok, i have the same problem but with Allow third party apps i solved.
    – rdn87
    Commented Jan 15, 2016 at 9:59
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
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!

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 protected]";
$password="accountpassword";
$to="[email protected]";
$from="[email protected]";
$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";
}
?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.