Send mail to multiple recipients using nodemailer - node.js

how to send mail to multiple recipients which are stored in database(mongodb) using nodemailer?
Currently Im sending to single recipient. But im not able to figure out how to send mail to multiple people whose mail ids are stored in mongodb.
If someone knows the answer, please respond.
Thank you in advance :)

Use mongodb distinct to get array for all the email_address you want to send the email, and pass that array to nodemailer.
const nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'gmail.user#gmail.com',
pass: 'yourpass'
}
});
let email_arr = db.users.distinct( "email", { /* Any condition you want to put*/ } )
let mailOptions = {
from: "test#example.com", // sender address
subject: "Hello ✔", // Subject line
text: "Hello This is an auto generated Email for testing from node please ignore it", // plaintext body
to: email_arr
}
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});

Related

Receiving email from users by nodemailer

I am trying to receive email from users using nodemailer. The email will be sent from users email and received by my email. But all it doing is sending email from my email to my email though I set different email account for from and to. But when I revesre the email acounts and set from:myemail#example.com and to:usersemail#example.com this works fine. Why this is happening? How can I fix this so that I can receive emails from users? Here is the code:
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: "gmail",
secure: false,
auth: {
user: process.env.MY_EMAIL,
pass: process.env.MY_PASSWORD,
},
});
// Creating the email
let info = {
from: process.env.USERS_EMAIL,
to: process.env.MY_EMAIL,
subject: 'Demo message',
text: 'For clients with plaintext support only',
html: `
<p>This is a demo email</p>
`
};
//send the email
transporter.sendMail(info, function (err, info) {
if (err) {
console.log(err);
}
else{
console.log(info);
}
})

How to update insert records into database with every replied-to email?

I'm testing sending emails to myself with Nodemailer, and I have the basic setup,
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "foo#gmail.com",
pass: "foobar"
}
});
const mailOptionsUser = {
from: "John Doe <foo#bar.com>",
to: 'bar#foo.com',
subject: "Test | New email",
html: `<h3>test</h3>`
};
transporter.sendMail(mailOptionsUser, function(error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
I want to be able to track conversations on my web app, so that users can send emails to themselves, and they'll be able to see those messages when they log into the app.
I was thinking of using the replyTo feature, but that won't do what I need -- is there any way to update the database with every sent email (that isn't sent through Nodemailer)?

How to send an e-mail using mailgun/mailchimp/etc in an express/node.js app

I want to send automated e-mails like order brief, sign-in e-mail, confirm e-mail, change password e-mail, etc to clients using mailchimp or mailgun or whatever e-mail delivery server because when I used nodemailer, the clients were receiving the e-mails in their spam inbox and sometimes not receiving at all.
Here's the code I used:
automated_emails.js file:
const nodemailer = require('nodemailer');
const ejs = require('ejs');
const user = 'xxx'
const pass = 'xxx';
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: { user, pass }
});
const emailPasswordChange = (email, uuid) => {
ejs.renderFile("./mail/forgot-password.ejs", { uuid }, (err, data) => {
if (err) return console.log(err)
let mailOptions = {
from: 'xxx',
to: email,
subject: "Forgot My Password",
html: data
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) return console.log(error);
});
})
}
module.exports.emailPasswordChange = emailPasswordChange;
The EJS file is a file that contains the template, and I pass to it the user info like e-mail, name, etc.
They are a bunch of functions I call inside the main index.js file.
How do you suggest me to implement this? Is there a way I can put mailchimp/mailgun/etc's email delivery method inside my nodemailer app?
To prevent moving your email to spam folder be sure that send email (from) is same as userEmail account which used by nodemailer.
I'm using gmail account to send emails using 'nodemailer' and it always success, and here is the code which I use:
const nodemailer = require('nodemailer');
var userEmail = 'yourUserName#gmail.com';
var userPassword = 'yourPassword';
var transporter = nodemailer.createTransport(`smtps://${userEmail}:${userPassword}#smtp.gmail.com`);
// setup e-mail data with unicode symbols
var mailOptions = {
from: userEmail, // sender address
to: 'abc1#hotmail.com, abc2#yahoo.com', // list of receivers
subject: 'Demo-1', // Subject line
text: 'Hello world from Node.js', // plaintext body
html: '<b>Hello world from Node.js</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);
});

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

sending emails in node js

After watching this video http://vimeo.com/26963384 on vimeo which was about how kue works,i have to ask how the code worked without installing any package to help send emails like node mailer.
Does the latest version of node js come with the capability to send emails?.
The code used looks like
jobs.create('email', {
title: 'welcome email for tj'
, to: 'tj#learnboost.com'
, template: 'welcome-email'
}).save();
In the presentation,no package to send emails was added.
var nodemailer = require('nodemailer');
// create SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'xxx#gmail.com',
pass: '******'
}
});
// transporter object for all e-mails
var mail = {
from: 'XXX XXXX <XXX#gmail.com>', // sender address
to: 'XXX#hotmail.com, XXX#gmail.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(mail, function (error, info) {
if (error) {
return console.log('Error : ' + error);
}
console.log('Mail sent: ' + info.response);
});

Resources