最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sending HTML emails from Node.JS using MailGun - Stack Overflow

programmeradmin6浏览0评论

I send email notifications to my users from my app but currently I only send it as a text. I would like to send it HTML emails which are styled.

Currently I tried this:

var data = {
              from: 'my app',
              to: user.email,
              subject: 'Wele',
              html: '<div style="width: 500px; height: 400px: background: #ebebeb; color: #ddd"><p>Hi  + "user.firstName" + \n ,this email is to inform you that has added their bio to the knowledge Base \n</p></div>'
           };

Compiling the above code does not work, it does not like the styles I have put in. I have created a separate HTML file within my local directory for each type of email I want send and I would like to be able to attach that html file to my email.

Something like this:

var data = {
              from: 'my app',
              to: user.email,
              subject: 'Wele',
              html: weleToSiteEmail.html
           };

Is the above possible? Any help will be greatly appreciated.

I send email notifications to my users from my app but currently I only send it as a text. I would like to send it HTML emails which are styled.

Currently I tried this:

var data = {
              from: 'my app',
              to: user.email,
              subject: 'Wele',
              html: '<div style="width: 500px; height: 400px: background: #ebebeb; color: #ddd"><p>Hi  + "user.firstName" + \n ,this email is to inform you that has added their bio to the knowledge Base \n</p></div>'
           };

Compiling the above code does not work, it does not like the styles I have put in. I have created a separate HTML file within my local directory for each type of email I want send and I would like to be able to attach that html file to my email.

Something like this:

var data = {
              from: 'my app',
              to: user.email,
              subject: 'Wele',
              html: weleToSiteEmail.html
           };

Is the above possible? Any help will be greatly appreciated.

Share Improve this question asked Apr 21, 2016 at 9:51 SkywalkerSkywalker 5,19417 gold badges65 silver badges127 bronze badges 1
  • I also ran into this and the mailgun-js module didn't support the very important template property (I believe this is what you want, instead of the html property). I found this github project github./mailgun/node-prelaunch has a working solution where they bine the templating of nodemailer with the mailgun-js transport. If this answers your question, I can pull out the code which specifically handles this and create an answer. – santeko Commented Sep 10, 2016 at 1:56
Add a ment  | 

2 Answers 2

Reset to default 12

You can use mailgun-js together with mailposer to send HTML formatted emails.

The mailgun-js docs include an example:

var domain = 'mydomain.mailgun';
var mailgun = require('mailgun-js')({ apiKey: "YOUR API KEY", domain: domain });
var mailposer = require('mailposer');

var mail = mailposer({
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Test email subject',
  body: 'Test email text',
  html: '<b> Test email text </b>'
});

mail.build(function(mailBuildError, message) {

    var dataToSend = {
        to: '[email protected]',
        message: message.toString('ascii')
    };

    mailgun.messages().sendMime(dataToSend, function (sendError, body) {
        if (sendError) {
            console.log(sendError);
            return;
        }
    });
});

Alternately, you could check out nodemailer on npm. It's a great package: easy to use and extensive documentation. With nodemailer, you can do something like this

var nodemailer = require('nodemailer');

var transport = nodemailer.createTransport({
      host: 'smtp.mailgun',
      port: 587,
      secure: false,
      tls: { ciphers: 'SSLv3' },
      auth: {
        user: '<Mailgun SMTP login>',
        pass: '<Mailgun SMTP password>'
      }
    });

transport.sendMail({
        from: '<Mailgun SMTP login>',
        to: ['[email protected]', '[email protected]', /*etc*/],
        subject: 'Fancy Email',
        text: 'still send some text to be on the safe side',
        html: { path: 'path/to/email.html' }
    }, callback)
// also returns a promise.

However, I would suggest being very thorough in your design of html emails. Writing html email is very different from html in the web. There's a much wider variety of email clients that do will render your html differently, and some, Outlook for Windows and gmail web for example, will not treat your html very nicely. Litmus has some nice resources regarding best practices for designing html emails.

My suggestion would be to use foundation for emails for your styling, use inky to simplify the semantics of writing html email, and inline-css to inline all of your styles. Even if you use alternate methods of sending the mail, check out these resources for designing it. They will save you lots of headache.

发布评论

评论列表(0)

  1. 暂无评论