问题描述
我一直在尝试使用带有nodemailer的邮件枪来提供电子邮件服务。
这是我的auth.js文件的样子:
const bcrypt = require("bcryptjs");
const User = require("../models/user");
var nodemailer = require("nodemailer");
var mg = require("nodemailer-mailgun-transport");
var auth = {
auth: {
api_key: "****",domain:"xyz"
},};
var nodemailerMailgun = nodemailer.createTransport(mg(auth));
exports.getLogin = (req,res,next) => {
let message = req.flash("error");
if (message.length > 0) {
message = message[0];
} else {
message = null;
}
console.log(req.flash("error"));
res.render("auth/login",{
isAuthenticated: false,errorMessage: message,});
};
exports.getsignup = (req,next) => {
res.render("auth/signup",});
};
exports.postLogin = (req,next) => {
const email = req.body.email;
const password = req.body.password;
User.findOne({ email: email })
.then((user) => {
if (!user) {
req.flash("error","Invalid email or password.");
return res.redirect("/login");
}
bcrypt
.compare(password,user.password)
.then((doMatch) => {
if (doMatch) {
req.session.isLoggedIn = true;
req.session.user = user;
return req.session.save((err) => {
console.log(err);
res.redirect("/");
});
}
req.flash("error","Invalid email or password.");
res.redirect("/login");
})
.catch((err) => {
console.log(err);
res.redirect("/login");
});
})
.catch((err) => console.log(err));
};
exports.postLogout = (req,next) => {
req.session.destroy((err) => {
console.log(err);
res.redirect("/");
});
};
exports.postsignup = (req,next) => {
const email = req.body.email;
const password = req.body.password;
User.findOne({ email: email })
.then((userDoc) => {
if (userDoc) {
return res.redirect("/signup");
}
return bcrypt
.hash(password,12)
.then((hashedPassword) => {
const user = new User({
email: email,password: hashedPassword,cart: { items: [] },});
return user.save();
})
.then((result) => {
nodemailerMailgun
.sendMail({
from: "[email protected]",to: "[email protected]",replyTo: "[email protected]",subject: "Mailgun Transport",text: "This is text content",})
.then((info) => {
console.log("SUCCESS");
})
.catch((error) => {
console.log("Something is wrong");
});
res.redirect("/login");
});
})
.catch((err) => {
console.log(err);
});
};
但是,我收到一个错误mg is not a function
。我的依存关系如下:
$ npm ls --depth=0
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
我的示例基本上是copy-paste of the official quickstart example。
由于某些原因,要求nodemailer-mailgun-transport
不能按预期工作。
任何帮助将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)