我正在尝试使用 Mailgin,因为电子邮件提供商使用电子邮件发送可用于登录的“神奇链接”。
import clientPromise from "@/app/lib/connectDb";
import NextAuth from "next-auth";
import EmailProvider from "next-auth/providers/email";
import { MongoDBAdapter } from "@next-auth/mongodb-adapter";
const handler = NextAuth({
adapter: MongoDBAdapter(clientPromise),
providers: [
EmailProvider({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
maxAge: 10 * 60, // Magic links are valid for 10 min only
}),
],
});
export { handler as GET, handler as POST };
环境变量是:
MAIL_SERVER_HOST="smtp.mailgun.org"
EMAIL_SERVER_PORT="587"
EMAIL_SERVER_USER="postmaster@mydomain"
EMAIL_SERVER_PASSWORD="key-**********"
我收到错误
message: 'Invalid login: 535 Authentication failed'
已修复。我犯了一些错误。我使用了错误的 SMTP 凭据。在 Mailgun 仪表板中,我创建了新的 SMTP 用户,然后使用用户名和新生成的密码
MAIL_SERVER_HOST="smtp.mailgun.org"
EMAIL_SERVER_PORT=587
EMAIL_SERVER_USER="postmaster@mydomain" // ex. [email protected] - my domain was setup as mg.mydomain.com
EMAIL_SERVER_PASSWORD=****** // SMTP user password not api key that I have used earlier "key-**********" ( wrong)
鉴于您可能不会在该文件之外重用所有 4 个 SMTP 环境变量(或在整个应用程序中单独导入它们),您可以按如下方式简化
EmailProvider
的服务器配置:
EmailProvider({
from: process.env.EMAIL_FROM,
server: process.env.EMAIL_SERVER,
maxAge: 10 * 60, // Magic links are valid for 10 min only
}),
然后巩固环境变量:
# Email Sender Info
EMAIL_FROM=My App <[email protected]>
# Magic Link Email Server
EMAIL_SERVER=smtp://[email protected]:[email protected]:587
很高兴知道:如果您的 Mailgun 帐户托管在欧洲,则主机将变为:
smtp.eu.mailgun.org