PHPMailer do not work properly sometimes on gmail port 465 or 587 on localhost - phpmailer

I am using the latest version of PHPMailer 6.0.2 on LOCALHOST with Gmail SMTP Ports [ SSL on 465 and TLS 587 ]. It works well but not stable or works properly, very strange while OpenSSL Extension is also active in PHP Config /PHP.ini file.
It returns the error " SMTP Error: Could not connect to SMTP host ", sometimes on 465 or 587 port.
As right now it's on 587.
Here the exact error on port 587 ;
2017-12-05 13:00:26 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, ),)
2017-12-05 13:00:26 Connection: opened
2017-12-05 13:00:26 SMTP INBOUND: "220 smtp.gmail.com ESMTP f3sm245851pgt.15 - gsmtp"
2017-12-05 13:00:26 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP f3sm245851pgt.15 - gsmtp
2017-12-05 13:00:26 CLIENT -> SERVER: EHLO localhost
2017-12-05 13:00:27 SMTP INBOUND: "250-smtp.gmail.com at your service, [110.36.136.72]"
2017-12-05 13:00:27 SMTP INBOUND: "250-SIZE 35882577"
2017-12-05 13:00:27 SMTP INBOUND: "250-8BITMIME"
2017-12-05 13:00:27 SMTP INBOUND: "250-STARTTLS"
2017-12-05 13:00:27 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2017-12-05 13:00:27 SMTP INBOUND: "250-PIPELINING"
2017-12-05 13:00:27 SMTP INBOUND: "250-CHUNKING"
2017-12-05 13:00:27 SMTP INBOUND: "250 SMTPUTF8"
2017-12-05 13:00:27 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [110.36.136.72]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-12-05 13:00:27 CLIENT -> SERVER: STARTTLS
2017-12-05 13:00:27 SMTP INBOUND: ""
2017-12-05 13:00:27 SERVER -> CLIENT:
2017-12-05 13:00:27 SMTP ERROR: STARTTLS command failed:
SMTP Error: Could not connect to SMTP host.
2017-12-05 13:00:27 SMTP NOTICE: EOF caught while checking if connected
2017-12-05 13:00:27 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
HERE IS THE RELEVANT CODE:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use League\OAuth2\Client\Provider\Google;
require '../../vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 4;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->SMTPAutoTLS = false;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Username = $from_address;
$mail->Password = $from_password;
$mail->SetLanguage("tr", "phpmailer/language");
$mail->CharSet = "utf-8";
$mail->Encoding = "base64";
$mail->SetFrom($from_address, $from_name);
foreach ($to_email_list as $to) {
$mail->AddAddress($to);
}
$mail->AddReplyTo($from_address, $from_name);
$mail->Subject = $email_subject;
//Creating Email Body
$message = "<html>\n";
$message .= "<body>\n";
$message .= '<p>Greetings,</p>';
$message .= '<p>' . $email_message . '</p>';
$message .= "</body>\n";
$message .= "</html>\n";
$mail->isHTML(true);
$mail->MsgHTML($message);
if(!$mail->Send()) {
echo "On Port: " . $from_smtp_port . " </br> Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent! on Port " . $from_smtp_port . "</br>";
foreach($to_email_list as $list){
echo $list . "</br>";
}
}

Finally, I found the solution on troubleshooting guide.
I recommend to other users who are facing the same issue; please see following Points carefully on troubleshooting guide;
- Opportunistic TLS
- PHP 5.6 certificate verification failure
- cURL error 60
My Script is working fine with Gmail ss/465 and tls/587.
Thanks! #Synchro.

Related

Azure Function app cannot connect to on-premise SMTP server

I'm using Azure Functions to email an Excel file. I use the npm package NodeMailer to send the email. When I try to send the email I get this error in the Function app logs:
Result: Failure Exception: connect ETIMEDOUT [SMTP server IP address]:50025 Stack: Error: connect ETIMEDOUT [SMTP server IP address]:50025 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)
Here is part of the app code that sends the email:
// Send the email
const transporter = nodemailer.createTransport({
host: "[SMTP server IP address]",
port: 50025,
secure: false,
ignoreTLS: true,
connectionTimeout: 180000,
debug: true
});
let mailOptions = { .... }
context.log('Sending email');
let info = await transporter.sendMail({mailOptions, function(error, info) {
if (error) {
context.log("sent mail error: " +error.message);
}
context.log(info.response);
}
});
The 'Sending email' line prints out in the logs so I know the code gets that far. However, the code doesn't print out 'sent email error ...'.
I'm using an on-premises SMTP server that is hosted on a private network. I whitelisted the Azure Function app's outbound IP addresses so the app can access the SMTP server. The request from the Function app is not hitting the firewall on the private network(ie none of the outbound IPs are hitting the firewall).
I've tried tweaking the nodemailer configuration, like increasing the connection timeout time and enabling TLS with no difference.
I don't know what else to try. Is there any way to solve this?

PHPMailer Connection --> Error #2: stream_socket_enable_crypto(): SSL: Success

Getting a very weird connection error configuring a new PHPMAILER SMTP end-point. PHPMailer code,is below. I am able to connect with the same set of credentials with Thunderbird, and hence the auth and server settings info is correct, as mentioned in the PHPMailer script. With PHPMailer, getting the error,
Connection failed. Error #2: stream_socket_enable_crypto(): SSL:
Success
[/var/www/html/phpmailer/vendor/phpmailer/phpmailer/src/SMTP.php line
468] SMTP Error: Could not connect to SMTP host.
Has anyone experienced this same issue?
Running an Ubuntu 20.04 server with PHP 7.4.
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.example.com'; //Set the SMTP server to send through
$mail->SMTPDebug = 5;
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'test#example.com'; //SMTP username
$mail->Password = 'secretpassword'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption
// $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//$mail->SMTPKeepAlive = true;
//Recipients
$mail->setFrom('test#example.com', 'Mailer');
$mail->addAddress('receiver#test.com', 'Joe User'); //Add a recipient
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$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';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
My SMTPDebug output
2022-02-28 14:26:53 Connection: opening to smtp.example.com:587, timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, ),)
2022-02-28 14:26:53 Connection: opened
2022-02-28 14:26:53 SMTP INBOUND: "220 example.local Microsoft ESMTP MAIL Service ready at Mon, 28 Feb 2022 15:30:07 +0100"
2022-02-28 14:26:53 SERVER -> CLIENT: 220 example.local Microsoft ESMTP MAIL Service ready at Mon, 28 Feb 2022 15:30:07 +0100
2022-02-28 14:26:53 CLIENT -> SERVER: EHLO example.website.com
2022-02-28 14:26:53 SMTP INBOUND: "250-example.local Hello [123.123.123.123]"
2022-02-28 14:26:53 SMTP INBOUND: "250-SIZE 62914560"
2022-02-28 14:26:53 SMTP INBOUND: "250-PIPELINING"
2022-02-28 14:26:53 SMTP INBOUND: "250-DSN"
2022-02-28 14:26:53 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2022-02-28 14:26:53 SMTP INBOUND: "250-STARTTLS"
2022-02-28 14:26:53 SMTP INBOUND: "250-X-ANONYMOUSTLS"
2022-02-28 14:26:53 SMTP INBOUND: "250-AUTH LOGIN"
2022-02-28 14:26:53 SMTP INBOUND: "250-X-EXPS GSSAPI NTLM"
2022-02-28 14:26:53 SMTP INBOUND: "250-8BITMIME"
2022-02-28 14:26:53 SMTP INBOUND: "250-BINARYMIME"
2022-02-28 14:26:53 SMTP INBOUND: "250-CHUNKING"
2022-02-28 14:26:53 SMTP INBOUND: "250 XRDST"
2022-02-28 14:26:53 SERVER -> CLIENT: 250-example.local Hello [123.123.123.123]250-SIZE 62914560250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-X-ANONYMOUSTLS250-AUTH LOGIN250-X-EXPS GSSAPI NTLM250-8BITMIME250-BINARYMIME250-CHUNKING250 XRDST
2022-02-28 14:26:53 CLIENT -> SERVER: STARTTLS
2022-02-28 14:26:53 SMTP INBOUND: "220 2.0.0 SMTP server ready"
2022-02-28 14:26:53 SERVER -> CLIENT: 220 2.0.0 SMTP server ready
2022-02-28 14:26:53 Connection failed. Error #2: stream_socket_enable_crypto(): SSL: Success [/var/www/html/phpmailer/vendor/phpmailer/phpmailer/src/SMTP.php line 468]
SMTP Error: Could not connect to SMTP host.
2022-02-28 14:26:53 CLIENT -> SERVER: QUIT
2022-02-28 14:26:53 SERVER -> CLIENT:
2022-02-28 14:26:53 SMTP ERROR: QUIT command failed:
2022-02-28 14:26:53 Connection: closed
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

Emails stopped sending after the project was deployed on a real hosting

I deployed the project on the real host, and email stopped sending. It works on the local host.
Here is the nodemailer settings:
var smtpTransport = nodeMailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
auth: {
user: "here is my gmail",
pass: "here is password"
}
});
Here is the data that was sent to me from the hosting:
List of POP3 IMAP and SMTP mail servers
Webmail: https://mail.ye4wrc7dna.nlnode.webrahost.eu
POP3/IMAP Server:
mail.webrahost.com
Port: 993
TLS/SSL
Outgoing Server (SMTP):
mail.webrahost.com
Port: 465
Connection security: SSL/TSL
(this server requires an encrypted connection)
The request comes to the server (checked through console.log), but the email is not sent. How to set up correctly?
Thanks to everyone
The problem was solved in the following way. Technical support created an account at http://mail.webrahost.com/ for me, and then I entered this data into the settings of nodemailer.
var smtpTransport = nodeMailer.createTransport({
host: "mail.webrahost.eu",
port: 465,
secure: true,
auth: {
user: "my webrahost email",
pass: "my password"
}
});
Now email is working

sending email using nodemailer from godaddy email

here is my code to integrate godaddy email using nodemailer
var transporter = nodeMailer.createTransport({
service: 'Godaddy',
host: "smtpout.secureserver.net",
secureConnection: true,
port: 465,
auth: {
user: "***",
pass: "***"
}
});
it gives error i have also tried using port 80 and smtpout.asia.secureserver.net it also does not work below is my godaddy gmail settings
Server Standard ports SSl
Incoming server (IMAP):
imap.asia.secureserver.net 143 993
Incoming server (POP3):
pop.asia.secureserver.net 110 995
Outgoing server (SMTP):
smtpout.asia.secureserver.net 80,3535,25 465

smtp send mail via postfix and nodejs nodemailer connection closed

i have problem of remote sending via smtp server
using nodejs module nodemailer
host: 'mail.5cpa.ru'
port: 587
secure: false (true tried also)
smtp server is mine, and i can send mails through thenderbird, using same setting
log
server 220 5cpa.ru ESMTP Postfix (Ubuntu)
client EHLO vonica
server 250-5cpa.ru
250-PIPELINING
250-SIZE 15728640
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
client STARTTLS
server 220 2.0.0 Ready to start TLS
socket Closing connection to the server
thanks!
I had similar problem (maybe the same) and I figure out that this happens when i use self signed SSL certificate.
I fixed this by adding "rejectUnauthorized" option to tls:
var transporter = nodemailer.createTransport(smtpTransport({
host: host,
port: 25,
auth: {
user: user,
pass: pass
},
tls:{
rejectUnauthorized: false
}
}));
The error is applied to the callback of "sendMail" function and is not emitted as 'error'.
I don't have enough reputation to reply above, but you could also check the latest syntax from Nodemailer, ie:
const smtpConfig = {
host: 'smtp.gmail.com',
port: 465,
secure: true, // use SSL
auth: {
user: 'user#gmail.com',
pass: 'pass'
}
}
note:
secure: true // use SSL
You might not need that TLS work-around if you tell it to use SSL.
Reference: https://nodemailer.com/2-0-0-beta/setup-smtp/
Mine is currently running off postfix and mailx, and I only had to put in the hostname:
const transporter = nodemailer.createTransport({
name: 'localhost'
})
That should work as long as you can perform this test (type in shell):
echo "Email Body Test" | mail -s "Email Subject Test" you#yourdomain.com
If it doesn't work, investigate your firewall, port 25 (probably not the issue in this case, but in other similar cases).
Also confirm the user running node has permission to use the mail program.

Resources