Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support custom SMTP sendfrom #2405

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions apps/core/src/modules/comment/comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,9 @@ export class CommentService implements OnModuleInit {
const location =
`${result.countryName || ''}${
result.regionName && result.regionName !== result.cityName
? `${result.regionName}`
? String(result.regionName)
: ''
}${result.cityName ? `${result.cityName}` : ''}` || undefined
}${result.cityName ? String(result.cityName) : ''}` || undefined

if (location) await this.commentModel.updateOne({ _id: id }, { location })
}
Expand Down Expand Up @@ -574,13 +574,13 @@ export class CommentService implements OnModuleInit {
type: CommentReplyMailType
}) {
const { seo, mailOptions } = await this.configsService.waitForConfigReady()
const { user } = mailOptions
const from = `"${seo.title || 'Mx Space'}" <${user}>`
const { from, user } = mailOptions
const sendfrom = `"${seo.title || 'Mx Space'}" <${from || user}>`

source.ip ??= ''
if (type === CommentReplyMailType.Guest) {
const options = {
from,
from: sendfrom,
subject: `[${seo.title || 'Mx Space'}] 主人给你了新的回复呐`,
to,
html: render(
Expand All @@ -598,7 +598,7 @@ export class CommentService implements OnModuleInit {
await this.mailService.send(options)
} else {
const options = {
from,
from: sendfrom,
subject: `[${seo.title || 'Mx Space'}] 有新回复了耶~`,
to,
html: render(
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/modules/configs/configs.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const generateDefaultConfig: () => IConfig = () => ({

user: '',
pass: '',
from: '',
options: {
host: '',
port: 465,
Expand Down
10 changes: 7 additions & 3 deletions apps/core/src/modules/configs/configs.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ class MailOption {
@IsInt()
@Transform(({ value: val }) => Number.parseInt(val))
@IsOptional()
@JSONSchemaNumberField('发件邮箱端口', halfFieldOption)
@JSONSchemaNumberField('SMTP 端口', halfFieldOption)
port: number
@IsUrl({ require_protocol: false })
@IsOptional()
@JSONSchemaHalfGirdPlainField('发件邮箱 host')
@JSONSchemaHalfGirdPlainField('SMTP 主机')
host: string
@IsBoolean()
@IsOptional()
Expand All @@ -102,11 +102,15 @@ export class MailOptionsDto {
@IsEmail()
@IsOptional()
@JSONSchemaHalfGirdPlainField('发件邮箱地址')
from: string
@IsString()
@IsOptional()
@JSONSchemaHalfGirdPlainField('SMTP 用户名')
user: string
@IsString()
@IsNotEmpty()
@IsOptional()
@JSONSchemaPasswordField('发件邮箱密码', halfFieldOption)
@JSONSchemaPasswordField('SMTP 密码', halfFieldOption)
@SecretField
pass: string

Expand Down
12 changes: 6 additions & 6 deletions apps/core/src/modules/link/link.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ export class LinkService {
template: LinkApplyEmailType
}) {
const { seo, mailOptions } = await this.configsService.waitForConfigReady()
const { user } = mailOptions
const from = `"${seo.title || 'Mx Space'}" <${user}>`
const { from, user } = mailOptions
const sendfrom = `"${seo.title || 'Mx Space'}" <${from || user}>`
await this.emailService.getInstance().sendMail({
from,
from: sendfrom,
to,
subject:
template === LinkApplyEmailType.ToMaster
Expand Down Expand Up @@ -278,10 +278,10 @@ export class LinkService {
return
}

const { user } = mailOptions
const from = `"${seo.title || 'Mx Space'}" <${user}>`
const { from, user } = mailOptions
const sendfrom = `"${seo.title || 'Mx Space'}" <${from || user}>`
await this.emailService.getInstance().sendMail({
from,
from: sendfrom,
to: doc.email,
subject: `嘿!~, 主人已处理你的友链申请!~`,
text: `申请结果:${LinkStateMap[state]}\n原因:${reason}`,
Expand Down
6 changes: 3 additions & 3 deletions apps/core/src/modules/subscribe/subscribe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ export class SubscribeService implements OnModuleInit, OnModuleDestroy {
unsubscribeLink: string,
) {
const { seo, mailOptions } = await this.configService.waitForConfigReady()
const { user } = mailOptions
const from = `"${seo.title || 'Mx Space'}" <${user}>`
const { from, user } = mailOptions
const sendfrom = `"${seo.title || 'Mx Space'}" <${from || user}>`
let finalTemplate = ''

const cacheKey = 'template'
Expand All @@ -275,7 +275,7 @@ export class SubscribeService implements OnModuleInit, OnModuleDestroy {
}

const options: Mail.Options = {
from,
from: sendfrom,
...{
subject: `[${seo.title || 'Mx Space'}] 发布了新内容~`,
to: email,
Expand Down
2 changes: 1 addition & 1 deletion apps/core/src/processors/helper/helper.email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class EmailService implements OnModuleInit, OnModuleDestroy {
const master = await this.userService.getMaster()
const mailOptions = await this.configsService.get('mailOptions')
return this.instance.sendMail({
from: `"Mx Space" <${mailOptions.user}>`,
from: `"Mx Space" <${mailOptions.from || mailOptions.user}>`,
to: master.mail,
subject: '测试邮件',
text: '这是一封测试邮件',
Expand Down