diff --git a/keyserver/src/emails/sendmail.js b/keyserver/src/emails/sendmail.js index fc29f42ba..9e44fb757 100644 --- a/keyserver/src/emails/sendmail.js +++ b/keyserver/src/emails/sendmail.js @@ -1,19 +1,55 @@ // @flow +import invariant from 'invariant'; import nodemailer from 'nodemailer'; +import { isDev } from 'lib/utils/dev-utils'; + +import { importJSON } from '../utils/import-json.js'; + type MailInfo = { +from: string, +to: string, +subject: string, +html: string, ... }; type Transport = { +sendMail: (info: MailInfo) => Promise, ... }; -const sendmail: Transport = nodemailer.createTransport({ sendmail: true }); +type PostmarkConfig = { + +apiToken: string, +}; + +let cachedTransport: ?Transport; +async function getSendmail(): Promise { + if (cachedTransport) { + return cachedTransport; + } + const postmark: ?PostmarkConfig = await importJSON({ + folder: 'facts', + name: 'postmark', + }); + + if (isDev && !postmark) { + cachedTransport = nodemailer.createTransport({ sendmail: true }); + return cachedTransport; + } + + invariant(postmark, 'Postmark config missing'); + cachedTransport = nodemailer.createTransport({ + host: 'smtp.postmarkapp.com', + port: 587, + secure: false, + auth: { + user: postmark.apiToken, + pass: postmark.apiToken, + }, + requireTLS: true, + }); + return cachedTransport; +} -export default sendmail; +export default getSendmail; diff --git a/keyserver/src/emails/subscribe-email-updates.js b/keyserver/src/emails/subscribe-email-updates.js index 25d543422..d1b317eae 100644 --- a/keyserver/src/emails/subscribe-email-updates.js +++ b/keyserver/src/emails/subscribe-email-updates.js @@ -1,33 +1,34 @@ // @flow import * as React from 'react'; import { Item, Span, renderEmail } from 'react-html-email'; import ashoat from 'lib/facts/ashoat'; import type { EmailSubscriptionRequest } from 'lib/types/account-types'; -import sendmail from './sendmail'; +import getSendmail from './sendmail'; import Template from './template.react'; async function sendEmailSubscriptionRequestToAshoat( request: EmailSubscriptionRequest, ): Promise { const title = 'Somebody wants to learn more about Comm!'; const email = ( ); const html = renderEmail(email); + const sendmail = await getSendmail(); await sendmail.sendMail({ - from: 'no-reply@squadcal.org', + from: 'no-reply@comm.app', to: ashoat.landing_email, subject: title, html, }); } export { sendEmailSubscriptionRequestToAshoat };