Nodejs send mails - node.js

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.

Related

this is my nodemailer program Even if i give wrong mail in TO addrress also it is showing message sent how to rectifyy that

const nodemailer = require('nodemailer');
let mailTransporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: '*************',
pass: '*********',
},
});
let mailDetails = {
from: '********',
to: 'uuwdwuvw#', //guguygugiug
subject: 'Test mail',
//text: 'redeem your gift',
html: 'redeem your coupon code ',
};
mailTransporter.sendMail(mailDetails, function (err, data) {
if (err) {
console.log('Error Occurs');
} else {
console.log('Email sent successfully');
}
});
this is my nodemailer program Even if i give wrong mail in TO addrress also it is showing message sent how to rectifyy that
Nodemailer itself can't tell if an email has been delivered or not. It can tell you only if the email has been sent. There's a difference between email being sent and being delivered. Detecting bounced emails is out of scope for the Nodemailer.
In order to do that you need to implement your own bounced email mechanism, probably with your own SMTP server. It's not an easy thing to do, so you should probably use some real email provider that has this functionality instead of Gmail (Google likes to block such applications).
Have a look at the similar question I've found on GitHub and on SO.

Use sendgrid from send emails with nodemailer appication gives following error?

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.

How to send an email with nodemailer on AWS Lambda

I'm calling the following code ( with actual email addresses and passwords ) in my AWS Lambda function. Unfortunately, I'm unable to view the logs (my account currently doesn't have the permissions). Is there any specific permissions I have to set up to send an email from a lambda function? Or is there another/better way to send and email?
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'source#gmail.com',
pass: 'paswword'
}
});
var mailOptions = {
from: 'source#gmail.com',
to: 'destination#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);
}
});
Does it work locally? I'd start with that. Your method might be not working because https://www.google.com/settings/security/lesssecureapps feature is disabled.
Assuming you're using HTTP integration, if you cannot see logs, I'd create an array and instead of calling console.log, I'd append each log to that array. At the end of function, I would simply respond with 200 containing that logs.
You can also consider using https://aws.amazon.com/ses/. It's a cloud-based email sending service. It works great with AWS Lambda. In order to get it work, you need to add ses:SendEmail permissions and get through verification process.

Send mail to multiple recipients using nodemailer

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

Send email in Node.js with nodemailer

My target is to be able to send email without setting credentials. For this purpose I've picked up the nodemailer module. Here is my code:
var nodemailer = require('nodemailer');
var message = {
from: "test#gmail.com",
to: "test1#gmail.com",
subject: "Hello ✔",
text: "Hello world ✔",
html: "<b>Hello world ✔</b>"
};
nodemailer.mail(message);
According to documentation the "direct" transport method should be used (actually I don't know nothing about transport methods at all). But unfortunately this method is absolutely unstable - sometimes it works sometimes it doesn't.
Could anyone shed some light on it? How can I send email without configuring SMTP transport credentials?
Sure, but it totally depends on server's configuration. Most email servers will not work unless you use authentication. It is 2017 🙂
Well, AFAIK nodemailer detects the correct configuration based on the email domain, in your example you have not set the transporter object so it uses the default port 25 configured. To change the port specify in options the type. And I highly recommend you to specify it explicitly.
Probably windows firewall or antivirus preventing outgoing access. Try to get debug/error messages. We need something to help you more.
Here is a new version of the nodemailer and here is an example how to use it:
const nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: 465,
secure: true, // secure:true for port 465, secure:false for port 587
auth: {
user: 'username#example.com',
pass: 'userpass'
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Fred Foo 👻" <foo#blurdybloop.com>', // sender address
to: 'bar#blurdybloop.com, baz#blurdybloop.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ?', // plain text body
html: '<b>Hello world ?</b>' // html body
};
// 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 hope it helps.

Resources