提前感谢您的帮助。我在连接到联系表单的 Nodemailer 和 SMTP 电子邮件方面遇到问题。当我使用带有应用程序密码的 gmail 时,它在我的本地版本和 vercel 版本上都有效,但对于新电子邮件则不起作用。新电子邮件现在只能在本地使用,在生产版本上提交表单时,我收到 500 错误
这是我的电子邮件 API 代码:
import { type NextRequest, NextResponse } from 'next/server';
import nodemailer from 'nodemailer';
export async function POST(request: NextRequest) {
try {
const { email, name, message } = await request.json();
const htmlContent = `
<html>
<head>
<style>
/* You can add CSS styles here for the email content */
body {
font-family: Arial, sans-serif;
font-size: 16px;
}
.container {
max-width: 600px;
margin: 0 auto;
}
.subject {
color: #b02d1f;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<h2 class="subject">New Message From Contact Form</h2>
<p><strong>Name:</strong> ${name}</p>
<p><strong>Email:</strong> ${email}</p>
<p><strong>Message:</strong> ${message}</p>
</div>
</body>
</html>
`;
const transport = nodemailer.createTransport({
host: "example.prod.iad2.secureserver.net",
port: 465,
secure: true,
auth: {
user: process.env.MY_EMAIL,
pass: process.env.MY_PASSWORD,
},
});
const mailOptions = {
from: process.env.MY_EMAIL,
to: process.env.MY_EMAIL,
subject: `New Message from ${name} (${email})`,
html: htmlContent,
replyTo: email,
};
await new Promise((resolve, reject) => {
transport.sendMail(mailOptions, function (err) {
if (!err) {
resolve('Email sent!');
} else {
reject(err);
}
});
});
return NextResponse.json({ message: 'Email sent' });
} catch (err) {
return NextResponse.json({ error: err.message || "An error occurred" }, { status: 500 });
}
}
发送电子邮件.ts:
import { FormData } from '@/components/ContactForm';
export function sendEmail(data: FormData) {
const apiEndpoint = '/api/email';
fetch(apiEndpoint, {
method: 'POST',
body: JSON.stringify(data),
})
.then((res) => res.json())
}
我需要将电子邮件直接转至 SMTP 服务器电子邮件。我还尝试了“secure: false”和“port: 587”,但出现同样的问题。如果我使用nodemailer,我是否会被迫只使用gmail?如果我的联系表格代码,请告诉我。谢谢!
Vercel 对出站连接有限制,特别是对某些端口(如 25,通常用于 SMTP)。这可能会导致您的应用程序出现超时错误。