2019-05-23 14:46:10 +00:00
|
|
|
import * as nodemailer from 'nodemailer';
|
|
|
|
import { fetchMeta } from '../misc/fetch-meta';
|
|
|
|
import Logger from './logger';
|
2019-09-01 19:42:52 +00:00
|
|
|
import config from '../config';
|
2019-05-23 14:46:10 +00:00
|
|
|
|
|
|
|
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,
|
2019-09-01 19:42:52 +00:00
|
|
|
proxy: config.proxySmtp,
|
2019-05-23 14:46:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|