Sending HTML Messages from Node.JS Using MailGun

I send email notifications to my users from my application, but currently I only send them as text. I would like to send him HTML letters that are styled.

I have currently 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, he does not like the styles that I have embedded. I created a separate HTML file in my local directory for each type of email that I want to send, and I would like to be able to attach this html file to my email.

Something like that:

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

Is it possible? Any help would be greatly appreciated.

+5
source share
3 answers

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

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: ' you@samples.mailgun.org ', to: ' mm@samples.mailgun.org ', subject: 'Test email subject', body: 'Test email text', html: '<b> Test email text </b>' }); mail.build(function(mailBuildError, message) { var dataToSend = { to: ' mm@samples.mailgun.org ', message: message.toString('ascii') }; mailgun.messages().sendMime(dataToSend, function (sendError, body) { if (sendError) { console.log(sendError); return; } }); }); 
+9
source

Alternatively, you can check nodemailer on npm. This is 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>', password: 'password' } }); transport.sendMail({ from: '<Mailgun SMTP login>', to: [' bob@example.com ', ' bill@foobarbaz.com ', /*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 recommend being very thorough in your html email design. Email html is very different from html on the web. There is a much wider variety of email clients that will do your html differently, and some, like Outlook for Windows and gmail, will not handle your html very well. Litmus contains several useful resources regarding best practices for designing html messages.

My suggestion would be to use a writing base for your style, use inky to simplify the semantics of writing the html email address and inline-css to embed all your styles. Even if you use alternative methods of sending mail, check out these resources to develop it. They will save you a lot of headache.

+4
source

similar to This

read the file first and send it as the body of the HTML.

Try this

-1
source

Source: https://habr.com/ru/post/1247599/


All Articles