问题描述
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 (将#修改为@)