Nodemailer to GMAIL Error: self-signed certificate in certificate chain - node.js

It is a GMAIL server my two-step password is correct and my recipient data is correct
"use strict";
const nodemailer = require("nodemailer");
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
//let testAccount = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com", //Servidor de gmail
port: 465, //Puerto de servidor de gmail
secure: true, //Esta activado en true // true for 465, false for other ports
auth: {
user: ' ****#gmail.com',/*testAccount.user,*/ // generated ethereal user
pass: 'qskafnvgyhzyrceb'/*testAccount.pass,*/ // generated ethereal password
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '*****#gmail.com',/*'"Fred Foo 👻" <foo#example.com>',*/ // sender address
to: '*****#gmail.com',/*"bar#example.com, baz#example.com",*/ // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<h1>Hello World</h1>", // html body
});
}
main().then(() => { console.log('Correo Enviado') }).catch((err)=>{ console.log(err) }/*console.error*/);`
but I have this error
Error: self-signed certificate in certificate chain
at TLSSocket.onConnectSecure (node:_tls_wrap:1538:34)
at TLSSocket.emit (node:events:513:28)
at TLSSocket._finishInit (node:_tls_wrap:952:8)
at ssl.onhandshakedone (node:_tls_wrap:733:12) {
code: 'ESOCKET',
command: 'CONN'
}
I want send any email to gmail acount,
this code worked for me last week

Related

I got an error, after add my cpanel smtp mail service with nodemailer,

I got this error please help me , i am stuck , its happening with my cpanel smtp server.
This is error in my terminal:
Error: connect ETIMEDOUT 76.76.21.21:465
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
errno: -4039,
code: 'ESOCKET',
}
My Code:
// async..await is not allowed in global scope, must use a wrapper
async function main() {
let transporter = nodemailer.createTransport({
host: "workbes.com",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: 'customer#workbes.com', // generated ethereal user
pass: 'mypassword', // generated ethereal password
},
tls: {
rejectUnauthorized:false
}
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Fred Foo 👻" <foo#example.com>', // sender address
to: "almafuz97#gmail.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
});
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);

nodemailer is causing an error in aws ec2 production evnironment

I am sending emails in node.js using nodemailer and I have this configuration to send emails.
var transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port : 465,
secure: true,
auth: {
user: 'myemail#gmail.com',
pass: 'password'
}
});
const mailOptions = {
from: 'myemail#gmail.com', // sender address
to: user.company_email, // list of receivers
subject: 'EnStream New Account Signup Password', // Subject line
html: `<p style="font-size : 15px;">Please sing in to your en-stream account here http://demo.en-stream.com/auth/login with this password : ${userPassword}</p>`// plain text body
};
It's sending emails on local environment correctly but in production environment on Aws Ec2 it is not sending email and throwing this error
code: "EAUTH"
command: "AUTH PLAIN"
response: "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbua↵534-5.7.14 qup7zOOL4GtmYEf1HEHBASSSBgbuMNJRQqK2v3X9mcxDRX1SrXU2Y_LSqZm7Y7yJvH2PwL↵534-5.7.14 JZW6iSXxsRhBdntFAAamrvitpdKS_YJiE-pEnXWakITAz1PAzwDMxjJPKntQrLl2Qx-xA1↵534-5.7.14 zZ4aTvKvYOAk85YHwABnnd0wHU2HkUeHPoDYqgXUWgSA_8Rrn4xkIsUN> Please log↵534-5.7.14 in via your web browser and then try again.↵534-5.7.14 Learn more at↵534 5.7.14 https://support.google.com/mail/answer/78754 a11sm34494120wrx.5 - gsmtp"
responseCode: 534
I allowed outbound port 465 in my ec2 instance security group as well.

Send Email Using Microsoft 365 Email Server In NodeJS

let transporter = nodemailer.createTransport({
service: "Outlook365",
host: 'smtp.office365.com',
port: 587,
tls: {
ciphers:'SSLv3'
},
auth: {
user: 'username',
pass: 'password'
}
});
I have an EAUTH error while sending an email, please check the image for error.
[1]: https://i.stack.imgur.com/snt3T.jpg
This code should do what you wish, you'll need to set your password to test this.
If the password is incorrect, you'll get an error:
Error: Invalid login: 535 5.7.3 Authentication unsuccessful message.
const nodemailer = require('nodemailer');
// Set this from config or environment variable.
const PASSWORD = '....';
async function send365Email(from, to, subject, html, text) {
try {
const transportOptions = {
host: 'smtp.office365.com',
port: '587',
auth: { user: from, pass: PASSWORD },
secureConnection: true,
tls: { ciphers: 'SSLv3' }
};
const mailTransport = nodemailer.createTransport(transportOptions);
await mailTransport.sendMail({
from,
to,
replyTo: from,
subject,
html,
text
});
} catch (err) {
console.error(`send365Email: An error occurred:`, err);
}
}
send365Email("from#example.com", "to#example.com", "Subject", "<i>Hello World</i>", "Hello World");
You will need to disable the SmtpClientAuthenticationDisabled through the online power shell with the command
Set-TransportConfig -SmtpClientAuthenticationDisabled $false
You can check for more information at Enable or disable authenticated client SMTP submission (SMTP AUTH) in Exchange Online
You can find the detailed information here:
https://developer.microsoft.com/en-us/graph/quick-start?code=M.R3_BAY.822b5ade-d816-85bb-ec94-8c349cdfca4b&state=option-node

unable to send mail using nodemailer node.js api?

I have written code to send gmail using nodemailer node.js rest api. this is working fine for my local system server but when the same code i deployed on windows-2000 server, i am unable to send gmail and getting internal server error.
this is the code snippet i am using --
var nodemailer = require('nodemailer');
var stringify = require('json-stringify');
// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port:587,
secure: false,
auth: {
user: 'user#gmail.com',
pass: 'user#123'
}
});
// setup e-mail data with unicode symbols
module.exports={
create:function(req,res){
var params=req.allParams();
var order_details=JSON.stringify(params);
//console.log("order-->"+order_details);
var mailOptions = {
from: 'user#gmail.com', // sender address
to: 'user1#gmail.com', // list of receivers
subject: "g-mail message ", // Subject line
html: order_details // html body
}
console.log("mailOptions"+JSON.stringify(mailOptions));
smtpTransport.sendMail(mailOptions, function(err, result){
console.log(result);
if(err){
return res.status(500).json("Error");
//return res.json("System Error")
}else{
return res.status(200).json("e-mail sent to customer");
}
smtpTransport.close();
});
}
}
when i am calling the api from local system server this is the response i am getting for conole.log(result)-->
{ accepted: [ 'user1#gmail.com' ],
rejected: [],
envelopeTime: 1094,
messageTime: 1124,
messageSize: 300,
response: '250 2.0.0 OK 1539452574 189-v6sm7035924pfe.121 - gsmtp',
envelope:
{ from: 'user#gmail.com',
to: [ 'user1#gmail.com' ] },
messageId: '<d84e7c1e-7158-5c6a-851d-a783e05be5ce#gmail.com>' }
but when i am trying to call the api from my windows-2000 server, for line console.log(result)--> i am getting "undefined".
So my question is whether i am getting this response from gmail.smtp server or gmail.smtp server is unreachable from the system.
I tried to ping gmail.smtp server from my windws-2000 server and there is 0% packet loss. so gmail.smtp is reachable from my server?
I also tried port 465,587,25 and secure- true, false option.
for Error log i am getting this-
{ Error: self signed certificate in certificate chain at TLSSocket.
<anonymous> (_tls_wrap.js:1105:38) at emitNone (events.js:106:13)
at TLSSocket.emit (events.js:208:7) at TLSSocket._finishInit (_tls_wrap.js:639:8) at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:469:38) code: 'ECONNECTION', command: 'CONN' }}
Thnaks for your's valuable inputs on this issue.
var nodemailer = require('nodemailer');
var stringify = require('json-stringify');
// add this line before nodemailer.createTransport()
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port:587,
secure: false,
auth: {
user: 'user#gmail.com',
pass: 'user#123'
}
});
now it will work.

nodemailer unable to send mails from nodeJS

Im using nodemailer to send emails especially to outlook .
UPDATE:
With below code im getting Error: Message failed error
var nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587,
secureConnection: false, // secure:true for port 465, secure:false for port 587
auth: {
user: 'user#domain.com',
pass: 'password'
},
tls: { ciphers: 'SSLv3' }
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Fred Foo 👻" <foo#blurdybloop.com>', // sender address
to: 'myemail#companydomain.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ?', // plain text body
html: '<b>Hello world ?</b>' // html body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});
Full error trace:
Error: Message failed: 550 5.7.60 SMTP; Client does not have permissions to send as this sender [SG2PR06MB1725.apcprd06.prod.outlook.com]
at SMTPConnection._formatError (/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:557:19)
at SMTPConnection._actionSMTPStream (/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:1385:34)
at SMTPConnection._responseActions.push.str (/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:907:22)
at SMTPConnection._processResponse (/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:706:20)
at SMTPConnection._onData (/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:509:14)
at TLSSocket._socket.on.chunk (/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:657:51)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
at TLSWrap.onread (net.js:563:20)
code: 'EMESSAGE',
response: '550 5.7.60 SMTP; Client does not have permissions to send as this sender [SG2PR06MB1725.apcprd06.prod.outlook.com]',
responseCode: 550,
command: 'DATA' }
I had the same problem, and solved it via bring the from field into correspondence with the auth.user field.
i.e. something like the following will work for me.
var nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587,
auth: {
user: 'test-user#gmail.com',
pass: 'password'
},
tls: { ciphers: 'SSLv3' }
});
let mailOptions = {
from: 'user-alias <test-user#gmail.com>', // sender address
to: 'myemail#companydomain.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ?', // plain text body
html: '<b>Hello world ?</b>' // html body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
);
Notice that the two occurence of test-user#gmail.com should be the same.
I think this is because some email server want to make sure that the declared email sender be same as the real email sender.

Resources