From dda7b0c80e1785e1979b088a32d7b47a763bb6a3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 23 May 2019 23:46:10 +0900 Subject: [PATCH] Refactor --- src/server/api/endpoints/i/update-email.ts | 33 ++------------------ src/services/send-email.ts | 36 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 31 deletions(-) create mode 100644 src/services/send-email.ts diff --git a/src/server/api/endpoints/i/update-email.ts b/src/server/api/endpoints/i/update-email.ts index d4b9721d82..56284499d3 100644 --- a/src/server/api/endpoints/i/update-email.ts +++ b/src/server/api/endpoints/i/update-email.ts @@ -1,15 +1,13 @@ import $ from 'cafy'; import { publishMainStream } from '../../../../services/stream'; import define from '../../define'; -import * as nodemailer from 'nodemailer'; -import { fetchMeta } from '../../../../misc/fetch-meta'; import rndstr from 'rndstr'; import config from '../../../../config'; import * as ms from 'ms'; import * as bcrypt from 'bcryptjs'; -import { apiLogger } from '../../logger'; import { Users, UserProfiles } from '../../../../models'; import { ensure } from '../../../../prelude/ensure'; +import { sendEmail } from '../../../../services/send-email'; export const meta = { requireCredential: true, @@ -63,36 +61,9 @@ export default define(meta, async (ps, user) => { emailVerifyCode: code }); - const meta = await fetchMeta(true); - - const enableAuth = meta.smtpUser != null && meta.smtpUser !== ''; - - const transporter = nodemailer.createTransport({ - host: meta.smtpHost, - port: meta.smtpPort, - secure: meta.smtpSecure, - ignoreTLS: !enableAuth, - auth: enableAuth ? { - user: meta.smtpUser, - pass: meta.smtpPass - } : undefined - } as any); - const link = `${config.url}/verify-email/${code}`; - transporter.sendMail({ - from: meta.email!, - to: ps.email, - subject: meta.name || 'Misskey', - text: `To verify email, please click this link: ${link}` - }, (error, info) => { - if (error) { - apiLogger.error(error); - return; - } - - apiLogger.info('Message sent: %s', info.messageId); - }); + sendEmail(ps.email, 'Email verification', `To verify email, please click this link: ${link}`); } return iObj; diff --git a/src/services/send-email.ts b/src/services/send-email.ts new file mode 100644 index 0000000000..8818c5fcde --- /dev/null +++ b/src/services/send-email.ts @@ -0,0 +1,36 @@ +import * as nodemailer from 'nodemailer'; +import { fetchMeta } from '../misc/fetch-meta'; +import Logger from './logger'; + +export const logger = new Logger('email'); + +export async function sendEmail(to: string, subject: string, text: string) { + const meta = await fetchMeta(true); + + const enableAuth = meta.smtpUser != null && meta.smtpUser !== ''; + + const transporter = nodemailer.createTransport({ + host: meta.smtpHost, + port: meta.smtpPort, + secure: meta.smtpSecure, + ignoreTLS: !enableAuth, + auth: enableAuth ? { + user: meta.smtpUser, + pass: meta.smtpPass + } : undefined + } as any); + + try { + const info = await transporter.sendMail({ + from: meta.email!, + to: to, + subject: subject || 'Misskey', + text: text + }); + + logger.info('Message sent: %s', info.messageId); + } catch (e) { + logger.error(e); + throw e; + } +}