Nodemailer can't access info from .env - node.js

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

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!

E-mail getting rejected sent by Nodemailer

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.

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

How can I create a custom smtp server to send out notification emails in Nodejs?

My requirement is to send a notification email from my application to any email id , eg: a gmail address. I went through some modules like the smtp-server ,smtp-connection and emailjs
This is what I have got till now.
var SMTPServer = require('smtp-server').SMTPServer
var server = new SMTPServer({
name: 'testDomain.com',
authOptional: true,
onAuth: function (auth, session, callback) {
callback(null, {user: 'sample-user'})
}
})
server.on('error', function (err) {
console.log('Error %s', err.message)
})
var port = 1234
server.listen(port, function () {
console.log('SERVER: Listening on port: ' + port)
var opts = {
host: '127.0.0.1',
port: port,
username: 'testUser',
password: 'testUser123',
to: 'someUser#gmail.com'
}
sendEmail(opts,function (err, message) {
server.close()
})
})
where sendEmail is a function using emailjs.
function sendEmail(opts,callback) {
var server = email.server.connect({
user: opts.username || '',
password: opts.password || '',
host: opts.host,
ssl: false
})
server.send({
text: 'i hope this works',
from: 'you <'+opts.username+'#testDomain.com>',
to: ' <'+opts.to+'>',
subject: 'testing emailjs'
}, function (err, message) {
console.log(err || message);
callback(err, message)
})
}
But it seems that the client is not able to connect to the server. It is hanging.
I tried smtp-connection like this initially:
var connection = new SMTPConnection({
port: port,
host: '127.0.0.1',
ignoreTLS: true
})
connection.connect(function () {
var envelope = {
from: opts.username+'#testDomain.com',
to: opts.to
}
var message = "Hello!!!"
connection.send(envelope, message, function(err,message){
callback(err,message)
connection.quit()
})
This seems to work but gives this output
response: '250 OK: message queued'
the smtp-connection documentation says it only queues the messages doesnt deliver it to the recipient.
How can I achieve my requirement? I am attempting to send the notification from a custom mail server because I want to avoid adding the user credentials of an email account in the code in plaintext. I am looking for a simple mailserver which can be spun up when the notification needs to be sent and then shut down.
Am I completely offtrack, not understanding how mail servers work?? Please give some feedback and a best approach to solve this.
Just my opinion but I think its better to take a separate mail server.
like the example from nodemailer:
var nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass#smtp.gmail.com');
// setup e-mail data with unicode symbols
var mailOptions = {
from: '"Fred Foo ?" <foo#blurdybloop.com>', // sender address
to: 'bar#blurdybloop.com, baz#blurdybloop.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ?', // plaintext body
html: '<b>Hello world ?</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
For the security:
You can use separate file for storing the username / password.
Use can use a Token based authentication. So you don't need to save the password. An example of this is OAuth. Instead of the password you authenticate with a token. This token u get from the mailserver provider (like gmail).
An example use of oauth and nodemailer you can find here.

Nodemailer sending mail again after 8 hours

I am using nodemailer module to send mail from the node server when users request for a new password using the following function:
function(email, password, callback) {
var smtpTransport = nodemailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
user: 'something#gmail.com',
pass: 'my_password'
}
});
var mailOptions = {
from: 'My Team<something#gmail.com>',
to: email,
subject: 'new password',
text: 'login with email: ' + email + 'and password: ' + password,
html: '<p>login with email: ' + email + ' and password: ' + password +
'<br> Visit here to learn more ' + ' google</p>'
};
smtpTransport.sendMail(mailOptions, function(err, res) {
if(err) {
smtpTransport.close();
return callback(err);
} else {
smtpTransport.close();
return callback(null);
}
});
};
The email id I am using is a valid one. The user indeed gets a mail from this id containing a newly generated random password. However, he again gets a mail after exactly 8 hours containing yet another newly generated random password. I don't know how this function is called again after 8 hours on the server side.
I am using winston for logging all api calls and the data received in the request object. However the timestamp format is unfamiliar to me. How do I convert this
"timestamp":"2013-09-13T19:39:16.814Z" logged by winston to my local time? Also due to asynchronous nature of node.js the order in which api calls are logged is all interleaved and messed up. How do I log the api calls in the order they are called and served?

Resources