MCPcopy Create free account
hub / github.com/PIKACHUIM/CFWorkerACME / mailSend

Function mailSend

src/users.ts:346–473  ·  view source on GitHub ↗
(c: Context, email: string, title: string, text: string)

Source from the content-addressed store, hash-verified

344
345// 发送邮件 ###############################################################################
346export async function mailSend(c: Context, email: string, title: string, text: string) {
347 try {
348 // 统一走 readConf:Confs → env → 默认值 三级回退,无需额外 || c.env.XXX。
349 const {readConf} = await import("./db/conf");
350 const mailKeys = (await readConf(c.env as any, "MAIL_KEYS")) ?? "";
351 const mailSend = (await readConf(c.env as any, "MAIL_SEND")) ?? "";
352 const siteTitle = (await readConf(c.env as any, "SITE_TITLE")) || "SSL Helper";
353 if (!mailKeys || !mailSend) {
354 console.warn("[mailSend] MAIL_KEYS / MAIL_SEND 未配置,已跳过发送", {
355 mailKeysConfigured: !!mailKeys,
356 mailSendConfigured: !!mailSend,
357 to: email,
358 });
359 return {"flags": 1, "nonce": "系统尚未配置邮件发送服务"};
360 }
361 // 自动从 MAIL_SEND 发件地址里解析域名(@ 后半段)
362 // 用于:日志诊断 / domain 未验证错误提示 / 校验发件地址格式
363 const sendMatch = String(mailSend).match(/^\s*([^\s<>@]+)@([^\s<>@]+?)\s*$/);
364 const sendLocal = sendMatch ? sendMatch[1] : "";
365 const sendDomain = sendMatch ? sendMatch[2].toLowerCase() : "";
366 if (!sendDomain) {
367 console.error("[mailSend] MAIL_SEND 发件地址格式非法", {mailSend});
368 return {"flags": 1, "nonce": "系统配置的发件邮箱格式非法(MAIL_SEND)"};
369 }
370 const fromAddr = `${siteTitle}<${sendLocal}@${sendDomain}>`;
371 // 记录发送开始时间、发件域、Resend 密钥诊断信息
372 const sendStartAt = Date.now();
373 console.log("[mailSend] sending", {
374 from: fromAddr,
375 fromDomain: sendDomain,
376 to: email,
377 subject: title,
378 // 不直接打印密钥,只保留前缀用于确认是否符合 re_xxx 的 Resend Key 格式
379 mailKeyPrefix: String(mailKeys).slice(0, 4),
380 mailKeyLength: String(mailKeys).length,
381 siteTitle,
382 });
383 const resend = new Resend(mailKeys);
384 const sendResult = await resend.emails.send({
385 from: fromAddr,
386 to: [email],
387 subject: title,
388 html: text,
389 });
390 const sendElapsedMs = Date.now() - sendStartAt;
391 const {data, error} = sendResult;
392 // 无论成败都打印一份 Resend 完整响应摘要
393 let resultJson = "";
394 try { resultJson = JSON.stringify(sendResult); } catch { /* ignore */ }
395 console.log("[mailSend] Resend response", {
396 elapsedMs: sendElapsedMs,
397 from: fromAddr,
398 fromDomain: sendDomain,
399 to: email,
400 hasData: !!data,
401 hasError: !!error,
402 messageId: (data as any)?.id,
403 errorName: (error as any)?.name,

Callers 1

codeSendFunction · 0.85

Calls 1

readConfFunction · 0.85

Tested by

no test coverage detected