import * as nodemailer from 'nodemailer'; import { Inject, Injectable } from '@nestjs/common'; import { validate as validateEmail } from 'deep-email-validator'; import { MetaService } from '@/core/MetaService.js'; import { DI } from '@/di-symbols.js'; import type { Config } from '@/config.js'; import type Logger from '@/logger.js'; import type { UserProfilesRepository } from '@/models/index.js'; import { LoggerService } from '@/core/LoggerService.js'; import { bindThis } from '@/decorators.js'; @Injectable() export class EmailService { private logger: Logger; constructor( @Inject(DI.config) private config: Config, @Inject(DI.userProfilesRepository) private userProfilesRepository: UserProfilesRepository, private metaService: MetaService, private loggerService: LoggerService, ) { this.logger = this.loggerService.getLogger('email'); } @bindThis public async sendEmail(to: string, subject: string, html: string, text: string) { const meta = await this.metaService.fetch(true); const iconUrl = `${this.config.url}/static-assets/mi-white.png`; const emailSettingUrl = `${this.config.url}/settings/email`; const enableAuth = meta.smtpUser != null && meta.smtpUser !== ''; const transporter = nodemailer.createTransport({ host: meta.smtpHost, port: meta.smtpPort, secure: meta.smtpSecure, ignoreTLS: !enableAuth, proxy: this.config.proxySmtp, auth: enableAuth ? { user: meta.smtpUser, pass: meta.smtpPass, } : undefined, } as any); try { // TODO: htmlサニタイズ const info = await transporter.sendMail({ from: meta.email!, to: to, subject: subject, text: text, html: ` ${ subject }

${ subject }

${ html }
`, }); this.logger.info(`Message sent: ${info.messageId}`); } catch (err) { this.logger.error(err as Error); throw err; } } @bindThis public async validateEmailForAccount(emailAddress: string): Promise<{ available: boolean; reason: null | 'used' | 'format' | 'disposable' | 'mx' | 'smtp'; }> { const meta = await this.metaService.fetch(); const exist = await this.userProfilesRepository.countBy({ emailVerified: true, email: emailAddress, }); const validated = meta.enableActiveEmailValidation ? await validateEmail({ email: emailAddress, validateRegex: true, validateMx: true, validateTypo: false, // TLDを見ているみたいだけどclubとか弾かれるので validateDisposable: true, // 捨てアドかどうかチェック validateSMTP: false, // 日本だと25ポートが殆どのプロバイダーで塞がれていてタイムアウトになるので }) : { valid: true, reason: null }; const available = exist === 0 && validated.valid; return { available, reason: available ? null : exist !== 0 ? 'used' : validated.reason === 'regex' ? 'format' : validated.reason === 'disposable' ? 'disposable' : validated.reason === 'mx' ? 'mx' : validated.reason === 'smtp' ? 'smtp' : null, }; } }