E-mail getting rejected sent by Nodemailer - node.js

I am trying to send an email which includes HTML content with the help of nodemailer-express-handlebars. Every time my mail gets blocked by the Gmail which can be checked in sender's Gmail sent-box whereas I got success msg from nodemailer
Email sent: 250 2.0.0 OK 1595608108 i66sm6757247pfc.12 - gsmtp
I am unable to understand why this happening as when I send a mail with text, it gets delivered.
NODEMAILER CODE
sendMail=(email)=>{
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'emailId',
pass: 'password'
}
});
transporter.use('compile',hbs({
viewEngine:{
partialsDir:"./views/",
defaultLayout: "",
layoutsDir: "",
},
viewPath:"./views/",
extName:'.hbs',
}))
var mailOptions = {
from: '<xyz#gmail.com>',
to: email,
subject: 'Your order has been placed successfully.',
template:'mail',
context:{
name:"XYZ",
address:"133"
}
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}

It's recommended to use OAuth2 with NodeMailer and Gmail. Using the plain username and password might be what's causing you problems.

Related

NodeMailer Sending But Recipient Not Receiving

I'm working with NodeMailer trying to figure out why the emails are being sent, but not received. I can see they are being sent because I can see them in my sent folder of my Gmail account. Please let me know what I'm doing wrong.
exports.sendEmail = async(address, subject, html) => {
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
auth: {
user: 'noreply#example.app',
pass: process.env.GMAIL_PASSWORD
}
});
let mailOptions = {
subject: subject,
to: address,
html: html,
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
Then I call the function here:
await utils.sendEmail(user.email, 'Example Password Reset', '<body><p>Click this link to reset your password: Password Reset Link</p><br><p>Or copy this address into your browser: https://example.app/forgotpassword?code=' + passwordResetCode + '</p></body>');
All the emails look normal in my sent folder, but none of them are received by the recipient. Could it be something weird with having the ".app" at the end of my domain? Or am I just doing something wrong in my code?
Thanks!

How to send email using angular 9

The following code send email from server side. How to I use that code in Angular 9 application.
Could you please help some one for this.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'test#test.com',
pass: 'xxxx'
}
});
var mailOptions = {
from: 'test#test.com',
to: 'test#test.com',
subject: 'Sending Email using Node.js',
text: `Hi, thank you for your nice Node.js Email.`
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
Probably, the above code will works i the Angular application, but the big problem doing that, is you will have exposed mail transport credentials in your front app. As mentioned before, best solution is create a simple API that send the email on server side.

Sending email from a non existing mail id (noreply#myserver.com) in Nodejs using nodemailer npm

Below is the sample code am using for sending mail.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587,
auth: {
user: 'me#myserver.com',
pass: 'mypassword'
}
});
var mailOptions = {
from: 'no-reply#myserver.com', //It will work if i give me#myserver.com but i need no-reply#myserver.com in from option.
to: 'someuser#gmail.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
I will give me below error if i use no-reply#myserver.com in from option.
Error: Message failed: 554 5.2.0
STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied;
Failed to process message due to a permanent exception with message
Cannot submit message.
Some email servers do not accept changing FROM address. This is not about nodemailer. You need to check your email server configuration.

Nodemailer can't access info from .env

Can't get Nodemailer to send messages to my yahoo mail inbox. When a message is sent, I get a message that says "ReferenceError: processs is not defined". My interpretation is that it cannot access my .env file. I've been trying to solve this all day long. I originally had it connected to my personal gmail account, it worked at that point. Then I tried to connect it to a different gmail account, at which point it stopped working. I figured it was because I had to get an auth token or something from google, so being as my client uses Yahoo mail, I created a yahoo email account to connect it to, and that's where I'm at right now. I've been trying all day long. Here is my nodemailer method:
require('dotenv').config()
var nodemailer = require('nodemailer');
module.exports = {
sendEmail: (req,res) => {
console.log('-----hit', req.body)
const { name, email, text } = req.body
console.log('req.body', name, email, text)
var transporter = nodemailer.createTransport({
service: 'yahoo',
auth: {
user: processs.env.NODEMAILER_ADDRESS,
pass: process.env.NODEMAILER_PASSWORD
},
tls: {
rejectUnauthorized: false
}
})
var mailOptions = {
from: name + ' ' + process.env.NODEMAILER_ADDRESS,
to: process.env.NODEMAILER_ADDRESS,
subject: 'New Message From ' + name,
text: name + ' ' + email + ' ' + text
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
})
}}
There's a typo in the auth.user value: processs

How to make nodemailer with SMTP work?

I've already lowered my gmail account's security here:
https://myaccount.google.com/lesssecureapps
I've tried to send mail with, and without smtp, but always get some kind of error.
Error: invalid login: 535.5.....
I checked my "user", and "from" parameters are the same. Here's a bit of my code. I'm working on localhost. May it be the problem?
var nodemailer = require("nodemailer");
var smtpTransport = require("nodemailer-smtp-transport");
var transporter = nodemailer.createTransport(smtpTransport({
service: "gmail",
auth: {
user: "seratothdevelop#gmail.com", // my mail
pass: "*********"
}
}));
console.log('SMTP Configured');
var message = "<b>Kedves Felhasználó!</b>"+
"<p>Sikeresen feliratkoztál a Kutyapplikáció hírlevelére, ahonnan friss információkkal látunk el hétről-hétre.</p>"+
"<br><p>Üdvözlettel, </p><p><i>sethdevelop</i></p>";
var mailOptions = {
from: "seratothdevelop#gmail.com", // sender address
to: request.body.subscribeduser, // list of receivers
subject: "Kutyapp feliratkozás", // Subject line
html: message // You can choose to send an HTML body instead
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Üzenet elküldve: ' + info.response);
}
transporter.close();
});

Resources