Import HTML Nodemailer

0

I am using nodemailer to send emails on my server node, however I did not like to store all HTML in a variable, I would like to know if it is possible to leave saved in a arquivo.html and just call the content of it to send that email. I'm currently using it that way.

conta.sendMail({
                    from: '[email protected]',
                    to: req.body.nome+' <'+req.body.email+'>',
                    subject: 'ASSUNTO',
                    html: html
                }, (err) => {
                    if(err){
                        throw err
                    }else{
                        console.log('Email Enviado')
                    }
                })

The variable html is being declared with all html that I will send.

    
asked by anonymous 11.09.2017 / 15:32

1 answer

1

Yes, you can do with HTML by using the email templates.

Here is a sample template:

var nodemailer = require('nodemailer');
var EmailTemplate = require('email-templates').EmailTemplate;
var welcome = new EmailTemplate(templateDir);
var path = require('path'); 
var templateDir = path.join(__dirname, '..', 'templates', 'welcome'); 


var defaultTransport = nodemailer.createTransport({
 service: 'hotmail',  
 auth: {
    user: [email protected],
    pass: 1234   
}});

module.exports = {

    boasVindas: function(user){
        welcome.render(user, function (err, result) {
            if(err){
                console.log(err);
            }
            else{
                var transport = defaultTransport;
                transport.sendMail({
                    from: "[email protected]",
                    to: user.email,
                    subject: "Bem vindo",
                    html: result.html
                }, function (err, responseStatus) {
                    if (err) {
                        console.log(err)
                    }
                    else{
                        console.log(responseStatus) // email foi enviado
                    }                    
                });
            }            
        });
    },

}

In the example above, the file path is in / templates / welcome, and inside this folder has a html .html file.

    
15.09.2017 / 17:27