使用node.js,node mailer和nodemailer-mailgun-transport发送模板化电子邮件

时间:2015-07-21 08:08:38

标签: express handlebars.js mailgun nodemailer transport

我有以下基本nodejs app:

var nodemailer = require('nodemailer');
var hbs = require('nodemailer-express-handlebars');
var options = {
 viewEngine: {
     extname: '.hbs',
     layoutsDir: 'views/email/',
     defaultLayout : 'template',
     partialsDir : 'views/partials/'
 },
 viewPath: 'views/email/',
 extName: '.hbs'
};

var mg = require('nodemailer-mailgun-transport');

var auth = {
    auth: {
        api_key: ' mailgun api key ',
        domain: ' mailgun email domain '
    }
}

var mailer = nodemailer.createTransport(mg(auth));

mailer.use('compile', hbs(options));
mailer.sendMail({
 from: 'test@inventori.io',
 to: 'test@test.com',
 subject: 'Any Subject',
 template: 'email.body',
 context: {
      variable1 : 'value1',
      variable2 : 'value2'
 }
}, function (error, response) {
    // console.error(error);
    if (error) {
        throw error;
    };
 console.log('mail sent to ',response);
 mailer.close();
});

视图/电子邮件/ template.hbs

{{>email/head}}
<body>
     {{>email/header}}
     {{{body}}}
     {{>email/footer}}
</body>
</html>

视图/电子邮件/ email.body.hbs

<h4>Main Body Here</h4>
{{variable1}} <br/>
{{variable2}}

视图/分音/电子邮件/ header.hbs

<h4>Header Content</h4>

视图/分音/电子邮件/ footer.hbs

<h4>Footer Content</h4>

把手模板引擎没有出现错误,但是mailgun传输引发了以下错误:

Error: Sorry: template parameter is not supported yet. Check back soon!
    at IncomingMessage.<anonymous> (~/test/node_modules/nodemailer-mailgun-transport/node_modules/mailgun-js/lib/request.js:228:15)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)  

此示例使用gmail节点邮件传输:

http://excellencenodejsblog.com/express-nodemailer-sending-mails/

我希望能够使用mailgun发送模板化电子邮件。

非常感谢任何帮助。

谢谢。

2 个答案:

答案 0 :(得分:2)

template参数更改为html

如果您查看source code here,错误是正确的 - 那么template参数就不存在了。

答案 1 :(得分:0)

对我来说,模板无法正确呈现,因为当密钥实际上为extName(全部为小写字母)时,我正在遵循使用extname的示例。也许是将其重命名为加班,而我正在查看的指南现在有些过时了。

以下截至2020年5月30日的完整示例。

目录结构:

root/
   src/
      email-templates/
              layouts/
                     blank.hbs
              partials/
              hello.hbs
      services/
              email.service.ts

email.service.ts (尚未将其更新为使用正确的类型。只是一个poc。)

export async function sendTestEmail() {
  try {
    // Generate test SMTP service account from ethereal.email
    // Only needed if you don't have a real mail account for testing
    const testAccount = await nodemailer.createTestAccount()

    // create reusable transporter object using the default SMTP transport
    const transporter = nodemailer.createTransport({
      host: 'smtp.ethereal.email',
      port: 587,
      secure: false, // true for 465, false for other ports
      auth: {
        user: testAccount.user, // generated ethereal user
        pass: testAccount.pass, // generated ethereal password
      },
    })

    transporter.use('compile', hbs({
      viewEngine: {
        extname: '.hbs', // handlebars extension
        partialsDir: 'src/email-templates',
        layoutsDir: 'src/email-templates/layouts',
        defaultLayout: 'blank',
      },
      viewPath: 'src/email-templates',
      extName: '.hbs'
    }))

    // send mail with defined transport object
    const mailOptions = {
      from: 'test@gmail.com', // sender address
      to: 'test@gmail.com', // list of receivers
      subject: 'Hello ✔', // Subject line
      text: 'Hello world?', // plain text body
      template: 'hello',
      context: {
        firstName: 'Clem'
      }
    }
    const info = await transporter.sendMail(mailOptions)

    console.log('Message sent: %s', info.messageId)
    // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>

    // Preview only available when sending through an Ethereal account
    console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info))
    // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
  } catch (error) {
    console.log(error)
  }
}