nodemailer not sending and returning error - node.js

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.

Related

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.

Sending email from a non existing mail id (noreply#myserver.com) in Nodejs using nodemailer npm

Below is the sample code am using for sending mail.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587,
auth: {
user: 'me#myserver.com',
pass: 'mypassword'
}
});
var mailOptions = {
from: 'no-reply#myserver.com', //It will work if i give me#myserver.com but i need no-reply#myserver.com in from option.
to: 'someuser#gmail.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
I will give me below error if i use no-reply#myserver.com in from option.
Error: Message failed: 554 5.2.0
STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied;
Failed to process message due to a permanent exception with message
Cannot submit message.
Some email servers do not accept changing FROM address. This is not about nodemailer. You need to check your email server configuration.

Nodemailer only working with one email address

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

Nodemailer doesn't send emails, and doesn't return an error message

I would like to know why nodemailer isn't sending emails, but it isn't returning any errors so I don't know what to do. Here's my code (pretty much copied from the nodemailer documentation):
var cors = require('cors');
var corsOptions = {
origin: 'http://davidmichael.me',
allowedHeaders: 'accept, content-type',
methods: 'GET, POST'
};
var nodemailer = require('nodemailer');
module.exports = function(app){
app.options('/email', cors(corsOptions));
app.post('/email', cors(corsOptions), function(req, res){
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'my-email',
pass: 'my-password'
}
});
var mailOptions = {
from: 'David Michael <my-email>',
to: 'recipient-email',
subject: 'Just verifying your email address ✔',
text: 'Hello to myself!',
html: '<p><b>Hello</b> to myself</p>'
};
var emailMessage = "";
transporter.sendMail(mailOptions, function(error, info){
if(error){
emailMessage = "there was an error :-(, and it was this: " + error.message;
}else{
emailMessage = "Message sent: " + info.response;
}
});
//transporter.close();
return res.json({
message: "success",
email: emailMessage
});
});
};
Every time I try it, it successfully returns the JSON object at the end as follows:
{"message":"success","email":""}
Perhaps I should note that I am using CORS because I want to take the user's email address. My client-side app sends a HTTP POST request with a JSON object containing their email address -- however, the current code doesn't do anything with that yet. I want to get the basic mechanism working first.
I am using Openshift. Could that make a difference?
Also, I have tried using non-Gmail email addresses, and the same thing happens.
Any ideas about where I'm going wrong?
Looks like you're returning your JSON response before the asynchronous method returns. Try putting the response inside the callback:
transporter.sendMail(mailOptions, function(error, info){
if(error){
emailMessage = "there was an error :-(, and it was this: " + error.message;
}else{
emailMessage = "Message sent: " + info.response;
}
return res.json({
message: "success",
email: emailMessage
});
});
//transporter.close();
You may not want to use this in practice though. Waiting for emails to send can be time-consuming and I suspect you'd see lots of timeouts. This should get you in the right direction though!
This also assumes your email address is all set up and you've entered real recipients as well. I know I had issues with Gmail because they're really good at detecting bots :)

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)

Resources