我想通过我的 Next js 网站向用户发送交易电子邮件,用于通讯和重置密码目的,我正在使用带有 SMTP 凭据的 Nodemailer。使用了 Mailgun、Sendinblue、MSG91、Postmark(都是免费计划)但我的邮件要么积压要么排队。
我也试过 Sendinblue api 但还是遇到了同样的问题,收件人的邮箱里没有一封邮件。您可以在下面看到完整的响应和 API;
这是我的API,跳过注释逻辑直接跳转到nodemailer部分
import ConnectDB from "@/utils/connect_db"
import User from "@/models/user"
const nodemailer = require('nodemailer');
const jwt = require("jsonwebtoken")
const forgotPassword = async (req, res) =\> {
try {
if (req.method === 'POST') {
await ConnectDB()
// let user = await User.findOne({ email: req.body.email })
// if (!user) user = await User.findOne({ username: req.body.email }) //because user can put the username or email in the same field and api should verify from both ways
// if (!user) return res.status(404).json({ success: false, msg: "You don't have an account with this email!" })
// const token = jwt.sign({ id: user._id }, process.env.SECRET_KEY, { expiresIn: '2m' })
// USING Postmark
const transport = nodemailer.createTransport({
host: "smtp.postmarkapp.com",
port: 587,
auth: {
user: "34dd6ee8-8015-444a-92e5-25194cf742cc",
pass: "34dd6ee8-8015-444a-92e5-25194cf742cc"
}
});
// create email message object
const message = {
from: '[email protected]',
to: '[email protected]',
text: "hellow this is a text email body to check the email service",
subject: 'You subscribed our Newsletter',
html: `<html><body><h1>Fitte Moooo!</h1><p>Dear valuable Jheengu this email is to notify you that you have successfully recieved the 'Fitte Moo' reward for literally just existing at this time of morning.</p></body></html>`
};
// send email using nodemailer
let info = await transport.sendMail(message);
console.log(info)
res.json({ success: true, info })
}
else {
res.status(400).json({ success: false, msg: "bad request, you are using wrong request method!" })
}
}
catch (error) {
console.log(error)
res.status(500).json({ success: false, msg: "Internal server error occurred, pleae try again later" })
}
}
export default forgotPassword
这是我点击 api 时的完整回复
{
"success": true,
"info": {
"accepted": [
"[email protected]"
],
"rejected": [],
"ehlo": [
"PIPELINING",
"SIZE 20480000",
"VRFY",
"ETRN",
"AUTH PLAIN LOGIN CRAM-MD5 DIGEST-MD5",
"AUTH=PLAIN LOGIN CRAM-MD5 DIGEST-MD5",
"ENHANCEDSTATUSCODES",
"8BITMIME",
"DSN"
],
"envelopeTime": 622,
"messageTime": 452,
"messageSize": 858,
"response": "250 2.0.0 Ok: queued as 3B28D414C40",
"envelope": {
"from": "[email protected]",
"to": [
"[email protected]"
]
},
"messageId": "<[email protected]>"
}
}
我在 Postmark 工作,虽然我的技术不足以解决您的代码问题,但我可以提供一些其他见解:如果您使用 Postmark 免费计划,您将只能将电子邮件发送到您自己的域,直到您的帐户已获批准(这里有更多详细信息)。否则您的电子邮件将像您在此处看到的那样排队。我们的支持团队也很乐意提供帮助——您可以通过 [email protected] 与他们联系。