Can't send mail node mailer - node.js

I am getting Recipient Error: Can't send mail - no recipient defined error while sending an email using npm node mailer. I am consulting the example as in the documentation. consider abc#gmail.com with my real email.
var emailAddress = "abc#gmail.com";
mailOptions = {
from: "Admin <abc#gmail.com>", // sender address
to: emailAddress, // list of receivers
subject: "Hello", // Subject line
text: "hello", // plaintext body
html: "<b>Hello</b>" // html body
}
smtpTransport = nodeMailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "abc#gmail.com",
pass: "123"
}
});
smtpTransport.sendMail(mailJson, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
// if you don't want to use this transport object anymore, uncomment following line
smtpTransport.close(); // shut down the connection pool, no more messages
});

The following advise would be useful (for others users):
mailJson is your configuration called mailOptions.
Would return in a promise your result, which is so useful when you're sending an email in an async code.
So, your code would be the following:
mailOptions = {
from: "Admin <abc#gmail.com>", // sender address
to: emailAddress, // list of receivers
subject: "Hello", // Subject line
text: "hello", // plaintext body
html: "<b>Hello</b>" // html body
}
smtpTransport = nodeMailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "abc#gmail.com",
pass: "123"
}
});
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error, info) => {
error ? reject(error) : resolve(info);
});
});
});

If no solution work for you
Go to that link and active less secure than try to send mail
enter link description here

Related

how to send email using loop and update each record?

I have implemented nodemailer for sending emails. for email detail like email id, subject and text I have a table in the db. in that table I had a flag (new, processed)to get the list of new records so that I can send mail of new records
I am using for loop on the record list to send emails. the thing I want to know is once the mail is forwarded successfully I want to update the record flag in the db. I am using sequlize in node.js.
var WEBDialerList=[];
var WEBDialerListCount;
SqlObj.WEBDialer.findAll(
{
where: {
IsDeleted: 0,
Status: 'New'
}
}
)
.then(data => {
WEBDialerList = JSON.parse(JSON.stringify(data));
console.log("d.length")
console.log(WEBDialerList.length)
console.log("d.length")
if (WEBDialerList.length > 0) {
for (var i in WEBDialerList){//= 0; i < WEBDialerList.length; i++) {
WEBDialerListCount = i;
"use strict";
const nodemailer = require("nodemailer");
let transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: 'abc#gmail.com',
pass: '******'
}
});
var e = WEBDialerList[i].Email;
let mailOptions = {
from: '"ib ik" <ib.ik2093#gmail.com>',
to: e,
subject: "Test Email from Node.js",
text: " scheduler Hello, this is a test email. I have configured my gmail account in node.js to send emails. I am checking, is it configured correctly.",// plain text body
html: "<b> " + WEBDialerList[i].WEBDialerId + " scheduler Hello, this is a test email. I have configured my gmail account in node.js to send emails. I am checking, is it configured correctly.</b>" // html body
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log(error);
}
else {
console.log("Message sent1: ", info);
console.log(WEBDialerListCount)
transporter.close();
}
})
}
}
})
.catch(function (err) {
console.log(err)
});
In transporter.sendMail I just get it at the end of the loop so it is difficult for me to get what records needs to be updated.
A possibile solution can be:
[..]
// Move this declarations out is a good idea
const nodemailer = require("nodemailer");
let transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: 'foobar#gmail.com',
pass: 'foobar'
}
});
[..]
for (var i in WEBDialerList) {
[..]
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
sql.setFlag('errored') // pseudocode
} else {
sql.setFlag('processed') // pseudocode
transporter.close();
}
})
}
[..]
PS: remove your gmail credentials!
Please, async await instead of promises code look good and easy to read.
Here is the solution to your question.
Promis.all(
WEBDialerList.map(WEBDialer => {
let mailOptions = {
from: '"ib ik" <ib.ik2093#gmail.com>',
to: WEBDialer.email,
subject: "Test Email from Node.js",
text: " scheduler Hello, this is a test email. I have configured my gmail account in node.js to send emails. I am checking, is it configured correctly.",// plain text body
html: "<b> " + WEBDialer.WEBDialerId + " scheduler Hello, this is a test email. I have configured my gmail account in node.js to send emails. I am checking, is it configured correctly.</b>" // html body
};
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
reject(error)
} else {
console.log("Message sent1: ", info);
console.log(WEBDialerListCount)
// Update user here
transporter.close();
resolve()
}
})
})
})
)
Please make the necessary changes according to your requirements.
DO let me know if you need more help or anything you want to ask.
I hope you like it :)

Sending email to dynamic sender address using MEAN and Node Mailer

I am struck with sending email to dynamic sender address using MEAN(Angular 2+) and Node Mailer. I couldn't find a single example or tutorial for sending email to dynamic sender address. Can any one give a very simple example with single value with html, TS, service and server file codes please.
var nodeMailer = require('nodemailer');
function sendEmail(email, callback) {
var transporter = nodeMailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: 'youremail#gmail.com',
pass: 'password'
}
});
var mailOptions = {
from: 'youremail#gmail.com',
to: email,
subject: 'Email Verification',
text: 'Hi there, a simple test mail'
//html: '' //for html code
};
transporter.sendMail(mailOptions, function (err, info) {
if (err) {
callback(err);
} else {
callback(null, 'success');
}
});
}
Everything is same till HTTP call. Just call the function with required email

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 mail with nodemailer - email from field incorrect

Trying to set up a contact form with nodemailer. Here's what's in my app.js:
// EMail configuration
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "myemailaddress",
pass: "xxx"
}
});
// CONTACT FORM
app.get('/contact', function (req, res) {
res.render("contact");
});
app.post('/contact', function (req, res) {
var mailOptions = {
from: req.body.email, // sender address
to: "myemailaddress", // list of receivers
subject: req.body.subject, // Subject line
text: req.body.message, // plaintext body
}
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close(); // shut down the connection pool, no more messages
});
res.render("contact", { success: "building web app" });
});
And my contact.jade template looks like this:
form#contact-form(action="/contact", method="POST")
div.span5
p Full name:
input#name(type="text", name="name")
p Email address:
input#email(type="email", name="email")
p Subject:
input#subject(type="text", name="subject")
p Message:
textarea#message(type="text", name="message", rows="5")
p: button(type="submit") Send message
The email now works, but comes from myemailaddress rather than the one I enter into the email field on the template. Any ideas
Gmail and many other email services don't allow you to send messages with various FROM field.
you can use postmark, they provide an excellent api for sending emails and there is a node module for it (https://npmjs.org/package/postmark)

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