feat: implement email sending functionality via Resend service

This commit is contained in:
beilunyang
2025-06-21 23:50:46 +08:00
parent 9d55564073
commit e85f6b04bd
27 changed files with 2347 additions and 467 deletions

View File

@@ -7,12 +7,11 @@ interface Env {
DB: D1Database
}
const MAX_EMAIL_COUNT = 5
const MAX_MESSAGE_COUNT = 50
const MAX_EMAIL_COUNT = 10
const MAX_MESSAGE_COUNT = 100
const BATCH_SIZE = 10 // SQLite 变量限制
async function getUserId(db: ReturnType<typeof drizzle>, identifier: string): Promise<string | null> {
// 尝试通过 email 查找用户
let user = await db
.select()
.from(users)
@@ -20,7 +19,6 @@ async function getUserId(db: ReturnType<typeof drizzle>, identifier: string): Pr
.limit(1)
.then(rows => rows[0])
// 如果没找到,尝试通过 username 查找
if (!user) {
user = await db
.select()
@@ -38,13 +36,11 @@ async function generateTestData(env: Env, userIdentifier: string) {
const now = new Date()
try {
// 获取用户 ID
const userId = await getUserId(db, userIdentifier)
if (!userId) {
throw new Error(`未找到用户: ${userIdentifier}`)
}
// 生成测试邮箱
const testEmails = Array.from({ length: MAX_EMAIL_COUNT }).map(() => ({
id: crypto.randomUUID(),
address: `${nanoid(6)}@moemail.app`,
@@ -53,34 +49,49 @@ async function generateTestData(env: Env, userIdentifier: string) {
expiresAt: new Date(now.getTime() + 24 * 60 * 60 * 1000),
}))
// 插入测试邮箱
const emailResults = await db.insert(emails).values(testEmails).returning()
console.log('Created test emails:', emailResults)
// 为每个邮箱生成测试消息
for (const email of emailResults) {
const allMessages = Array.from({ length: MAX_MESSAGE_COUNT }).map((_, index) => ({
const receivedMessages = Array.from({ length: Math.floor(MAX_MESSAGE_COUNT * 0.7) }).map((_, index) => ({
id: crypto.randomUUID(),
emailId: email.id,
fromAddress: `sender${index + 1}@example.com`,
subject: `Test Message ${index + 1} - ${nanoid(6)}`,
content: `This is test message ${index + 1} content.\n\nBest regards,\nSender ${index + 1}`,
toAddress: null,
subject: `Received Message ${index + 1} - ${nanoid(6)}`,
content: `This is received message ${index + 1} content.\n\nBest regards,\nSender ${index + 1}`,
html: `<div>
<h1>Test Message ${index + 1}</h1>
<p>This is test message ${index + 1} content.</p>
<h1>Received Message ${index + 1}</h1>
<p>This is received message ${index + 1} content.</p>
<p>With some <strong>HTML</strong> formatting.</p>
<br>
<p>Best regards,<br>Sender ${index + 1}</p>
</div>`,
type: 'received',
receivedAt: new Date(now.getTime() - index * 60 * 60 * 1000),
}))
// 分批插入消息
const sentMessages = Array.from({ length: Math.floor(MAX_MESSAGE_COUNT * 0.3) }).map((_, index) => ({
id: crypto.randomUUID(),
emailId: email.id,
fromAddress: null,
toAddress: `recipient${index + 1}@example.com`,
subject: `Sent Message ${index + 1} - ${nanoid(6)}`,
html: `This is sent message ${index + 1} content.\n\nBest regards,\n${email.address}`,
content: '',
type: 'sent',
sentAt: new Date(now.getTime() - index * 60 * 60 * 1000),
}))
const allMessages = [...receivedMessages, ...sentMessages]
for (let i = 0; i < allMessages.length; i += BATCH_SIZE) {
const batch = allMessages.slice(i, i + BATCH_SIZE)
await db.insert(messages).values(batch)
console.log(`Created batch of ${batch.length} messages for email ${email.address}`)
console.log(`Created batch of ${batch.length} messages (received + sent) for email ${email.address}`)
}
console.log(`Email ${email.address}: ${receivedMessages.length} received, ${sentMessages.length} sent messages`)
}
console.log('Test data generation completed successfully!')