Use sendgrid from send emails with nodemailer appication gives following error? - node.js

use nodemailer and nodemailer-sendgrid-transport it show the following error
error:The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements
`const nodemailer=require('nodemailer');
const sendGridTranspoter=require('nodemailer-sendgrid-transport')
const transport=nodemailer.createTransport(sendGridTranspoter({
service: 'SendGrid',
auth:{
api_user:'myuser name',
api_key:'password'
}));
`
this is inside my sign up controller function
var email = {
from: 'sener email',
to: 'receiver valid email',
subject: 'shopMe',
text: 'successfully sign up',
html: '<b>Hello world</b>'
};
transport.sendMail (email, function(err, info){
if (err ){
console.log(err);
}
else {
console.log('Message sent: ' + info.response);
}
});

there are two possible ways,
1.allow less secure apps "on" in your google mail security
2.Go to sendgrid, click on marketing and after click senders , fillup the details and check it

You need to verify the mail that is the sender. I solved this problem in this way:
Go here:
Setting -> Sender Authentication -> Single Sender Verification -> Verify an Address
Example
After that, fill out the form on the right and complete the verification of the sender's mail.
You can read in more detail here.

Related

Some emails from Nodemailer are not reaching the clients

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.

nodemailer not sending and returning error

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.

Sending emails to a mailing list with Mailgun

I am creating a personal app for email marketing/news letter/campaign, for this am using the Mailgun API with node.js and a simple HTML form to send emails.
As of now, sending emails is working fine when I give the "to"-address directly in my front part. But when I try to send to a mailing list, I encounter some issues.
I'm trying to send emails to a list which is already created in my Mailgun account.
My below post method is to send mails.
app.post('/', function(req, res) {
var api_key = 'key-here';
var domain = 'mydomin.in';
var Mailgun = require('mailgun-js');
var mailgun = new Mailgun({ apiKey: api_key, domain: domain });
var data = {
from: req.body.myname + "<" + req.body.email + ">",
to: req.body.to,
subject: req.body.subject,
text: req.body.plaintext,
'o:tag': req.body.tag
};
console.log(req.body);
mailgun.messages().send(data, function(error, body) {
console.log(body);
var list = mailgun.lists(req.body.to);
list.members().list(function (err, members) {
// `members` is the list of members
console.log(members);
});
if (error) {
// email not sent
res.render('index', { title: 'No Email', msg: 'Error. Something went wrong.', err: true })
} else {
// Yay!! Email sent
res.render('index', { title: 'Sent Email', msg: 'Yay! Message successfully sent.', err: false })
}
});
});
In the code I mention my mailing list. What happens is when I hit send, it shows success as in the following image:
Finally, here are some logs from the mailgun dashboard
Please point out what I did wrong and share your suggestions how to send emails to the list. My approach may be totally wrong, so any guidance will be appreciated.
NOTE: from my console.it shows "thank you,the messages are in queue". but am not receiving any mails yet. Many many thanks for any help.
Official docs
https://help.mailgun.com/hc/en-us/articles/217531258:
If you're on the Free plan without credit card information present or using your sandbox domain for testing, you're restricted to sending just to Authorized Recipients.

Nodejs send mails

Hi I've sent some mails using express mailer. My problem is, when I need to send mails it's asking for my credentials and using that email to send. However, I want to send emails from a no-reply email but this would mean sending emails from an account thats not set up.
I know in other application servers you can pretty much send emails from any email address, even one which is not your own. I'm wondering how I can do this with nodejs or expressjs.
Edit:
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'yang#example.com',
pass: '****'
}
});
var mailOptions = {
from: 'noreply#send.com',
to: 'yang#receive.com',
subject: 'Hello ✔',
text: 'Hello world ✔',
html: '<b>Hello world ✔</b>'
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Message sent: ' + info.response);
}
});
I've tried this but it's still sending from yang#example.com which is the server I logged in with
Google SMTP servers automatically set your sending address to the one you logged in with.
If you want to send e-mail with an address which isn't your own, you should use a different SMTP server or set up your own one (Haraka, smtp-server, etc.)
You should be able to set the "reply-to" or "from" to whatever you need to. It's independent of which email account you use to send the email. Like an address on a postage mail envelope.
Nodemailer is a bit more robust in my personal experience: https://www.npmjs.org/package/nodemailer
And for higher volume setting up a service like Amazon SES as your transport.

Nodemailer with Gmail service not working on heroku

I've got a basic email setup done for sending an email using Nodemailer with AngularJS and NodeJS and I've got the project deployed on heroku.
The emailing seems to be working just fine when I am running the app on heroku, but when I get it deployed to Heroku no emails are sent.
For authentication, I am using a Gmail address and I also have a bcc to another Gmail address. So from and bcc addresses are two different Gmail addresses. The from address is the same as the address used for authentication.
Could somebody help me with resolving this issue?
Edit: Adding code
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'foobar#gmail.com',
pass: 'foobar'
}
});
router.post('/send',function(req,res){
var mailOptions = {
from: 'Foo Bar ✔ <foobar#gmail.com>',
to: req.body.email,
subject: "Hello " + req.body.email,
text: 'Hello ' + req.body.email + '✔',
html: "<p>Hello " + req.body.email + " </p>",
bcc: "fred#gmail.com"
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Message sent: ' + info.response);
res.send(200);
}
});
});
I believe this is an issue with google account security.
Google blocked your sign-in to use the mailing features due to an unknown device (location).
A few step to verify this:
Start your server locally and sends the email.
Check your account alerts for unknown sign-in.
This can be temporally resolved by:
https://accounts.google.com/DisplayUnlockCaptcha
A more permanent resolution would be to change your password to a stronger level:
upper case letter + lower case letter + special symbols + numbers
Instead of using direct gmail credentials like this
auth: {
user: 'foobar#gmail.com',
pass: 'foobar'
}
Use OAuth2
auth: {
type: 'OAuth2',
user: 'user#example.com',
accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x'
}
Google blocks the heroku IPs (unsafe), if we are using direct credentials like you mentioned above. You can refer this Medium article here
5 years later, I still struggled this problem (all answers found on SO failed in my case). Here is what I did, for now everything works smoothly:
Install SendGrid addon to your Heroku app (Free plan gives you 12k messages a month)
Go to SendGrid panel
Go to Marketing -> Senders to add your sender bot, configure it like this:
From = any_address#heroku.com
Reply = your_gmail#gmail.com
Generate API KEY here
Configure NodeMailer like so:
const nodemailer = require('nodemailer'),
sgTransport = require('nodemailer-sendgrid-transport');
const mailTransporter = nodemailer.createTransport(sgTransport({
auth: {
api_key: process.env.ADMIN_EMAIL_API_KEY // your api key here, better hide it in env vars
}
}))
To send an email now, you have to add your gmail in 'Reply To' field, like so:
mailTransporter.sendMail({
from: `"Admin" <any_address#heroku.com>`,
to: 'receiver#hotmail.com',
replyTo: 'your_gmail#gmail.com',
subject: 'Something',
html: `Boom !`
});
I think that's all, in case I forgot something, please add a comment below
Try updating nodemailer package (using "npm update nodemailer" command)
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: 'youremai#gmail.com', // Your email id
pass: 'pwd123' // Your password
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
}
});
router.post('/send',function(req,res){
var mailOptions = {
from: 'Foo Bar ✔ <foobar#gmail.com>',
to: req.body.email,
subject: "Hello " + req.body.email,
text: 'Hello ' + req.body.email + '✔',
html: "<p>Hello " + req.body.email + " </p>",
bcc: "fred#gmail.com"
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Message sent: ' + info.response);
res.send(200);
}
});
});
I know I'm too late but hopefully someone will find it helpful.
At the time of writing, Less Secure Apps is no longer supported by google.
And you can't use your google account password.
You're gonna have to generate a new app password.
Follow this link
In the "Select App" dropdown, select Other and give the app a name (could be anything)
Copy the password and replace it with your original password
It works on Local machine and Heroku.
const client = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "username#gmail.com",
pass: "Google-App-Password-Without-Spaces"
}
});
client.sendMail(
{
from: "sender",
to: "recipient",
subject: "Sending it from Heroku",
text: "Hey, I'm being sent from the cloud"
}
)
App passwords only work if 2-step verification is turned on. You can do so here
Also if your app password is in your .env file you have to add it to the config vars in the settings tab of your app at heroku.

Resources