Import HTML Nodemailer

Asked

Viewed 252 times

1

I’m using the nodemailer for sending emails on my server HTML in a variable, I wonder if it is possible to leave saved in a arquivo.html and just call his content 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 in full htmlthat I will send.

1 answer

1


Yes, you can do it with HTML using email-templates https://www.npmjs.com/package/email-templates.

Follow a model example:

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 above example, the file path is in /templates/Welcome, and inside this folder has a file html.html.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.