I'm trying to send an e-mail with PHPMailer to my SMTP server hosted with Mail-In-A-Box (which uses Postfix) on another Ubuntu 16.04 VPS, but I have an error
The mail vps is hosted on Digital Ocean, same for the website/php one
This is what I get when I run the code:
2019-02-26 17:54:01 CLIENT -> SERVER: EHLO mail
2019-02-26 17:54:01 SERVER -> CLIENT: 250-mail.ultracore.it
250-PIPELINING
250-SIZE 134217728
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250 SMTPUTF8
2019-02-26 17:54:01 CLIENT -> SERVER: STARTTLS
2019-02-26 17:54:01 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2019-02-26 17:54:01 SMTP Error: Could not connect to SMTP host.
2019-02-26 17:54:01 CLIENT -> SERVER: QUIT
2019-02-26 17:54:01 SERVER -> CLIENT:
2019-02-26 17:54:01 SMTP ERROR: QUIT command failed:
2019-02-26 17:54:01 SMTP Error: Could not connect to SMTP host.
This is the code I use to send the email:
function sendEmail($from, $password, $email, $subject, $messageHtml, $message)
{
$mail = new PHPMailer(true);
//$pop = POP3::popBeforeSmtp('pop3.ultracore.it', 110, 1, $from, $password, 2);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.ultracore.it'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $from; // SMTP username
$mail->Password = $password; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom($from, $from);
$mail->addAddress($email, 'Joe User'); // Add a recipient
$mail->addReplyTo('support#ultracore.it', 'Support');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $messageHtml;
$mail->AltBody = $message;
$mail->send();
echo 'The message has been sent';
} catch (Exception $e) {
echo $mail->ErrorInfo;
}
}
It's the PHPMailer's example modified.
As you can see I tried using the popBeforeSMTP function, but I had the same error, and another one before it:
<pre>Connecting to the POP3 server raised a PHP warning:errno: 2 errstr: fsockopen(): unable to connect to pop3.ultracore.it:110 (Connection refused); errfile: /var/www/html/vendor/phpmailer/phpmailer/src/POP3.php; errline: 238</pre><pre>Connecting to the POP3 server raised a PHP warning:errno: 2 errstr: fsockopen(): unable to connect to pop3.ultracore.it:110 (Connection refused); errfile: /var/www/html/vendor/phpmailer/phpmailer/src/POP3.php; errline: 238Failed to connect to server pop3.ultracore.it on port 110. errno: 111; errstr: Connection refused</pre>
I tried with ssl instead of tls, but I'm receiving this:
2019-02-26 18:03:52 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshootingr`
I know some other user have asked a question like this, but none of the solutions I've tried worked for me.
I've imported PHPMailer through the composer with this line
"require": {
"phpmailer/phpmailer": "~6.0"
}
Update
I changed the code to this (still coming from the PHPMailer examples)
function sendEmail($from, $password, $to, $subject, $messageHtml)
{
$smtp_settings = array(
"debug" => 2,
"host" => "smtp.ultracore.it",
"port" => 587,
"auth" => true,
"encryption" => "tls",
"reply_address" => "support#ultracore.it",
"peer_name" => "smtp.ultracore.it",
"verify_peer" => true,
"verify_depth" => 3,
"allow_self_signed" => false,
"cafile" => "/var/www/html/certificates/ssl_private_key.pem"
);
$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 = $smtp_settings['debug'];
//Set the hostname of the mail server
$mail->Host = $smtp_settings['host'];
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = $smtp_settings['port'];
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = $smtp_settings['encryption'];
//Custom connection options
//Note that these settings are INSECURE
$mail->SMTPOptions = array(
'ssl' => [
'verify_peer' => $smtp_settings['verify_peer'],
'verify_depth' => $smtp_settings['verify_depth'],
'allow_self_signed' => $smtp_settings['allow_self_signed'],
'peer_name' => $smtp_settings['peer_name'],
'cafile' => $smtp_settings['cafile'],
],
);
//Whether to use SMTP authentication
$mail->SMTPAuth = $smtp_settings['auth'];
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $from;
//Password to use for SMTP authentication
$mail->Password = $password;
//Set who the message is to be sent from
$mail->setFrom($from, 'First Last');
//Set who the message is to be sent to
$mail->addAddress($to, 'John Doe');
//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
$mail->msgHTML($messageHtml, __DIR__);
//Send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
}
But I have the same error
2019-02-26 21:48:23 SERVER -> CLIENT: 220 mail.ultracore.it ESMTP Hi, I'm a Mail-in-a-Box (Ubuntu/Postfix; see https://mailinabox.email/)
2019-02-26 21:48:23 CLIENT -> SERVER: EHLO ultracore.it
2019-02-26 21:48:23 SERVER -> CLIENT: 250-mail.ultracore.it250-PIPELINING250-SIZE 134217728250-VRFY250-ETRN250-STARTTLS250-ENHANCEDSTATUSCODES250-8BITMIME250-DSN250 SMTPUTF8
2019-02-26 21:48:23 CLIENT -> SERVER: STARTTLS
2019-02-26 21:48:23 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2019-02-26 21:48:23 CLIENT -> SERVER: QUIT
2019-02-26 21:48:23 SERVER -> CLIENT:
2019-02-26 21:48:23 SMTP ERROR: QUIT command failed:
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I tried switching the cafile to all of this files:
files I tried
Am I doing this wrong? The files are on the website's VPS, I've applied the certificates to the server through Mail-In-A-Box's admin panel.
I have CloudFlare, but the address smtp.ultracore.it is not under cloudflare's protection, and all the certificates are created on the Mail VPS' address, so ultracore.it is created with the address of mail.ultracore.it, not the website's one...
The code
echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."<br>";
returns this
SSL loaded
You're using SMTPSecure = 'ssl' (implicit TLS) on port 587, which expects explicit TLS (STARTTLS). This is covered in all the examples and the docs.
When STARTTLS fails, it's most likely due to an invalid certificate, or an invalid CA certificate, and again, that is covered in the troubleshooting guide.
Don't use POP-before-SMTP; that's a really ancient auth mechanism and should not be used.
I fixed setting "verify_peer" to false, so my code looks like this now:
function sendEmail($from, $password, $to, $subject, $messageHtml)
{
$smtp_settings = array(
"debug" => 2,
"host" => "mail.ultracore.it",
"port" => 587,
"auth" => true,
"encryption" => "tls",
"reply_address" => "support#ultracore.it",
"peer_name" => "mail.ultracore.it",
"verify_peer" => false,
"verify_depth" => 3,
"allow_self_signed" => true,
"cafile" => "/var/www/html/certificates/ssl_certificate.pem"
);
$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 = $smtp_settings['debug'];
//Set the hostname of the mail server
$mail->Host = $smtp_settings['host'];
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = $smtp_settings['port'];
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = $smtp_settings['encryption'];
//Custom connection options
//Note that these settings are INSECURE
$mail->SMTPOptions = array(
'ssl' => [
'verify_peer' => $smtp_settings['verify_peer'],
'verify_depth' => $smtp_settings['verify_depth'],
'allow_self_signed' => $smtp_settings['allow_self_signed'],
'peer_name' => $smtp_settings['peer_name'],
'cafile' => $smtp_settings['cafile'],
],
);
//Whether to use SMTP authentication
$mail->SMTPAuth = $smtp_settings['auth'];
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $from;
//Password to use for SMTP authentication
$mail->Password = $password;
//Set who the message is to be sent from
$mail->setFrom($from, 'First Last');
//Set who the message is to be sent to
$mail->addAddress($to, 'John Doe');
//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
$mail->msgHTML($messageHtml, __DIR__);
//Send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
}
I'm getting an EOF error. I'm using the basic template with a few subtractions (attachments, cc, etc) and one addition (AuthType='PLAIN').
<?php
require __DIR__ . '/vendor/autoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.zoho.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->AuthType = 'PLAIN';
$mail->Username = '*******'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('*************', '*****');
$mail->addAddress('*********', '******'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'From phpmailer-test.php';
$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've tried STMP via 465 both with this script and with telnet and gotten the same error.
-I've tested this with tls and 587 with this script (but not telent) and gotten this error.
-I'm using composer, per the Troubleshooting page's instructions.
-Script is at phillyguitarlessons.com, mail account is at *#kalegood.com (hosted by zoho.com). I don't think this would cause an error, but am not sure.
-I've tried looking through the docs (via a page search for "EOF") and found nothing.
-From my VPS, my ports should be wide-open. To zoho.com
-Opensssl is activated (confirmed via info.php)
Output (switched to debug level 2):
2016-10-03 13:47:40 SERVER -> CLIENT: 220 mx.zohomail.com SMTP Server ready October 3, 2016 6:47:40 AM PDT
2016-10-03 13:47:40 CLIENT -> SERVER: EHLO phillyguitarlessons.com
2016-10-03 13:47:40 SERVER -> CLIENT: 250-mx.zohomail.com Hello phillyguitarlessons.com (162.243.32.109 (162.243.32.109)) 250-STARTTLS 250 SIZE 53477376
2016-10-03 13:47:40 CLIENT -> SERVER: STARTTLS
2016-10-03 13:47:40 SERVER -> CLIENT: 220 Ready to start TLS.
2016-10-03 13:47:41 CLIENT -> SERVER: EHLO phillyguitarlessons.com
2016-10-03 13:47:41 SERVER -> CLIENT: 250-mx.zohomail.com Hello phillyguitarlessons.com (162.243.32.109 (162.243.32.109)) 250-AUTH LOGIN PLAIN 250 SIZE 53477376
2016-10-03 13:47:41 CLIENT -> SERVER: AUTH PLAIN
2016-10-03 13:49:46 SERVER -> CLIENT: 334
2016-10-03 13:49:46 SMTP NOTICE: EOF caught while checking if connected
2016-10-03 13:49:46 SMTP Error: Could not authenticate.
2016-10-03 13:49:46 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I want to know where the problem is because I get authentication error.
I already have a few gmail accounts,
I created a new one for my website,
I added a new email in "Account and import" to my old gmail account.
I copied my new email username and password to phpmailer code
include_once('phpmailer/class.phpmailer.php');
include_once('phpmailer/class.smtp.php');
//6nb5Drv;
function sendmail(){
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->Port = 587; // Set the SMTP port i tried and 457
$mail->Username = 'newmail#gmail.com'; // SMTP username
$mail->Password = 'newmailpass'; // SMTP password
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->From = 'from#yahoo.com';
$mail->FromName = 'From';
$mail->AddAddress('to#gmail.com', 'To'); // Add a recipient
$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';
}
sendmail();
But I get an authentication error.
What is wrong?
Probably something with the credentials, how do I configure gmail smtp?
Debug report:
2015-12-04 17:56:15 CLIENT -> SERVER: EHLO www.site.co
2015-12-04 17:56:15 CLIENT -> SERVER: STARTTLS 2015-12-04
17:56:15 CLIENT -> SERVER: EHLO www.site.co 2015-12-04
17:56:15 CLIENT -> SERVER: AUTH LOGIN 2015-12-04 17:56:15 CLIENT ->
SERVER: UHJlZGljdG9sb2d5 2015-12-04 17:56:15 CLIENT -> SERVER:
U2dHZlB0VHZUbTZ1SW9ZMi1qTlNCQQ== 2015-12-04 17:56:17 SMTP ERROR:
Password command failed: 435 4.7.8 Error: authentication failed:
UGFzc3dvcmQ6 2015-12-04 17:56:17 SMTP Error: Could not authenticate.
2015-12-04 17:56:17 CLIENT -> SERVER: QUIT 2015-12-04 17:56:17 SMTP
connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message
could not be sent.Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
According to this post:
PHPMailer - SMTP ERROR: Password command failed when send mail from my server, in some case, you have to specify google that this is not a suspicious activity and activate some less secure option in your account.
Assuming this is the right password...
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?
I'm trying to send email over Mandrill with PHPMailer but withouth sucess (testing on localhost).
Can someone tell me where is the problem?
This is the verbose information from PHPMailer:
2014-04-27 17:51:06 SERVER -> CLIENT: 220 smtp.mandrillapp.com ESMTP
2014-04-27 17:51:06 CLIENT -> SERVER: EHLO 127.0.0.1
2014-04-27 17:51:06 SERVER -> CLIENT: 250-ip-10-107-129-238
250-PIPELINING
250-SIZE 26214400
250-STARTTLS
250-AUTH PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250 8BITMIME
2014-04-27 17:51:06 CLIENT -> SERVER: STARTTLS
2014-04-27 17:51:06 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2014-04-27 17:51:06 CLIENT -> SERVER: QUIT
2014-04-27 17:51:16 SERVER -> CLIENT:
2014-04-27 17:51:16 SMTP ERROR: QUIT command failed: SMTP connect() failed. Message could not be sent.
Mailer Error: SMTP connect() failed.
Here is my code:
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#mail.com'; // yes, I have entered my username mail
$mail->Password = 'xxxxxxxxxxxx-xxxxxxxxx'; // yes, API key is here
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Your From name';
if($test_mode) {
$mail->SMTPDebug = 2;
$mail->AddAddress('mymail#gmail.com');
} else {
$mail->AddAddress($email);
}
$mail->IsHTML(true); // Set email format to HTML
$mail->WordWrap = 70; // Set word wrap to 70 characters
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->Subject = '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()) {
//redirect to
echo 'Message could not be sent.<br>';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
EDIT:
I can delete this:
$mail->SMTPSecure = 'tls';
and it will work! But now the encryption is disabled. Why is this not working with encryption enabled?
Uncoment php_openssl.dll in php.ini
Is the timezone set correctly?
include timezone definitions before loading phpmailer
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
$mail = new PHPMailer();
Also, if you're trying it from a gmail domain, it ught have been blocked. check at: https://security.google.com/settings/security/activity?hl=pt_BR