Google SMTP Stopped Working - php

The email address and password is definitely correct. The script was working until yesterday. Now we receive the following error:
Mailer Error: SMTP connect() failed.
We can login to the Google email address. I can send emails via SMTP in Outlook. We have Google Apps Free Edition, so it is not possible to contact Google.
Does anyone have any ideas as to why the connection has just stopped working? Thank you.
mail->SMTPDebug = 2;
$mail->IsSMTP(true); // enable SMTP
$mail->SMTPDebug = 0; // 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; // set the SMTP port for the GMAIL server
$mail->Username = $Username; // SMTP account username
$mail->Password = $Password;
$doc_dir = 'pdf/';
$mail->From = $From;
$mail->FromName = $FromName;
$mail->addAddress($email);
$mail->WordWrap = 50;
$mail->addAttachment($doc_dir.$FileName);
$mail->isHTML(true);
$mail->Subject = 'Your '.$warehouse_name.' Order Has Shipped';
$mail->Body = $Message;
$mail->AltBody = 'Racquet Depot UK';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
};

The host had made an SMTP restriction which caused the issue. Otherwise, the code is correct.

Related

SMTP Error: Could not authenticate. Message could not be sent. Mailer Error: SMTP Error: Could not authenticate

When I trying to send a email using PHP SMTP email server, following error has occurred.
SMTP Error: Could not authenticate. Message could not be sent.
Mailer Error: SMTP Error: Could not authenticate.
Following is my code that I have used.
function supervisorMail(){
global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "***#gmail.com";
$mail->Password = "****";
$mail->SetFrom("***#gmail.com", "Employee Leave Management System");
$userID=$_SESSION['userID'];
$select_query = mysql_query("SELECT * FROM employee WHERE emp_id = '$userID'");
$select_sql = mysql_fetch_array($select_query);
$name=$select_sql['manager_name'];
var_dump($name);
$select_query1 = mysql_query("SELECT email FROM employee WHERE emp_id='$name'");
$select_sql1 = mysql_fetch_array($select_query1);
$email=$select_sql1['email'];
var_dump($email);
$mail->Subject = "subject ";
$mail->Body = "something.";
$mail_to = $email;
$mail->AddAddress($mail_to);
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
}
How can I fixed this error.
The error message is very clear "Could not authenticate".
I would say you are correctly using the PHPMailer class, but just not filling in the correct gmail account details.
I suppose that in your question the fields username and password look like
$mail->Username = "#gmail.com";
$mail->Password = "";
just because you, obviously, don't want to share them, I would suggest to present them like this in the question
$mail->Username = "********#gmail.com";
$mail->Password = "********";
So if you are using the correct username and password there are other two things to check
Maybe your setting "Access for less secure apps" is turned off.
After you login from the web you can turn it on from this page
https://www.google.com/settings/u/0/security/lesssecureapps
If that is On, it might be possible you have 2-Step Verification enabled in your gmail account. If this is the case, you need to create an App specific password.
Follow this link for a step by step guide on how to do it
http://email.about.com/od/gmailtips/fl/How-to-Get-a-Password-to-Access-Gmail-By-POP-or-IMAP.htm
Please unable your less secure app
and then
Try to comment this line
//$mail->IsSMTP();
I hope it will work!
You may try to use google app password for authentication:
https://support.google.com/accounts/answer/185833?hl=en
2-Step-Verification & 2. App Passwords
As mentioned in comment, you need to enter valid gmail id and password.
Please refer below demo code.
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '*****001#gmail.com'; // SMTP username
$mail->Password = 'ma****02'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('ravi******#gmail.com', 'Mailer'); // Add set from id
$mail->addAddress('er.ravihirani#gmail.com', 'Ravi Hirani'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
I ran into this error message and wanted to post an answer in case it can help someone else. I have phpmailer installed with composer and am running php 7. The implementation of the phpmailer script that threw the error is in a php object method. The password variable, set in a config file elsewhere, needed to be declared as a global in the object context otherwise it did not contain the actual password and thus caused the authentication error..
global $password; // <- this added to make script work
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
// Set mailer to use SMTP
$mail->Host = '*******.com';
$mail->SMTPAuth = true;
$mail->Username = 'no-reply#*******.com';
$mail->Password = $password;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('no-reply#*******.com', '******* - DO NOT REPLY');
//$mail->isHTML(true);
$mail->AddAddress($user->email);
$mail->Subject = "*******";
$mail->Body = "*******";
$mail->send();
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
In my case it was because I didn't turn on the Less secure app access from here: https://myaccount.google.com/lesssecureapps

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
<?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?
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
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!";
}
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.
Check if IMAP is enabled
Check here and enable less secure apps
Display Unlock Captcha
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!
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";
}
?>

Sending mail in PHP using Hotmail smtp

I am trying to send mail in PHP using Hotmail Smtp. But I am getting error as below:
2014-03-13 06:59:01 CLIENT -> SERVER: EHLO site.com
2014-03-13 06:59:01 CLIENT -> SERVER: AUTH LOGIN
2014-03-13 06:59:01 SMTP ERROR: AUTH command failed: 504 5.3.3 AUTH mechanism LOGIN not available
2014-03-13 06:59:01 CLIENT -> SERVER: QUIT SMTP connect() failed. Mailer Error: SMTP connect() failed.
Please would anyone suggest me what I am doing wrong??
My code :
error_reporting(E_STRICT);
require_once('class.phpmailer.php');
include('class.smtp.php');
$mail = new PHPMailer(); //Initialize a new PHPMailer object;
//$body = preg_replace("[\]",'',$body); //Replace unwanted characters of the content
$mail->CharSet ="ISO-8859-1";//Set the character set you need to specify
$mail->IsSMTP(); // Use SMTP service
$mail->SMTPDebug = 1; // Enable debugging for SMTP
// 1 = errors and messages
// 2 = messages only
$mail->From = 'abc#hotmail.com';
$mail->FromName = 'Name';
$mail->SMTPAuth = true;
$mail->SMTPSecure = "SSL";
$mail->Host = 'smtp.live.com';
$mail->Port = '465';
$mail->Username = 'abc#hotmail.com'; //Username of your email account
$mail->Password = '***'; //Password of your email account
$mail->SetFrom('abc#hotmail.com', 'Name');
$mail->AddReplyTo('abc#hotmail.com','Name');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional, comment out and test
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, '');
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
//var_dump($body);
if(!$mail->Send()) {
//echo $body;
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo "Message sent successfully!";
}
Need help. thanks.
How would I solve this problem? Anyone's help would be appreciated.
Below works for me:
$mail = new PHPMailer();
$mail->SMTPSecure = 'tls';
$mail->Username = "mymail#hotmail.com";
$mail->Password = "mypassword";
$mail->AddAddress("mymail#hotmail.com");
$mail->FromName = "My Name";
$mail->Subject = "My Subject";
$mail->Body = "My Body";
$mail->Host = "smtp.live.com";
$mail->Port = 587;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
$mail->Send();
Windows Live Mail uses port 587(TLS enabled) not the standard 465.
That said wht not just use your hosts local smtp server? That way you won't have to auth (or collect the senders password) and you can still set the senders address as their Hotmail.
$mail->SMTPSecure = "tls";

SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate

include("class.phpmailer.php");
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "******#gmail.com"; // your SMTP username or your gmail username
$mail->Password = "******"; // your SMTP password or your gmail password
$from = "***********#example.com"; // Reply to this email
$to="******#gmail.com"; // Recipients email ID
$name=" Name"; // Recipient's name
$mail->From = $from;
$mail->FromName = "Webmaster"; // Name to indicate where the email came from when the recepient received
$mail->AddAddress($to,$name);
$mail->AddReplyTo($from,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Sending Email From Php Using Gmail";
$mail->Body = "This Email Send through phpmailer, This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
am getting this error
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.
Since you are using SSL. Add this line of code
$mail->SMTPSecure = 'ssl';
Also change your
$mail->Host = "ssl://smtp.gmail.com";
to
$mail->Host = "smtp.gmail.com";
Also, make sure if you have enabled the extension php_openssl.dll
if you are sure your username and password are correct
then do these steps
1 $mail -> SMTPSecure = 'ssl';
2 $mail->Host = "smtp.gmail.com";
3 https://support.google.com/mail/answer/78754
4 go to google acocunts then connected app and change to less secure apps on
follow all steps in number 3

Trying to send mail using yahoo smtp in php

Please can some one suggest me codes to send mails using yahoo smatp, and php..
require("class.phpmailer.php"); // be sure to change this to your location!
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.mail.yahoo.com"; // sets yahoo as the SMTP server
$mail->Port = 25; // set the SMTP port
$mail->Username = "sumthing#yahoo.com"; // yahoo username
$mail->Password = "password"; // yahoo password
$mail->From = "sumthing#yahoo.com";
$mail->FromName = "myname";
$mail->AddAddress("you#example.com");
$mail->Subject = "Test PHPMailer Message";
$mail->Body = "Hi! \n\n This was sent with phpMailer_example3.php.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
This code is giving me errrors 'Could not connect to SMTP host".
Many of the sites providing SMTP do not support SSL (SMTP-S). If supported is usually in different port, such as 465. Connecting to 465 often fails. Try commenting out
$mail->SMTPSecure = "ssl"
Try also first without authentication to make sure the connection works.

Categories