Nodemailer and postfix - node.js

I have install postfix with default parameters as Internet site, nodemailer package into my node.js server. And trying to do this:
var transporter = nodemailer.createTransport({
host: '127.0.0.1',
port: 25,
// auth: {
// user: user,
// pass: pass
// },
secure: false,
tls:{
rejectUnauthorized: false
}
});
I dont know what is username and password, I did not set it.
transporter.sendMail({
from: '"Mysite.com" <robot#mysite.com>', // sender address
to: user.email, // list of receivers
subject: "Your password for mysite.com", // Subject line
html: html // html body
}, (err) => {console.log('mail send error', err)});
In tail -f /var/log/mail.log I see this
Jan 30 01:00:04 80523 postfix/smtpd[26772]: connect from localhost[127.0.0.1]
Jan 30 01:00:04 80523 postfix/smtpd[26772]: lost connection after EHLO from localhost[127.0.0.1]
Jan 30 01:00:04 80523 postfix/smtpd[26772]: disconnect from localhost[127.0.0.1] ehlo=2 starttls=1 commands=3
And mail is not sent...
But if I use command echo "First message" | mutt -s "msg" mymail#mail.ru, mail sent add I receive it.
Some ideas why?

If you followed similar set up for postfix as found here:
https://medium.com/codingtown/send-mail-using-postfix-server-bbb08331d39d
Then you should be able to get the following to run
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: 'postfix',
host: 'localhost',
secure: false,
port: 25,
auth: { user: 'yourlinuxusername#edison.example.com', pass: 'yourlinuxuserpassword' },
tls: { rejectUnauthorized: false }
});
let mailOptions = {
from: 'yourlinuxusername#edison.example.com',
to: 'xxxyyyzzz111#gmail.com',
subject: 'nodemailer test',
text: 'hope it got there'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log(info);
}
});

Related

Why does nodemailer throw the error: Invalid credentials? [duplicate]

I would like to find a way to send email from my app using nodemailer to the users either with some kind of google authentication or any other way. Below mentioned working code has stopped working after Google has disabled less secure app option.
const nodemailer = require('nodemailer')
const sendEmail = async options => {
const transporter = nodemailer.createTransport({
// host: "smtp.gmail.com",
// port: "465",
// secure: true,
service:'gmail',
auth: {
user: "USER_EMAIL",
pass: "USER_PASSWORD"
},
tls:{rejectUnauthorized:false}
})
const message = {
from: `${process.env.FROM_NAME} <${process.env.FROM_EMAIL}>`,
to: options.email,
subject: options.subject,
text: options.message,
html: options.message,
attachments: [
{
filename: '.png',
path: __dirname + '.png',
cid: '.png'
}
]
}
const info = await transporter.sendMail(message)
console.log('Message sent : %s', info.messageId)
console.log(__dirname)
}
module.exports = sendEmail
At the time of writing, Less Secure Apps is no longer supported by google. And you can't use your google account password.
You're gonna have to generate a new app password.
App passwords only work if 2-step verification is turned on.
Follow this steps to get the app password
Go to https://myaccount.google.com/security
Enable 2FA
Create App Password for Email
Copy that password (16 characters) into the pass parameter in Nodemailer auth.
const client = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "username#gmail.com",
pass: "Google-App-Password-Without-Spaces"
}
});
client.sendMail(
{
from: "sender",
to: "recipient",
subject: "Sending it from Heroku",
text: "Hey, I'm being sent from the cloud"
}
)
You should check out Xoauth2.
Nodmailer supports serval types of Oauth
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
type: "OAuth2",
user: "user#example.com",
clientId: "000000000000-xxx0.apps.googleusercontent.com",
clientSecret: "XxxxxXXxX0xxxxxxxx0XXxX0",
refreshToken: "1/XXxXxsss-xxxXXXXXxXxx0XXXxxXXx0x00xxx",
accessToken: "ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x",
expires: 1484314697598,
},
});

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);

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

NodeJs Error When Submitting Email using Nodemailer

I have followed these steps to setup nodemailer
1) Allow access to less secure apps in gmail
2) Written the following in app.js
app.post('/reachus/send',function(req,res){
var transporter=nodemailer.createTransport({
service:'Gmail',
auth: {
user:'my#gmail.com',
pass:'***'
}
});
var mailOptions={
from:'Naveen DK <my#gmail.com>',
to:'my#gmail.com',
subject:'Email Sent from your website',
text:'You have a submission with the following details.. Name: '+req.body.name +'Email: '+req.body.email+' Message: '+ req.body.message,
html: '<p>You have a submission with the following details..</p><ul><li> Name :'+req.body.name + ' </li><li>Email: '+req.body.email +'</li><li>Message ' + req.body.message +'</li></ul>'
};
transporter.sendMail(mailOptions,function(error,info){
if(error){
console.log(error);
res.redirect('/');
} else{
console.log('Message Sent ' + info.response);
res.redirect('/');
}
});
});
3) Once I Click on Submit Email I get the following error
{ Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TLSWrap.onread (net.js:569:26)
code: 'ECONNECTION',
errno: 'ECONNRESET',
syscall: 'read',
command: 'CONN
}
Please find the below 2 vids for more details
1 https://www.dropbox.com/s/nc1zvivlfpabj6h/HowMyCodeLooksLike.wmv?dl=0
2 https://www.dropbox.com/s/tfsqu6ir90s682h/ErrorOnceSubmissionDone.wmv?dl=0
Thanks in advance
Naveen
use below code for sending email from nodemailer, inside function pass ur parameter and you will get ur result.
var AppConfig = {
'sendEmailID': 'useremail',
'sendEmailFromName': 'senderemail',
'sendEmailPassword': 'password'
}
function SendEmail(toEmail, Subject, html) {
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "username",
pass: AppConfig.sendEmailPassword
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3'
}
});
// setup e-mail data with unicode symbols
var mailOptions = {
from: AppConfig.sendEmailFromName, // sender address
to: toEmail, // list of receivers
subject: Subject, // Subject line
html: html // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log("ERROR----" + error);
}
console.log('Message sent: ' + info.response);
});
}
With the response I got from #chetan mekha I changed my code as follows:
var smtpTransport=nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "**",
pass: "**"
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3'
}
});
But there another error came up saying : { [Error: self signed certificate in certificate chain] code: 'ECONNECTION', command: 'CONN' } But adding the line
rejectUnauthorized: false under ciphers made it work! so final code snippet that worked for me looks like this..
var smtpTransport=nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "**",
pass: "**"
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false
}
});

how to setup emailjs to send email

I am trying to use mailjs to send out email, and here is what I got:
var server = email.server.connect({
user: "something#mydomain.com",
password: "pw",
host: "smtp.office365.com",
port: "587",
tls: true
});
server.send({
text: "test!!",
from: "someemail#mydomain.com",
to: "recipient#somedomain.com",
subject: "hello"
}, function(err, message){console.log(err || message);});
The above code eventually throw me an error:
{ [Error: authorization.failed]
code: 3,
previous: { [Error: timedout while connecting to smtp server] code: 4, smtp: undefined },
smtp: undefined }
So basically my corporate email service is on office365, configured to use mydomain.com. I don't really know what does the user and password mean for the server, so I just used the sender's email and password. Is it anything wrong with my code? Maybe this is the problem?? Or otherwise what would it be?
First check and confirm host.Here is no need to use port: "587", tls: true.
You can use, this is wrking code
var server = email.server.connect({
user: 'abc#india.com',
password: 'vvq3tng$',
host: 'your host',
ssl: true,
});
server.send({
from: '<abc#india.com>', // sender address
to: '<xyz#india.com>',// list of receivers
cc: '',
subject: 'your subject', // Subject line
text: 'contant', // html body
}, function (err, message) {
if(err) {
console.log(err)
} else {
console.log("email sent")
}
});

Resources