Sending email to dynamic sender address using MEAN and Node Mailer - node.js

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

Related

Why email is not receving if sent successfully using nodemailer in node js?

import nodemailer from "nodemailer";
// transporter object
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false, // true then port will change
requireTLS:true,
auth: {
user: "mygmail#gmail.com",
pass:"myapppassword" //google app password
}
})
// mail object
const mailOptions = {
name:"hello",
from: "my#gmail.com",
to: "myfriend#gmail.com", //allowed comma separated mutiple emails
subject: "Node js mail text",
text: "hello buddy aapke father aaye hain!"
}
// send mail
transporter.sendMail(mailOptions, (err, res) => {
if (err) {
console.log(err)
} else {
console.log(res.response)
}
})
In the above code snippet i tried send text email to another account and it is sent successfully but i am not receiving the email but when i try to send the email to the same email it is getting inside the inbox , why it is happening ? Thank you

Error using NodeMailer on Node.js server to generate emails - '451 4.7.0 Temporary server error. PRX4'

I use a Microsoft 365 Outlook account hosted on GoDaddy.
I am attempting to have my Node server send an email to my Outlook account when user clicks the [contact us] button. The code is below:
const nodemailer = require('nodemailer');
app.post('/contact', function(req, res){
res.send(JSON.stringify(req.body));
//generate email
let transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587,
secure: false,
auth: {
user: 'myemail#mysite.com',
pass: 'mypass'
}
});
const mailOptions = {
from: '"Administrator" <myemail#mysite.com>',
to: 'myemail#mysite.com',
subject: "Someone has messaged your website",
html: "<p>Name: " + req.body.fullName + "<br>Email: " + req.body.email + "<br>Message: " + req.body.msg + "</p>"
};
transporter.sendMail(mailOptions, function (err, info) {
if(err)
console.log(err)
else
console.log(info.messageId);
});
});
The error that is being logged to console is:
'451 4.7.0 Temporary server error. Please try again later. PRX4 [CO2PR04CA0181.namprd04.prod.outlook.com]'
UPDATED 7/13
Another error message which may be more useful (as is first line in error stack) is the following:
at SMTPConnection._formatError
I am using this function to send mail to outlook, try and see if it works.
The only missing thing I can suspect is tls.
module.exports.sendEmail = function (options) {
if(!options){
throw new Error("invalid options");
} else if(!options.auth) {
throw new Error("missing user and pass");
}
var transporter = nodemailer.createTransport({
host: 'smtp.office365.com', // Office 365 server
port: 587, // secure SMTP
secure:false, // false for TLS -
auth: options.auth,
tls: {ciphers: 'SSLv3'} // <<< try adding it
});
transporter.sendMail({
from: options.from,
sender: options.sender,
replyTo: options.replyTo,
to: options.to,
subject: options.subject,
cc:options.cc,
bcc:options.bcc,
text:options.text,
html:options.html,
attachments:options.attachments,
}, function (err, info) {
if (err && options.onError) {
options.onError(err);
}
else if(options.onSuccess) {
options.onSuccess(info);
}
});
}
****** caller ******
nodeoutlook.sendEmail({
auth: {
user: "user#outlook.com",
pass: "********"
},
from: 'from#outlook.com',
to: 'to#gmail.com',
subject: 'subject',
html: '<b>html</b>',
text: 'text',
replyTo: 'replyTo#gmail.com',
attachments: [],
onError: (e) => console.log(e),
onSuccess: (i) => console.log(i)
}
Understand this is a pretty lame answer....but its the truth!!
Im changed the password (mypass) for my email account then it worked.

Nodemailer is not sending mails to valid emailids if array contains invalid emailids

I have tried to send multiple emails with nodemailer-smtp-transport in nodejs but if multiple recipients list contains valid and invalid email ids then mails are not going to valid emails ,always it is going to error callback.
My sample code:-
var testemails=["name.#gmail.com,santosh#gmail.com"];
var mailOptions = {
from:config.email_from_addr,
//bcc :receiver_email,
bcc:testemails,
subject :subject,
html:html
};
var transporter = nodemailer.createTransport(smtpTransport({
host:config.email_host_name,
port: 25
}));
transporter.sendMail(mailOptions, function(error, info){
console.log("error: "+error);
console.log("info: "+info);
if(error){
//console.log("Rejected: "+info.rejected);
console.log("error",'Failed to send: '+subject+' ; Error: '+error);
return callback(null);
}else{
console.log("info","Promotion_EmailFurnished: "+subject+" : "+receiver_email);
return callback(null);
}
});
//here Invalid emaild: name.#gmail.com and
valid emailId: santosh#gmail.com
and how to collect if mails are failed to send due to invalid emails?
Any help?
Thanks is advance.
Your array is wrong,
var testemails=["name.#gmail.com,santosh#gmail.com"];
Should be,
var testemails=["name.#gmail.com","santosh#gmail.com"];
Update
Above is just the first thing wrong with you code,
Second
in mailOptions you forgot to put the to address, bcc might work for nodemailer but without a to address it will fail from smtp endpoint.
var mailOptions = {
from:config.email_from_addr,
to : testemails,
bcc:testemails,
subject :subject,
html:html
};
Third,
Where is the password field for the transporter ?
sample for latest node mailer :
var transport = nodemailer.createTransport("SMTP", {
host: "smtp.gmail.com", // hostname
secureConnection: true, // use SSL
port: 465, // port for secure SMTP
auth: {
user: "gmail.user#gmail.com",
pass: "userpass"
}
});
Please use this for more info as well : Docs

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

Can't send mail node mailer

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

Resources