@ -1,3 +1,4 @@
import log4js from "logger" ;
import { dbGlobal } from "drizzle-pkg/lib/db" ;
import { users } from "drizzle-pkg/lib/schema/auth" ;
import { eq } from "drizzle-orm" ;
@ -9,6 +10,8 @@ import {
sendCommentEmailTestMail ,
} from "#server/service/comment-email/test-mail" ;
const logger = log4js . getLogger ( "COMMENT_EMAIL_TEST" ) ;
export default defineWrappedResponseHandler ( async ( event ) = > {
const ip = getRequestIP ( event , { xForwardedFor : true } ) ? ? "unknown" ;
assertUnderRateLimit ( ` admin-config-comment-email-test: ${ ip } ` , 5 , 60 _000 ) ;
@ -21,19 +24,21 @@ export default defineWrappedResponseHandler(async (event) => {
. where ( eq ( users . id , admin . id ) )
. limit ( 1 ) ;
const commentEmailConfig = {
enabled : await event . context . config . getGlobal ( "commentEmailNotifyEnabled" ) ,
fromEmail : await event . context . config . getGlobal ( "commentMailFromEmail" ) ,
smtpHost : await event . context . config . getGlobal ( "commentSmtpHost" ) ,
smtpPort : await event . context . config . getGlobal ( "commentSmtpPort" ) ,
smtpSecure : await event . context . config . getGlobal ( "commentSmtpSecure" ) ,
smtpUser : await event . context . config . getGlobal ( "commentSmtpUser" ) ,
smtpPass : await event . context . config . getGlobal ( "commentSmtpPass" ) ,
} ;
try {
await sendCommentEmailTestMail ( {
toEmail : adminRow?.email ? ? "" ,
requestedBy : admin.username ,
config : {
enabled : await event . context . config . getGlobal ( "commentEmailNotifyEnabled" ) ,
fromEmail : await event . context . config . getGlobal ( "commentMailFromEmail" ) ,
smtpHost : await event . context . config . getGlobal ( "commentSmtpHost" ) ,
smtpPort : await event . context . config . getGlobal ( "commentSmtpPort" ) ,
smtpSecure : await event . context . config . getGlobal ( "commentSmtpSecure" ) ,
smtpUser : await event . context . config . getGlobal ( "commentSmtpUser" ) ,
smtpPass : await event . context . config . getGlobal ( "commentSmtpPass" ) ,
} ,
config : commentEmailConfig ,
} ) ;
return R . success ( {
message : "测试邮件发送成功,请检查管理员邮箱" ,
@ -45,6 +50,16 @@ export default defineWrappedResponseHandler(async (event) => {
statusMessage : error.message ,
} ) ;
}
logger . error (
"[send-test-mail-failed]" ,
` adminId= ${ admin . id } ` ,
` adminUsername= ${ admin . username } ` ,
` smtpHost= ${ commentEmailConfig . smtpHost } ` ,
` smtpPort= ${ commentEmailConfig . smtpPort } ` ,
` smtpSecure= ${ commentEmailConfig . smtpSecure } ` ,
error instanceof Error ? error.message : String ( error ) ,
error instanceof Error ? ( error . stack ? ? "" ) : "" ,
) ;
throw createError ( {
statusCode : 502 ,
statusMessage : "测试邮件发送失败,请检查 SMTP 配置或稍后重试" ,