8

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.

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.com/mailgun/node-prelaunch has a working solution where they combine 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. Commented Sep 10, 2016 at 1:56

2 Answers 2

12

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;
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

7

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.

3 Comments

Great extra tips - I've used Litmus for a long time - what a time saver! Also I just found this for storing the auth credentials: npmjs.com/package/buttercup
I had to use "pass" not "password" for the SMPT transport options.
I would strongly second Hayden Braxton I use mailchimp to layout out HTML - their wysiwyg email creator is excellent and renders very well. You can send yourself a test email and then view the HTML to extract it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.