0

Hi I am very new to phpmailer and smtp servers.. here is my code :

    require 'class/PHPMailerAutoload.php';

    $mail = new PHPMailer(); // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465; // or 587
    $mail->IsHTML(true);
    $mail->Username = "[email protected]";
    $mail->Password = "gmailpw";
    $mail->SetFrom("[email protected]");
    $mail->Subject = "Test";
    $mail->Body = "hello";
    $mail->AddAddress("[email protected]");
    if(!$mail->Send()){
          echo "Mailer Error: " . $mail->ErrorInfo;
   } else {
     echo "Message has been sent";
   }

when i try to run the program it return an error message:

SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I dont really know what is the problem I hope someone can help.

Update : here is the full error message here is the full error message.

2015-05-19 07:28:50 CLIENT -> SERVER: EHLO localhost 
2015-05-19 07:28:50 CLIENT -> SERVER: AUTH LOGIN 
2015-05-19 07:28:50 CLIENT -> SERVER: Y2FtcHVzbWVzc2FnaW5nLmRwY0BnbWFpbC5jb20= 
2015-05-19 07:28:50 CLIENT -> SERVER: ODA1Q01TdXBwb3J0 
2015-05-19 07:28:51 SMTP ERROR: Password command failed: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 t7sm7761459oie.22 - gsmtp 
2015-05-19 07:28:51 SMTP Error: Could not authenticate. 
2015-05-19 07:28:51 CLIENT -> SERVER: QUIT 2015-05-19 07:28:52 
7

1 Answer 1

1
<?php

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');

require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "[email protected]";

//Password to use for SMTP authentication
$mail->Password = "yourpassword";

//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'First Last');

//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'John Doe');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
2
  • While this is the currently the recommended way to send via gmail with PHPMailer, it still breaks in the way that the OP says because google's auth system breaks it, so this is not an answer.
    – Synchro
    May 19, 2015 at 8:42
  • You're just lucky - google has been changing the way they handle auth, especially if you use google apps for business. If you follow the troubleshooting link in the question you'll see an explanation of it.
    – Synchro
    May 19, 2015 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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