如何解决nodemailer-express-handlebars |错误:ENOENT:模板没有这样的文件或目录!!节点.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,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")
})
[
]版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。