Node - options for sending email, sendgrid and nod

2019-07-04 04:59发布

问题:

Not so experienced with sending email in node and as i can see there are different option to choose among. Which option to use in following scenario?

Using node-mailer with sengrid:
As i can see i must add additional package "nodemailer-sendgrid-transport" to force them to work together.
Because var transporter = nodemailer.createTransport({ service: 'SendGrid', auth: {...}); won't work. (Always returning Bad username / password)

var nodemailer        = require('nodemailer'),
    sendGridTransport = require('nodemailer-sendgrid-transport');

var options = {
    auth: {
        api_user: 'blabla',
        api_key:  'blabla'
    }
};
var client = nodemailer.createTransport(sendGridTransport(options));

var email = {
    to: ['joe@foo.com', 'mike@bar.com'],
    from: 'roger@tacos.com',
    subject: 'Hi there',
    text: 'Awesome sauce',
    html: '<b>Awesome sauce</b>'
};

client.sendMail(email, function(err, info) { ... });

Using just sendgrid module without nodemailer package:

var sendgrid = require('sendgrid')('blabla', 'blabla');

var email = {
    to: ['joe@foo.com', 'mike@bar.com'],
    from: 'roger@tacos.com',
    subject: 'Hi there',
    text: 'Awesome sauce',
    html: '<b>Awesome sauce</b>'
};

sendgrid.send(email, function(err, json) { ... });

So with second example i will include just one package (sengrid), with first approach i must include two packages (nodemailer and nodemailer-sendgrid-transport). I see that first approach is bigger abstraction over second in that i can update or switch entirely to some other service-transport (gmail for example) or can i? But in other hand i don't see situation where it would be appropriate to switch between different transport objects.

Side question appart all of this is how to use templates with this solutions. I dont see in official guides anything about templates nor at: https://github.com/sendgrid/sendgrid-nodejs nor at: https://github.com/andris9/Nodemailer:

Email templates are whole different story apart this and i need to install new package which will deal with it?
In the end I feel like I'm posted very retarded question.

回答1:

I have used nodemailer with Mandrill without any problems.

This is the code that works in my case, but you mentione not in yours:

// Create a SMTP transport object
var transport = nodemailer.createTransport("SMTP", {
    service: mainConfigFile.smtpServer.service, // use well known service
    auth: {
        user: mainConfigFile.smtpServer.user,
        pass: mainConfigFile.smtpServer.pass
    }
});

Please note that credentials are stored in mainConfigFile, but it is clear what is the intention.

I think you shoudl be ok with nodemailer.

Can you provide more details regarding the error you receive?