把手作为电子邮件模板

时间:2017-11-06 16:06:21

标签: node.js handlebars.js sendmail

我刚刚继承了一个代码库,它使用把手作为电子邮件模板语言。

我已经google了一下试图获取更多信息,但我找不到其他人这样做。

我只是想知道是否有人可以提供一些文档或搜索条件来寻找。我不知道你甚至可以使用这样的把手!

谢谢,

奥利

电子邮件发件人

// Send new account email 
function sendNewAccountEmail(expert) {   
  ...
  return handlebars.render('views/emails/newAccountEmail.handlebars', {
    name: `${expert.firstName} ${expert.lastName}`,
    layout: false,
    expert,
    url: `${LIVE_URL}/expert/reset/${expert.resetPasswordToken}`,   
}).then(email => new Promise((resolve, reject) => {
      sendmail({
        from: SEND_EMAIL,
        to: recipient,
        subject: '',
        text: email,
      }, (err, reply) => {
        ...
      });
    })); }

newAccountEmail.handlebars

Hi {{name}},

You now have access to RARA Survey tool!
You can now access your dashboard and assigned campaigns by going to the following link and creating a password:

Login URL: {{url}}

Thanks!

Influencer Team

2 个答案:

答案 0 :(得分:5)

要根据文件.hbs作为模板发送的电子邮件,必须使用NPM的包安装:

  1. nodemailer
  2. nodemailer快车车把
  3. 将设置主机信息:

        var transport = nodemailer.createTransport({
            host: 'YOUR HOST',
            port: 'YOUR PORT',
            auth: {
                user: 'YOUR USER',
                pass: 'YOUR PASSWORD'
            },
            tls: {
                rejectUnauthorized: false
            }
        });
    

    现在,我们需要配置传输到它能够使用模板:

        transport.use('compile', hbs({    
            viewPath: 'YOUR PATH where the files are, for example /app/view/email',
            extName: '.hbs'
        }));
    
    
    
        exports.sendEmail = function (from, to, subject, callback) {
    
            var email = {
                from: 'YOUR FROM FOR EXAMPLE YOU@GMAIL.COM',
                to: 'RECIPIENT',
                subject: 'SUBJECT',
                template: 'TEMPLATE NAME, DO NOT NEED TO PLACE  .HBS',
                context: {
                    name: 'YOUR NAME',
                    url: 'YOUR URL'
                }
            };
    
            transport.sendMail(email, function (err) {
                if (err) {
                    return callback({ 'status': 'error', 'erro': err });
                }
                else {
                    return callback({ 'status': 'success' });
                }
            })
        };
    

答案 1 :(得分:4)

请记住,把手只是一种模板语言。你的代码正在做的是它采用.handlebars模板,传递一些变量,这些变量将填充到模板中并将其编译为html,这是你的email变量。然后,您可以使用email html并使用sendmail功能实际发送电子邮件。您可以查看完整文档here