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 trying to setup PHP mailer on my VPS with Plesk SMTP.
The PHP code
require("path_to_phpmailer");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
//Server settings
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->CharSet = 'UTF-8';
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->SMTPSecure = 'tls';
$mail->SMTPOptions = array (
'ssl' => array(
'verify_peer' => true,
'verify_depth' => 3,
'allow_self_signed' => true,
'peer_name' => 'Plesk',
)
);
$mail->SMTPAuth = true;
$mail->Username = 'support#mywebsite.com';
$mail->Password = 'somepassword';
$mail->setFrom('support#mywebsite.com', 'Name Surname');
$mail->addAddress('someadress#web.de', 'Name Surname');
$mail->Subject = 'Email subject';
$mail->msgHTML('Email content with <strong>html</strong>');
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
support#mywebsite.com is the email that i created in my ples dashboard and somepassword is its password. However running the script gives the following output:
2018-04-14 09:27:10 SERVER -> CLIENT: 220 myserver.net ESMTP
Postfix (Ubuntu)
2018-04-14 09:27:10 CLIENT -> SERVER: EHLO mywebsite.com
2018-04-14 09:27:10 SERVER -> CLIENT: 250-myserver.net250-
PIPELINING250-SIZE 10240000250-ETRN250-STARTTLS250-AUTH DIGEST-MD5 CRAM-MD5
PLAIN LOGIN250-XFORWARD NAME ADDR PROTO HELO SOURCE PORT IDENT250-
ENHANCEDSTATUSCODES250-8BITMIME250 DSN
2018-04-14 09:27:10 CLIENT -> SERVER: STARTTLS
2018-04-14 09:27:10 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2018-04-14 09:27:10 CLIENT -> SERVER: QUIT
2018-04-14 09:27:10 SERVER -> CLIENT:
2018-04-14 09:27:10 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/TroubleshootingMessage has been
sent
Did I understand something wrong?
Thank you in advance!
Check if is a problem with ssl, and try again
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
if it still has an error
$mail->SMTPDebug = 3;
https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging
This question already has answers here:
PHPMailer - SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
(4 answers)
Closed 3 years ago.
PHPMailer is not working and throwing following error.
2017-12-04 13:34:14 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP y19sm20173980pgv.19 - gsmtp
2017-12-04 13:34:14 CLIENT -> SERVER: EHLO solutions.proprompt.com
2017-12-04 13:34:14 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.58.144.11]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-12-04 13:34:14 CLIENT -> SERVER: STARTTLS
2017-12-04 13:34:14 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2017-12-04 13:34:14 CLIENT -> SERVER: QUIT
2017-12-04 13:34:15
2017-12-04 13:34:15
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.
It was working properly before. I have used following lines of code:
$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 = 'some#email.com'; // SMTP username
$mail->Password = 'somepassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
Can anyone tell me where it went wrong?
After hours of struggling , I found your answer to be correct,
Those who are facing issues with PHPMailer and godaddy linux hosting server have to some simple changes ,
host = 'localhost'
and add the below lines
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
should work
Following lines of code solved this issue.
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
Followed code worked for me:
$mail = new PHPMailer(true);
$mail->isSMTP();// Set mailer to use SMTP
$mail->CharSet = "utf-8";// set charset to utf8
$mail->SMTPAuth = true;// Enable SMTP authentication
$mail->SMTPSecure = 'tls';// Enable TLS encryption, `ssl` also accepted
$mail->Host = 'smtp.gmail.com';// Specify main and backup SMTP servers
$mail->Port = 587;// TCP port to connect to
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->isHTML(true);// Set email format to HTML
$mail->Username = 'Sender Email';// SMTP username
$mail->Password = 'Sender Email Password';// SMTP password
$mail->setFrom('MAIL_USERNAME', 'Test');//Your application NAME and EMAIL
$mail->Subject = 'Test';//Message subject
$mail->MsgHTML('HTML code');// Message body
$mail->addAddress('User Email', 'User Name');// Target email
$mail->send();
I have installed PHPMailer using Composer and added all the Composer dependency to use Google XOAuth2 authentication.
Then I have followed some tutorials to get the refresh token with the Gmail API.
Everything should be working just fine. I did all the paper work right. Right?!
Despite my best intention and all the docs, I'm not able to establish a SMTP connection to smtp.gmail.com
I get this error:
2017-10-20 18:01:45 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP c17sm2715728wrg.26 - gsmtp
2017-10-20 18:01:45 CLIENT -> SERVER: EHLO localhost
2017-10-20 18:01:45 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [151.61.40.58]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-10-20 18:01:45 CLIENT -> SERVER: STARTTLS
2017-10-20 18:01:45 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2017-10-20 18:01:45 CLIENT -> SERVER: QUIT
2017-10-20 18:01:45
2017-10-20 18:01:45
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
My code is straight from the PHPMailer Gmail XOAUTH example.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use League\OAuth2\Client\Provider\Google;
date_default_timezone_set('Europe/Rome');
require 'lib/php/vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->AuthType = 'XOAUTH2';
$email = 'seckrit-stuff';
$clientId = 'seckrit-stuff';
$clientSecret = 'seckrit-stuff';
$refreshToken = 'seckrit-stuff';
$provider = new Google(
[
'clientId' => $clientId,
'clientSecret' => $clientSecret,
]
);
$mail->setOAuth(
new OAuth(
[
'provider' => $provider,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'refreshToken' => $refreshToken,
'userName' => $email,
]
)
);
$mail->setFrom($email, 'Matteo Bini');
$mail->addAddress('seckrit-stuff', 'Matteo Bini');
$mail->Subject = 'PHPMailer GMail XOAUTH2 SMTP test';
$mail->CharSet = 'utf-8';
$mail->msgHTML('<strong>HTML</strong> message!');
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Could you please help me finding a way to use PHPMailer with Gmail in localhost (xampp)?
This is nothing to do with OAuth. You’ve got a much earlier issue at the TLS level. Either you’re missing the openssl extension, it’s misconfigured, or your CA certificates are out of date.
This issue is covered in the troubleshooting guide the error links to - check using openssl.
when i try to send mail by phpmailer,there is some error.
Like this:
SMTP -> FROM SERVER: 250-m1.mydomain.com
250-PIPELINING
250-SIZE 15728640
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
CLIENT -> SMTP: STARTTLS
SMTP -> FROM SERVER:220 2.0.0 Ready to start TLS
CLIENT -> SMTP: EHLO localhost.localdomain
250-PIPELINING
250-SIZE 15728640
250-VRFY
250-ETRN
250-AUTH PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
SMTP -> ERROR: AUTH not accepted from server: 535 5.7.8 Error: authentication failed: Invalid authentication mechanism
CLIENT -> SMTP: RSET
SMTP -> FROM SERVER:250 2.0.0 Ok
CLIENT -> SMTP: MAIL FROM:
SMTP -> FROM SERVER:250 2.1.0 Ok
CLIENT -> SMTP: RCPT TO:
SMTP -> FROM SERVER:554 5.7.1 : Relay access denied
SMTP -> ERROR: RCPT not accepted from server: 554 5.7.1 : Relay access denied
CLIENT -> SMTP: quit
SMTP -> FROM SERVER:221 2.0.0 Bye
And my php code like this:
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Host = "m1.mydomain.com";
$mail->Port = 25;
$mail->Username = "service#mydomain.com";
$mail->Password = "password";
$mail->SetFrom('service#mydomain.com', 'service');
$mail->Subject = "Fine Design - Avise me";
$mail->Body = "This is very interest email for you";
$mail->AddAddress("to#domain.com","name");
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo "We're sorry, however, an error has occurred. You may manually e-mail us .";
return false;
} else {
echo "Thanks! Your message was successfully sent.";
return true;
}
?>
but i could send mail by foxmail and other client,
Did anybody could give me some suggestion?
You are using tls on port 25.
That normally should not work as port 25 is normally used for smtp without ssl/tls.
Try to test it with port 25, and with port 587.
Add:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
I used it and works fine without SSL.