【问题标题】:nodemailer-express-handlebars | Error: ENOENT: no such file or directory for template !! node.jsnodemailer-express-handlebars |错误:ENOENT:模板没有这样的文件或目录!节点.js
【发布时间】:2021-09-11 18:45:27
【问题描述】:
  1. 我正在尝试使用 nodemailer 和 express-handlebars 发送模板表单节点 js,但我收到错误 no such file 我不知道我缺少什么

  2. 我正在附加我的 index.js

const express = require('express')

const app = express()
const bodyParser = require('body-parser')
const hb = require('nodemailer-express-handlebars')
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())
 const nodemailer = require('nodemailer');
 const { google } = require('googleapis');
 const path = require('path');

// These id's and secrets should come from .env file.
 const CLIENT_ID = 'the id';
 const CLEINT_SECRET = 'the secret';
 const REDIRECT_URI = 'uri';
 const REFRESH_TOKEN = 'the token';

 const oAuth2Client = new google.auth.OAuth2(
   CLIENT_ID,
   CLEINT_SECRET,
   REDIRECT_URI
 );
 oAuth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });



app.post("/api", async (req,res) => {
    try{
    const accessToken = await oAuth2Client.getAccessToken();

    const transport = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        type: 'OAuth2',
        user: 'your [enter image description here][1]emial',
        clientId: CLIENT_ID,
        clientSecret: CLEINT_SECRET,
        refreshToken: REFRESH_TOKEN,
        accessToken: accessToken,
      },
    })
    
    const handlebarOptions = {
        viewEngine: {
          extName: ".handlebars",
          partialsDir: path.resolve(__dirname, "emialTemplate"),
          defaultLayout: false,
        },
        viewPath: path.resolve(__dirname, "emialTemplate"),
        extName: ".handlebars",
      };
      
      transport.use(
        "compile",
        hb(handlebarOptions)
      );

   // the data which we going to send

    const mailOptions = {
        from: req.body.name + '<ummed.gagrana@gmail.com>',
        to: 'ummed.gagrana@gmail.com',
        subject: req.body.subject,
        text: "From:" + req.body.email + "\n Message:" + req.body.message,
        //   html: '<h2>From:</h2>'+'<h4>'+req.body.email+"</h4> <br>"+"<h2>Message:</h2>"+'<h4>'+req.body.message+"</h4>",
        template: 'comeBack'
    };
   //sending mail

       const result = await transport.sendMail(mailOptions);

   // checking the result after sending mail
        console.log(result)
        res.send({hey:"well done you just paased some data"})

      } catch (error) {
        console.log(error)
      }
    
})

app.listen(3000, () => {
    console.log("server up and running on port 3000")
})
  1. 这个代码我不确定我缺少什么我是 nodejs 的初学者所以请帮助
  2. 我附上我的工作目录路径以寻求帮助

[]

【问题讨论】:

    标签: html node.js express nodemailer express-handlebars


    【解决方案1】:

    你有一个错字。 emialTemplate --> emailTemplate

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

    之后,我将返回以下代码行,告诉服务器该代码已按应有的方式处理。

    return res.send({hey:"well done you just paased some data"})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2017-10-07
      • 1970-01-01
      • 2016-09-01
      • 2019-06-29
      相关资源
      最近更新 更多