Error with SMTP host in PHPMailer - php

I'm trying to send an email and the PHPMailer is returning the following message:
SERVER -> CLIENT:
CLIENT -> SERVER: EHLO my-domain.com
SERVER -> CLIENT:
SMTP ERROR: EHLO command failed:
SMTP NOTICE: EOF caught while checking if connected
SMTP connect() failed.
Mailer Error: SMTP connect() failed.
My domain is my-domain.com
Here is my page that sends the email:
<?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 - likely to be 25, 465 or 587
$mail->Port = 465;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
//Username to use for SMTP authentication
$mail->Username = "sender#domain.com";
//Password to use for SMTP authentication
$mail->Password = "myPassword";
//Set who the message is to be sent from
$mail->setFrom('webmaster#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('webmaster#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('me#domain.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer 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!";
}
?>
As you can see, my smtp host is set to smtp.gmail.com and it seems tha PHPMailer is trying to send the email via another host, in the case, the same as where my app is hosted (my-domain.com).
Why is it happening?

Related

Message could not be sent.Mailer Error:

Guys i am now working with php mailer library.i have some errors related to mail()
functions.
This my source code
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.google.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'venki14101996#gmail.com'; // SMTP username
$mail->Password = '8903273610'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'venki14101996#gmail.com';
$mail->FromName = 'VENKAT';
$mail->AddAddress('venkat14101996#gmail.com', 'Josh Adams'); // Add a recipient
$mail->AddAddress('rishi27052001#gmail.com'); // Name is optional
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <strong>in bold!</strong>';
$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;
exit;
}
echo 'Message has been sent';
THIS SHOWs ME A ERROR MESSAGE
Message could not be sent.Mailer Error: The following From address failed: venki14101996#gmail.com : Called Mail() without being connected
At before i use this source code before
but i don't have the vendor/autoload file in my phpmailer folder
but i download this file in Github
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
* This uses traditional id & password authentication - look at the gmail_xoauth.phps
* example to see how to use XOAUTH2.
* The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
*/
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.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;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//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 = "username#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', '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'), __DIR__);
//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!";
//Section 2: IMAP
//Uncomment these to save your message in the 'Sent Mail' folder.
#if (save_mail($mail)) {
# echo "Message saved!";
#}
}
//Section 2: IMAP
//IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php
//Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php
//You can use imap_getmailboxes($imapStream, '/imap/ssl') to get a list of available folders or labels, this can
//be useful if you are trying to get this working on a non-Gmail IMAP server.
function save_mail($mail)
{
//You can change 'Sent Mail' to any other folder or tag
$path = "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail";
//Tell your server to open an IMAP connection using the same username and password as you used for SMTP
$imapStream = imap_open($path, $mail->Username, $mail->Password);
$result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
imap_close($imapStream);
return $result;
}
enter code here
Try adding the below code,
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

SMTP mail trigger is not working

I tried to send a mail using smtp server in php mailer function.
This was the error am getting when I triggered this.
Am using the gmail mail server.
SMTP connect() failed.
require_once(TEMPLATEPATH . '/PHPMailer-master/PHPMailerAutoload.php');
$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 - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "xxxx";
//Password to use for SMTP authentication
$mail->Password = "xxxx";
//Set who the message is to be sent from
$mail->setFrom('xxxx', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('xxxxx', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('xxxx', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
$mail->msgHTML('test');
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
This is my code. Please suggest what is the mistake in my code or where to check smtp connection failed error.
Thanks in Advance.
Well, that's why it didn't work ,
Open your php.ini and search for this line ;extension=php_openssl.dll and remove the semi colon before it , save the file and restart your webserver. You will see your coding working after you do this change.
and set your host as follow:
$mail->Host = "ssl://smtp.gmail.com";

PHPMailer Smtp connect failed. Connection Timed Out (110)

date_default_timezone_set('Etc/UTC');
require 'phpmailer/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';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//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 = "username#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "mypassword";
//Set who the message is to be sent from
$mail->setFrom('username#gmail.com', 'User');
//Set an alternative reply-to address
//Set who the message is to be sent to
$mail->addAddress($email,$name);
//Set the subject line
$mail->Subject = 'Subject';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$body = "body";
$mail->msgHTML($body);
//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!";
I tried with ports 465 and 587. No access to php.ini file and also tried setting timeout limit. No go. openssl is enabled.
Checked with:
<?php echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."\n"; ?>
I always get the same error:
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
Mailer Error: SMTP connect() failed

phpmailer with yahoo not working under any setting

so i have been working with gmail and using php mailer and it was working fine.
i tried to do the same with yahoo mail but it doent seem to work.
i have tried various ports and settings but it isnt working.
here is the code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?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 'phpmailer/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.mail.yahoo.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587; //995 and 465 port tried but not working
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tsl';//only ssl tried not working
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "sender#yahoo.com";
//Password to use for SMTP authentication
$mail->Password = "sender_pass";
//Set who the message is to be sent from
$mail->setFrom('sender_#yahoo.com', 'sender_name');
//Set who the message is to be sent to
$mail->addAddress('receiver#yahoo.com', 'receiver_name');
//Set the subject line
$mail->Subject = 'PHPMailer YMail SMTP test';
$mail->msgHTML(file_get_contents('phpmailer/examples/contents.html'), dirname(__FILE__));
$mail->AltBody = 'This is a plain-text message body';
$mail->addAttachment('phpmailer/examples/images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
and i get the following error:
SERVER -> CLIENT: 220 smtp.mail.yahoo.com ESMTP ready
CLIENT -> SERVER: EHLO localhost
CLIENT -> SERVER: AUTH LOGIN
SERVER -> CLIENT: 530 5.7.0 Must issue a STARTTLS command first
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 Bye
SMTP connect() failed.
Mailer Error: SMTP connect() failed.
The correct is tls, you have a spell mistake:
$mail->SMTPSecure = 'tls'
2020 - Google brought me here for something similar.
Finally got phpmailer working with yahoo.
My Findings:
* Ports -> Both ssl/465 and tls/587 work with Yahoo. You dont have to
worry about that.
* From Address -> Cannot be something arbitrary, just use your
yahoo account email here
* ReplyTo Address -> This too must be a valid yahoo account
or you may leave this blank.
And ...
for programmatic usage of yahoo with phpmailer, you need to use another passwd ( not the password that you use to login manually via the web - got this tip from a non stackoverflow site, maybe expert xchange? ). This password is called the App Password. You have to generate it from inside your account.
Account Info -> Account Settings -> Manage Your App Passwords
-> Select Other App -> Key in 'AutoMailer'
or anything you like and generate the passwd - Its a 4 word passwd.
Use this passwd in your phpmailer script.
Everything should be fine now.
The yahoo SMTP host is: smtp.mail.yahoo.fr, the port: 465 and SSL is required.
So with PHPMailer:
$mail->Host = "smtp.mail.yahoo.fr, ";
$mail->Username = "youryahooadd#yahoo.fr";
$mail->Password = "youryahoopw";
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail = new PHPMailer();
$body = "<h1>hello, world!</h1>"
$mail->IsSMTP(); // telling the class to use SMTP
$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 = 465; // 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!";
}

PHP mailer error

I tried to use php mailer but errors as follows.
SMTP -> FROM SERVER:
SMTP -> FROM SERVER:
SMTP -> ERROR: EHLO not accepted from server:
SMTP -> FROM SERVER:
SMTP -> ERROR: HELO not accepted from server:
SMTP -> ERROR: AUTH not accepted from server:
SMTP -> NOTICE: EOF caught while checking if connectedSMTP Error: Could not authenticate. Message could not be sent.
Mailer Error: SMTP Error: Could not authenticate.
and my code
<?php
require("class.phpmailer.php")
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPDebug = 2;
$mail->Username = "admin#xxxxxxxxxxxx.in";
$mail->Password = "xxxxxxxx";
$mail->From = "admin#xxxxxxxxxxxx.in";
$mail->FromName = "Mailer";
$mail->AddAddress("xxxx#yahoo.co.in", "mine");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$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. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
I was getting this due to wrong port for SSL.
SSL = 465
TLS = 587
See:
http://mail.google.com/support/bin/answer.py?hl=en&answer=13287
Some servers (especially shared hosting) will block you from using SSL with SMTP, I had the same problem once.
Either change host if you can, try using the default PHP mail() function or send through another mail server that does not require SSL e.g. port 25 not 465.
Something like AuthSMTP would be your best bet for an alternate mail server.
I had the same problems, it seems that we have
to set the SMPTSecure value.
First I changed the port from 465 to 587 and added:
$mail->SMTPSecure = "tls";
and it worked :)
If you are working in your localhost just go to the PHP Extention and enable or check the php_openssl
it will be able to access the SSL ports.
try this code
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 = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "admin#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "admin123";
$mail->setFrom('admin3#gmail.com', 'development'); //add sender email address.
$mail->addAddress('admins#gmail.com', "development"); //Set who the message is to be sent to.
//Set the subject line
$mail->Subject = $response->subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->Body = 'Name: '.$data['name'].'<br />Location: '.$data['location'].'<br />Email: '.$data['email'].'<br />Phone:'.$data['phone'].'<br />ailment: '.$data['ailment'].'<br />symptoms: '.$data['symptoms'];
//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.gif');
//$mail->SMTPAuth = true;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Had the same issue, Change port No in opencart mail setting to 587 and works fine
May be because of fire wall?
If you can't sign in to Google Talk,
or you're receiving an error that
says, Could not authenticate to
server, check if you have personal
firewall software installed, or if
your computer is behind a proxy server
that requires a username and password.
http://www.google.com/support/talk/bin/answer.py?hl=en&answer=30998
I use the same script for several clients and only run into this problem when deploying to Amazon EC2 cloud providers (such as Openshift).
These are tried and tested settings in phpmailer:
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
'but' Google blocks these services as an 'anti-spam' / political maneuver, and this has caught me out because it works locally and on most hosting providers, there is nothing much you can do when they don't accept outbound messages from your hosts DNS / IP. Accept it and move on by looking for another smtp server to route messages through.
not sure but try $mail->Host = "smtp.gmail.com" =>$mail->Host = "smtp.google.com"

Categories