Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling NodeMailer Email

Below I have referenced an example from the NodeMailer package in NodeJS. It is designed to automate email sending. As you can see, in the mailOptions object, you are able to write HTML into the message body. This is an awesome feature that I plan on using.

However, I do have a question on how I might style this HTML. Are there any styling options that I can use on this HTML and how would i go about this. The most logical way I would think would be inline styling.

Has anybody tried this before and had any success?

var nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
var transporter = 
nodemailer.createTransport('smtps://user%40gmail.com:[email protected]');

// setup e-mail data with unicode symbols
var mailOptions = {
    from: '"Fred Foo ?" <[email protected]>', // sender address
    to: '[email protected], [email protected]', // list of receivers
    subject: 'Hello ✔', // Subject line
    text: 'Hello world ?', // plaintext body
    html: '<b>Hello world ?</b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
});
like image 235
Hysii Avatar asked Oct 20 '25 14:10

Hysii


2 Answers

I will provide you with some hints, hopefully you should be able to implement it in your project as well.

First things first, you would need an EJS (there are other template engines around). You would required it like this in your project:

const ejs = require('ejs');

I have implementation in a class, that loads desired template dynamically, and then feeds it data.

This is how my render method (that outputs the HTML ) looks like:

renderTemplate(template, parameters, options) {
        return ejs.render(template, parameters, options);
    }

You could check a EJS for more details. That would be very basic implementation, that should get you going in good direction. Later on, you can include the headers or footers for that matter.

Have fun

like image 200
Wexoni Avatar answered Oct 22 '25 02:10

Wexoni


For styling emails, you would want to either write the styling in-line, or it looks like there is some NPM packages for helping with this. (nodemailer-juice), looks like it will take your css file and make it inline for you.

like image 33
ChrisBurns Avatar answered Oct 22 '25 02:10

ChrisBurns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!