First time trying to get nodemailer to work, and it does work, but only if the sending email address is my email address... I currently have this code:
app.post('/', function (req, res) {
const transporter = nodemailer.createTransport({
service: 'Hotmail',
auth: {
user: 'myemail#hotmail.co.uk',
pass: 'XXXX'
}
});
const mailOptions = {
from: req.body.address, // sender address
to: 'myemail#hotmail.co.uk', // list of receivers
subject: req.body.address,
html: req.body.message // html body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log(req.body);
console.log('Message %s sent: %s', info.messageId, info.response);
res.render('index');
});
If i put in a random email address in the form and send it, I get an Error: Message Failed. Not sure if i'm missing something really obvious with this but at the moment the sender address has to match the receiver (or it could be my auth.user email) for it to work.
Has anyone had trouble with this before / can shine some light on this? Thanks
So i think i've fixed it. It doesn't seem to let emails be sent from someone who isn't authorised, so I have set the sender and receiver to my email address, and used the users email address from the form in the body of the email.
const mailOptions = {
from: 'myemail#hotmail.co.uk', // sender address
to: 'myemail#hotmail.co.uk', // list of receivers
subject: req.body.address,
text: `${req.body.address} - ${req.body.message}` // html body
};
Related
I was sending register verification emails through Nodemailer using the code below, and it turns out that some of my clients are either not receiving anything or the email goes to spam. Some other clients can receive the email normally. I asked Google Support but they said it is not possible that the same kind of emails goes to some users' spam folder and some other users' inbox folder. That's why I am confused here.
BTW, Google confirmed with me that the DKIM and other verifications are good. And the emails that are sent have arrived at those clients' mailboxes. But without their approval, Google doesn't know if the email is not actually there or is sent to the spam folder.
function registerEmailSender(firstName, lastName, email, uuid) {
console.log('registerEmailSender is triggered');
console.log('email: ', email);
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'info#mydomain.com', // generated ethereal user
pass: 'mypassword' // generated ethereal password
},
});
const html = `
Hi ` + firstName + `,<br>
<br>
Please click the link to active your account: https://example.com/sessions/email-verification/` + uuid + `<br>
<br>
Sincerely, <br>
My Team<br>
`
const mailOptions = {
from: 'My Email <info#mydomain.com>', // sender address
to: email, // list of receivers
subject: 'Happy to have you here', // Subject line
html: html
};
return transporter.sendMail(mailOptions, function (err, info) {
console.log('sendMail is triggered');
// if(err)
// console.log('Error occurs: ', err)
// else
// console.log('Email sent: ', info);
if(err) {
console.log('Error occurs: ', err)
}
else {
console.log('Email sent: ', info);
}
});
}
It turns out nodemailer is not very reliable due to it is not recognized as a 'trusted application' unless your server itself is trusted by Google. And that seems to be reducing your reputation and cause the email to be rejected.
Best solution I can find for now is to use some paid mailing service like Mailchimp. Just don't use Nodemailer if you don't have to.
I'm trying to setup a simple contact form using nodemailer. All i'm trying to do is send an email to my email from the contact form. My email is "myemail#mac.com"
When I do my axios post I get an error in the data object saying: 550 5.7.0 From address is not one of your addresses. but yet the status:200 and statusText:"OK"
When I use the same from email as the icloud email "myemail#mac.com" then it works ? I dont see anything where it says you have to use the same from address as the service address ?
Axios post:
const request = axios.post('http://localhost:3002/send', {'name':John Doe,'email':meme#gmail.com'});
request.then((result)=>{
console.log("request = ", result);
});
Error message from console.log("request = ", result);
error:{
code:"EMESSAGE",
command:"DATA",
response:"550 5.7.0 From address is not one of your addresses.",
responseCode:550
}
nodemailer is my node.js
const transporter = nodemailer.createTransport({
service: "iCloud",
auth: {
user: "myemail#mac.com",
pass: "myemailpassword"
}
})
app.use('/send', function(req, res){
var message = {
from: req.body.email,
to: 'myemail#mac.com',
subject: 'Message From Portfolio Contact Form',
//text: 'Plaintext version of the message',
html: '<p>'+req.body.description+'</p>'
};
transporter.sendMail(message, function(error, info){
if(error){
res.json({error: error});
}else{
res.json({success: info.response});
};
});
})
You configure nodemailer transport with iCloud Service and you are trying to send a mail with a gmail adresse.
from: req.body.email, // meme#gmail.com
to: 'myemail#mac.com'
Which logically produces the error:
response:"550 5.7.0 From address is not one of your addresses."
You probably want to do the inverse.
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);
});
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
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)