diff --git a/functions/src/emails.ts b/functions/src/emails.ts index 60cfab3b..d7318caa 100644 --- a/functions/src/emails.ts +++ b/functions/src/emails.ts @@ -1,8 +1,17 @@ -import { sendEmail } from './send-email' import { Contract } from '../../common/contract' import { User } from '../../common/user' +import { sendTemplateEmail } from './send-email' import { getUser } from './utils' +type market_resolved_template = { + name: string + creatorName: string + question: string + outcome: string + payout: string + url: string +} + export const sendMarketResolutionEmail = async ( userId: string, payout: number, @@ -13,22 +22,24 @@ export const sendMarketResolutionEmail = async ( const user = await getUser(userId) if (!user) return - const subject = `Resolved ${toDisplayResolution[resolution]}: ${contract.question}` + const outcome = toDisplayResolution[resolution] - const body = `Dear ${user.name}, + const subject = `Resolved ${outcome}: ${contract.question}` -A market you bet in has been resolved! + const templateData: market_resolved_template = { + name: user.name, + creatorName: creator.name, + question: contract.question, + outcome, + payout: `${Math.round(payout)}`, + url: `https://manifold.markets/${creator.username}/${contract.slug}`, + } -Creator: ${contract.creatorName} -Question: ${contract.question} -Resolution: ${toDisplayResolution[resolution]} + // Modify template here: + // https://app.mailgun.com/app/sending/domains/mg.manifold.markets/templates/edit/market-resolved/initial + // Mailgun username: james@mantic.markets -Your payout is M$ ${Math.round(payout)} - -View the market here: -https://manifold.markets/${creator.username}/${contract.slug} -` - await sendEmail(user.email, subject, body) + await sendTemplateEmail(user.email, subject, 'market-resolved', templateData) } const toDisplayResolution = { YES: 'YES', NO: 'NO', CANCEL: 'N/A', MKT: 'MKT' } diff --git a/functions/src/send-email.ts b/functions/src/send-email.ts index fd995e59..1afc1526 100644 --- a/functions/src/send-email.ts +++ b/functions/src/send-email.ts @@ -4,7 +4,7 @@ import * as functions from 'firebase-functions' const DOMAIN = 'mg.manifold.markets' const mg = mailgun({ apiKey: functions.config().mailgun.key, domain: DOMAIN }) -export const sendEmail = (to: string, subject: string, text: string) => { +export const sendTextEmail = (to: string, subject: string, text: string) => { const data = { from: 'Manifold Markets ', to, @@ -12,7 +12,27 @@ export const sendEmail = (to: string, subject: string, text: string) => { text, } - return mg.messages().send(data, (error, body) => { - console.log('Sent email', error, body) + return mg.messages().send(data, (error) => { + if (error) console.log('Error sending email', error) + else console.log('Sent text email', to, subject) + }) +} + +export const sendTemplateEmail = ( + to: string, + subject: string, + templateId: string, + templateData: Record +) => { + const data = { + from: 'Manifold Markets ', + to, + subject, + template: templateId, + 'h:X-Mailgun-Variables': JSON.stringify(templateData), + } + return mg.messages().send(data, (error) => { + if (error) console.log('Error sending email', error) + else console.log('Sent template email', templateId, to, subject) }) }