I have been trying to use mail gun with nodemailer for the email service.
This is what my auth.js file looks like:
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, res, next) => {
res.render("auth/signup", {
isAuthenticated: false,
});
};
exports.postLogin = (req, res, 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, res, next) => {
req.session.destroy((err) => {
console.log(err);
res.redirect("/");
});
};
exports.postsignup = (req, res, 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@example.com",
to: "nishitsadual@gmail.com",
replyTo: "reply-to@example.com",
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);
});
};
However, I am getting an error that mg is not a function
. My dependencies look as follows:
$ npm ls --depth=0
+-- bcryptjs@2.4.3
+-- body-parser@1.19.0
+-- connect-flash@0.1.1
+-- connect-session-sequelize@7.0.1
+-- csrf@3.1.0
+-- csurf@1.11.0
+-- ejs@3.1.3
+-- express@4.17.1
+-- express-session@1.17.1
+-- express-session-sequelize@2.3.0
+-- mailgun-nodemailer-transport@1.4.0
+-- mysql2@2.1.0
+-- nodemailer@6.4.11
+-- request@2.88.2
+-- save@2.4.0
+-- sequelize@6.3.1
My example is basically a copy-paste of the official quickstart example .
For some reason, requiring nodemailer-mailgun-transport
does not work as expected.
Any help would be appreciated.
After taking a quick look at your npm dump, it appears that your mailgun package for some reason is called mailgun-nodemailer-transport
, not nodemailer-mailgun-transport
.
I'd try changing the line with:
var mg = require("nodemailer-mailgun-transport");
to
var mg = require("mailgun-nodemailer-transport");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.