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);
});
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With