簡體   English   中英

使用帶有把手的 nodemailer 來處理動態電子郵件

[英]Using nodemailer with handlebars for dynamic emails

我遇到了一個問題,我似乎無法縮小我做錯了什么的范圍。 當我運行我的代碼時,這是我得到的錯誤:

[Error: ENOENT: no such file or directory, open 'C:\\Users\\Alex\\Desktop\\emailtest\\main.handlebars'] { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'C:\\\\Users\\\\Alex\\\\Desktop\\\\emailtest\\\\main.handlebars' }

這是我的代碼:

const nodemailer = require("nodemailer");
const hbs = require("nodemailer-express-handlebars");
const express = require("express");
const app = express();
const port = 3000;

const transporter = nodemailer.createTransport({
  service: "gmail",
  auth: {
    user: "useremail",
    pass: "userpassword",
  },
  //   tls: {
  //     rejectUnauthorized: false,
  //   },
});

transporter.use(
  "compile",
  hbs({
    viewEngine: "express-handlebars",
    viewPath: "views",
  })
);

const mailOptions = {
  from: "myemailhere",
  to: "receiverpassword",
  subject: "Automated Email",
  template: "index",
};

transporter.sendMail(mailOptions, function (error, info) {
  if (error) {
    console.log(error);
  } else {
    console.log("Email sent: " + info.response);
  }
});

app.listen(port, () => {
  console.log(
    `Listening at http://localhost:${port}`
  );
});

文件夾結構:

server.js
views
    -index.handlebars

在不使用 Handlebars 的情況下,我可以很好地發送電子郵件,但是由於我需要一種動態填充 HTML 的方法,我認為如果我可以讓它正常工作,那么 Handlebars 將是最好的選擇。 任何見解都會很棒,提前致謝!

const path = require('path');

const handlebarOptions = {
    viewEngine: {
        extName: ".handlebars",
        partialsDir: path.resolve(__dirname, "views"),
        defaultLayout: false,
    },
    viewPath: path.resolve(__dirname, "views"),
    extName: ".handlebars",
};

trasnporter.use('compile', hbs(handlebarOptions));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM