Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending HTML emails from Node.JS using MailGun

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: 'Welcome',
              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: 'Welcome',
              html: welcomeToSiteEmail.html
           };

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

like image 853
Skywalker Avatar asked Apr 21 '16 09:04

Skywalker


2 Answers

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

The mailgun-js docs include an example:

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

var mail = mailcomposer({
  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;
        }
    });
});
like image 143
duncanhall Avatar answered Oct 10 '22 05:10

duncanhall


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.org',
      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.

like image 38
Hayden Braxton Avatar answered Oct 10 '22 03:10

Hayden Braxton