Strange behavior from Nodemailer - node.js

I have a strange bug with nodemailer , that's my code
await transporter.sendMail({
from: "noreply#myDomainName.com",
to: this.inputs.email,
subject: "Accessing your account",
html: emailTemplate
});
the email sent to my users from my personal account
myPersonalEmail#gmail.com not from noreply#myDomainName.com
I use my personal account in the transporter - but why considering it ?
here's the transporter configs
process.env.EMAIL_SENDING_CONFIG includes
myPersonalEmail#gmail.com#myPersonalEmailPassword
var transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
service: "gmail",
port: 587,
secure: false,
auth: {
user: process.env.EMAIL_SENDING_CONFIG.split("#")[0],
pass: process.env.EMAIL_SENDING_CONFIG.split("#")[1]
}
});
By the way , this email noreply#myDomainName.com is not exist , if this related to the issue

you need to pass your custom domain inside your auth propriety as mentioned in nodmailer document https://nodemailer.com/smtp/.
also you can't use a custom domain with a free account (FREE STMP)
So you need to configure a transporter with your custom domain info (host, port, user and password) You can find this info in the email configuration of your specific hosting provider.
var transporter = nodemailer.createTransport({
host: 'something.yourdomain.com',
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: 'username#mydomain.com', // your domain email address
pass: 'password' // your password
}
});
Then you can go on and define the mail options:
var mailOptions = {
from: '"Bob" <bob#bobsdomain.com>',
to: 'tom#gmail.com',
subject: "Hello",
html : "Here goes the message body"
};

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

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.

Using Webmail with Nodemailer

I used gmail with node mailer to create a login system in js node.
In development mode I used gmail.
const sendEmail = async options =>{
//1)Create a transporter
// const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_SEND_SESSION,
pass: process.env.EMAIL_SEND_PASSWORD,
}
});
//2) Define the mail options
const mailOptions = {
from:'Creative Point <test764#gmail.com>',
to:options.email,
subject:options.subject,
html:options.message
}
//3) Actually send the email
await transporter.sendMail(mailOptions);
};
But in production mode I would like to use webmail,I received when I created the data and port account on cpanel.
const transporter = nodemailer.createTransport({
service:'smtp.specialsoft.ro ',
port: 465,
auth: {
user: process.env.EMAIL_SEND_SESSIONCPANEL,
pass: process.env.EMAIL_SEND_PASSWORDCPANEL,
}
});
But it gives me the next mistake.
Error: There was an error sending the email.Try again later!Error: Invalid login: 535-5.7.8 Username
and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials g20sm3266545ejz.88 - gsmtp
at C:\Users\Cosmin\Desktop\Authentification System Node
JS,Express,JsonWebToken\controllers\sessionController.js:56:21
at processTicksAndRejections (internal/process/task_queues.js:93:5)
I haven't found anything relevant on the internet about this error.
I solved the problem by following the steps below.
I installed a module: nodemailer-smtp-transport.
const smtpTransport = require('nodemailer-smtp-transport');
I corrected the host name and some options.
host:'mail.mydomani.ro'
secureConnection: false,
tls: {
rejectUnauthorized: false
}
3)I integrated the module nodemailer-smtp-transport in the transporter.
const transporter = nodemailer.createTransport(smtpTransport({
host:'mail.mydomanin.ro',
secureConnection: false,
tls: {
rejectUnauthorized: false
},
port: 587,
auth: {
user: process.env.EMAIL_SEND_SESSION,
pass: process.env.EMAIL_SEND_PASSWORD,
}
}));

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

Nodemailer and Godaddy, I've tried every solution to no avail

I bought a domain and an email adrees with GoDaddy with Office 365 Basic bundled with it.
I'm trying from a few days to send just an email with nodemailer from my node app without ever succeding because I always get a 535 Authentication Failed from user#domain.com.
I've scanned StackOverflow for days trying every solution, this is every setting I've tried to pass to nodemailer.createTransport:
const mailerConfig =
{
//First set of parameters tried
service: 'Godaddy',
host: "smtp.office365.com",
secureConnection: true,
port: 587,
auth:
{
user: "user#domain.com",
pass: "password"
}
//Second set of parameters tried
host: "smtp.office365.com",
secureConnection: false,
port: "587",
auth:
{
user : "user#domain.com",
pass : "password"
},
tls:
{
ciphers:'SSLv3'
}
//Third set of parameters tried
service: "outlook",
auth:
{
user: 'user#domain.com',
pass: 'password'
}
}
None of these three settings worked, someone has any idea?
Thanks.
There is a godaddy FAQ entry called "Manually configure email set up on Thunderbird" (which seems to be only accessible via WaybackMachine).
It mentions the following configuration:
Incoming (IMAP): imap.secureserver.net 993 SSL Normal password
Outgoing (SMTP): smtpout.secureserver.net 465,587 SSL Normal password
and
Username: Workspace Email address
Using the above, especially smtpout.secureserver.net as SMTP Host works for me – tested with godaddy email in 2021.
If anyone is still having issues, this config is working for me actually in Sept 2021:
var transporter = nodemailer.createTransport({
host: "smtpout.secureserver.net",
port: 587, // port for secure SMTP
auth: {
user: "youremailaddress",
pass: "yourpassword",
},
});
You need to enable all access
and don't forget to disable the sms/call confirmation for the login
by the way this code i used on firebase cloud function/nodemailer
cors(req, res, async () => {
var transporter = nodemailer.createTransport({
host: "smtp.office365.com",
port: 587, // port for secure SMTP
auth: {
user: "user#domain.com",
pass: "password",
},
tls: {
ciphers: 'SSLv3'
}
});
if (req.method === 'POST') {
var mailOptions = {
from:req.body.from,
to: req.body.email, // list of receivers
subject: req.body.subject, // Subject line
text: req.body.text,
};
return transporter.sendMail(mailOptions).then(() => {
res.status(200).send('Email has been sent to mr/ms : ' + req.body.email); return ("mail sent")
});
}
})
You must enable SMTP login for the O365 mailbox or user in the admin settings
If the user has multifactor turned on, then you need to use "app password", normal password won't work if MFA is enabled.
https://learn.microsoft.com/en-us/azure/active-directory/user-help/multi-factor-authentication-end-user-app-passwords
You need to disable security defaults or adjust Conditional Access policies.
https://learn.microsoft.com/en-us/azure/active-directory/fundamentals/concept-fundamentals-security-defaults
Also, you can debug issue using sign-in logs in azure user account.

Resources