SMTP ERROR: Failed to connect to server: (0) & SMTP connect() failed. Mailer Error: SMTP connect() failed - phpmailer

I am trying to send a mail via phpMailer but I got below error
2019-03-28 10:54:00 SMTP ERROR: Failed to connect to server: (0)
2019-03-28 10:54:00 SMTP connect() failed. Mailer Error: SMTP
connect() failed.
I referred below links
https://stackoverflow.com/questions/43815571/mailer-error-smtp-connect-failed
https://stackoverflow.com/questions/21088062/phpmailer-smtp-connection-error
https://stackoverflow.com/questions/43999573/phpmailer-smtp-connect-error
https://stackoverflow.com/questions/36735979/mailer-error-smtp-connect-failed
https://stackoverflow.com/questions/30476024/smtp-connect-failed-phpmailer
but after implementing the solution as mention in the above links, getting the same error.
I tried with PHP mail() function for the same credential, it's working but I wanted to send a mail via SMTP.
here is my code
require('mail/class.phpmailer.php');
require('mail/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Mailer = "smtp";
$mail->Host = 'mail.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'From#example.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('From#example.com', 'from');
$mail->addAddress('To#example.com', 'To');
$mail->isHTML(true);
$mail->Subject = 'subject';
$mail->Body = 'This is message';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

Related

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated in linux

Hi i want send a mail from my linux server my code block/project is working in my local(windows) but i get this error when i run my project in linux.
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
Here is my code block where is my problem? thanks.
string domain = "my.local";
string userName = "info";
string userPass = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("AkXyz2*"));
string host = "mail.mymail.com.tr";
string fromMail = "mymail#mymail.com.tr";
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
var smtp = new SmtpClient(host, 587);//port
smtp.EnableSsl = true;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(userName, userPass, domain);
smtp.Credentials = credentials;
smtp.ServicePoint.Expect100Continue = false;
ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
...
...
smtp.Send(mail);

PHPMailer cannot send via smtp.gmail.com

I'm trying to send mail with PHPMailer plugin via smtp.gmail.com. The sending works fine on my local computer but when I use my remote server with following code it delays the sending, normally around 30 seconds.
function sendAuthEmail($subject, $mail_body, $mailTo, $mailToTitle, $mailFrom, $fromTitle)
{
require 'PhpMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->Username = 'no-reply#domail.com';
$mail->Password = 'password';
$mail->AddReplyTo($mailFrom, $fromTitle);
$mail->SetFrom($mail->Username, 'Domain.com');
$mail->AddAddress($mailTo, $mailToTitle);
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->MsgHTML($mail_body);
if($mail->Send()) {
return;
} else {
die('There was an error: '.$mail->ErrorInfo.'</i>');
}
}
But when I try to use gethostbyname('smtp.gmail.com') it fails with following debug information:
2016-11-23 12:36:11 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP a47sm16079918qtc.17 - gsmtp
2016-11-23 12:36:11 CLIENT -> SERVER: EHLO www.sellercloud.com
2016-11-23 12:36:11 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [199.189.62.92] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
2016-11-23 12:36:11 CLIENT -> SERVER: STARTTLS
2016-11-23 12:36:11 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2016-11-23 12:36:11 SMTP Error: Could not connect to SMTP host.
2016-11-23 12:36:11 CLIENT -> SERVER: QUIT
2016-11-23 12:36:11 SERVER -> CLIENT:
2016-11-23 12:36:11 SMTP ERROR: QUIT command failed:
2016-11-23 12:36:11 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
The problem with the server turned out to be a DNS resolving issue. PHP mailer couldn't resolve smtp.gmail.com correctly, and this was the main reason why none of the known fixes worked.
If you're having similar issue, check your DNS servers.

phpmailer suddenly got very slow

I use phpmailer for sending emails and while it worked flawlesly until now, I noticed that sending emails got very very slow, around 2-3 minutes. The mails are sent, but the waiting time is killing me.
My code is:
protected function SendPHPMailerMail($from = null, $name = null, $subject = null, $message = null)
{
require_once ('phpmailer/PHPMailerAutoload.php');
if($from && $name && $subject && $message)
{
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;
$mail->Debugoutput = 'html';
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // sets GMAIL as the SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'tls'; // sets the prefix to the servier
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = 'x#gmail.com'; // GMAIL username
$mail->Password = 'pass'; // GMAIL password
$mail->AddReplyTo($from, $name);
$mail->AddAddress('x#gmail.com', 'Me');
$mail->SetFrom('x#gmail.com', 'Me');
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Send();
}
}
I tried debugging, there are no errors. I read in a different question thread that using gethostbyname('smtp.gmail.com'); worked for some, but not for me. I haven't changed anything on the server and this popped out of nowhere. Not sure what to do.
Thanks!
edit: used SMTPdebug=2 and this is what I get
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 90sm16517084otw.3 - gsmtp
CLIENT -> SERVER: EHLO www.host.com
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [107.182.234.254]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO www.host.com
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [107.182.234.254]250- SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: AUTH LOGIN
SERVER -> CLIENT: 334 VXNlcm5hbWU6
CLIENT -> SERVER: Y29udGFjdEBmaWxlY2x1c3Rlci5jb20=
SERVER -> CLIENT: 334 UGFzc3dvcmQ6
CLIENT -> SERVER: Z2l6bW8uMTIzcXdl
SERVER -> CLIENT: 235 2.7.0 Accepted
CLIENT -> SERVER: MAIL FROM:<x#gmail.com>
SERVER -> CLIENT: 250 2.1.0 OK 90sm16517084otw.3 - gsmtp
CLIENT -> SERVER: RCPT TO:<x#gmail.com>
SERVER -> CLIENT: 250 2.1.5 OK 90sm16517084otw.3 - gsmtp
CLIENT -> SERVER: DATA
SERVER -> CLIENT: 354 Go ahead 90sm16517084otw.3 - gsmtp
CLIENT -> SERVER: Date: Thu, 20 Oct 2016 20:44:01 +0000
CLIENT -> SERVER: To: Me <x#gmail.com>
CLIENT -> SERVER: From: Me <x#gmail.com>
CLIENT -> SERVER: Reply-To: X <sender#gmail.com>
CLIENT -> SERVER: Subject: Partnership and business development
CLIENT -> SERVER: Message-ID: <6dae364927ad751cc524f07a77f5eea1#www.host.com>
CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.16 (https://github.com/PHPMailer/PHPMailer)
CLIENT -> SERVER: MIME-Version: 1.0
CLIENT -> SERVER: Content-Type: text/plain; charset=iso-8859-1
CLIENT -> SERVER:
CLIENT -> SERVER: message
CLIENT -> SERVER:
CLIENT -> SERVER: .
SERVER -> CLIENT: 250 2.0.0 OK 1476996370 90sm16517084otw.3 - gsmtp
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection 90sm16517084otw.3 - gsmtp
I replaced the actual values of the site and emails.

Want to receive bounce back emails on my gmail account

I am using phpmailer class to send email from my server. Below is my code, i am able to send email with this code to valid email address. I want to receive bounce back emails on my email address farazaleem#gmail.com but i am not receiving any email when i send email to invalid email account like dsfsdfdsfsfsfsfs#rtretreert.com
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true;
$mail->Host = "smtpout.secureserver.net";
$mail->Port = 25;
$mail->Username = "xxxxxx#mydomain.com";
$mail->Password = "xxxxxxx";
$mail->SetFrom('farazaleem#gmail.com');
$mail->Subject = "Email body";
$mail->Sender = 'farazaleem#gmail.com';
$mail->MsgHTML($email_details);
$mail->AddAddress($entered_person_email, $full_name);
$mail->Send();

Mail rejected using phpmailer on GoDaddy.com host

Using this:
require("lib/class.phpmailer.php");
$mail = new PHPMailer();
$mail->Username = "XXX#validGoDaddyAccount.com";
$mail->Password = "xxx";
$mail->Subject = $subject;
$mail->Body= $message;
$mail->AddAddress($toAddr);
$mail->FromName = "XXXXXXXXXXXX";
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "smtpout.secureserver.net";
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
return true;
}
I get this response:
SMTP -> FROM SERVER:220 p3plsmtpa07-05.prod.phx3.secureserver.net ESMTP
SMTP -> FROM SERVER: 250-p3plsmtpa07-05.prod.phx3.secureserver.net hello [00.00.0.0], secureserver.net 250-HELP 250-AUTH LOGIN PLAIN 250-SIZE 31457280 250-PIPELINING 250-8BITMIME 250 OK
SMTP -> FROM SERVER:550 User XXX has exceeded their send quota
SMTP -> ERROR: MAIL not accepted from server: 550 User XXX has exceeded their send quota
The following From address failed: XXX : MAIL not accepted from server,550,User XXXm has exceeded their send quota
SMTP server error: User XXX has exceeded their send quota
Mailer Error: The following From address failed: XXX : MAIL not accepted from server,550,User XXX has exceeded their send quota
SMTP server error: User XXX has exceeded their send quota
I know about setting the SMTP Relays for GoDaddy accounts and they are set right. I can send email from this account all day long as long as I don't use PHPmailer.
see the following line
SMTP -> FROM SERVER:550 User XXX has exceeded their send quota
That's why it is rejecting.
The solution is that I needed to pre-authorize with POP prior to starting the SMTP operation:
require("lib/class.phpmailer.php");
require_once('lib/class.pop3.php');
$pop = new POP3();
$pop->Authorise('pop.server.net', 110, 30, 'username', 'password', 1);
$mail = new PHPMailer();
...

Resources