这里不过多赘述,网上教程很多。
在dashboard打开域名控制台,并选择电子邮件进行如下操作
这里什么都不填,直接点这里:
进到resend.com注册后这样操作
这里填入你的域名再点add
再按照他的设置配置好cloudflare的dns,网上也有很多教程
配置好后点击上面的verify就行了
然后点击api-key,生成后保存好
回到dashboard
现在左侧会有一个代码框
删除原来的并把此代码填入:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
})
async function handleRequest(request) {
if (request.method === 'POST') {
const { senderName, username, to, subject, html } = await request.json();
// 固定的 API 密钥
const apiKey = '<你的密钥>';
// 构建发件人邮箱
const fromEmail = `${username}@<你的域名>`;
const requestBody = {
from: `${senderName} <${fromEmail}>`, // 设置显示的发件人名称
to: [to],
subject: subject,
html: html
};
const response = await fetch('https://api.resend.com/emails', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
});
const data = await response.json();
if (data.success) {
return new Response(JSON.stringify({ message: '邮件发送成功!' }), { status: 200 });
} else {
return new Response(JSON.stringify({ error: `邮件发送失败: ${data.error}` }), { status: 400 });
}
} else {
// 返回 HTML 表单页面
return new Response(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Send Email via Resend API</title>
<style>
body { font-family: 'Arial', sans-serif; background-color: #f4f7fc; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; }
.container { background-color: #fff; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); width: 400px; padding: 30px; }
h1 { text-align: center; color: #333; font-size: 24px; margin-bottom: 20px; }
label { display: block; font-size: 14px; margin-bottom: 6px; color: #555; }
input[type="email"], input[type="text"], textarea { width: 100%; padding: 10px; margin-bottom: 16px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px; color: #333; }
textarea { height: 120px; resize: vertical; }
button { width: 100%; padding: 12px; background-color: #5c6bc0; color: #fff; border: none; border-radius: 4px; font-size: 16px; cursor: pointer; transition: background-color 0.3s; }
button:hover { background-color: #3f4a90; }
.success-message, .error-message { text-align: center; font-weight: bold; margin-top: 20px; }
.success-message { color: #4caf50; }
.error-message { color: #f44336; }
.form-group { margin-bottom: 16px; }
</style>
</head>
<body>
<div class="container">
<h1>发送邮件</h1>
<form id="emailForm">
<div class="form-group">
<label for="senderName">发件人名称:</label>
<input type="text" id="senderName" required>
</div>
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" id="username" required>
</div>
<div class="form-group">
<label for="toEmail">收件人邮箱:</label>
<input type="email" id="toEmail" required>
</div>
<div class="form-group">
<label for="subject">主题:</label>
<input type="text" id="subject" required>
</div>
<div class="form-group">
<label for="htmlContent">内容:</label>
<textarea id="htmlContent" required></textarea>
</div>
<button type="submit">发送邮件</button>
</form>
<div id="message"></div>
</div>
<script>
document.getElementById('emailForm').addEventListener('submit', async function(event) {
event.preventDefault();
const senderName = document.getElementById('senderName').value;
const username = document.getElementById('username').value;
const toEmail = document.getElementById('toEmail').value;
const subject = document.getElementById('subject').value;
const htmlContent = document.getElementById('htmlContent').value;
const response = await fetch('/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ senderName, username, to: toEmail, subject, html: htmlContent })
});
const data = await response.json();
const messageDiv = document.getElementById('message');
if (response.ok) {
messageDiv.innerHTML = '<div class="success-message">' + data.message + '</div>';
} else {
messageDiv.innerHTML = '<div class="error-message">' + data.error + '</div>';
}
});
</script>
</body>
</html>
`, { headers: { "Content-Type": "text/html" } });
}
}
紧接着修改一下11行和13行的apikey和域名,点击部署
这个可能有泄露风险
欢迎大佬指点
点击访问
发送后错误信息如果是undefine就算成功了!!!
完成