Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | 9x 9x 9x 17x 15x 11x 7x 220x 7x 7x 1x 6x 9x | /**
* Transactional email sending through Amazon SES.
*
* Every stage sends for real: e2e test accounts live on a subdomain whose mail
* SES receives into an S3 bucket (see e2e-mailbox.ts), so tests read what was
* sent instead of it bouncing and hurting the account's sender reputation.
*/
const { SESv2Client, SendEmailCommand } = require('@aws-sdk/client-sesv2')
const sesClient = new SESv2Client({})
// Languages the UI is translated into (flows-ui useLanguage.preferredUILanguage);
// emails follow the same choice so a user reads them in their interface language.
const EMAIL_LANGUAGES = ['en', 'fr', 'es']
/**
* Picks the email language the same way the UI picks its own: the first of the
* user's language preferences that has a translation, English otherwise.
* @param {unknown} languagePreferences - The user's ordered language codes
* @returns {'en'|'fr'|'es'}
*/
function pickEmailLanguage(languagePreferences) {
if (Array.isArray(languagePreferences)) {
const match = languagePreferences.find((lang) => EMAIL_LANGUAGES.includes(lang))
if (match) return match
}
return 'en'
}
/**
* Escapes user-supplied text before it is interpolated into an HTML email body.
* @param {unknown} value
* @returns {string}
*/
function escapeHtml(value) {
return String(value)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
}
/**
* Sends one email. Throws on failure: callers that must not fail because of
* an email (it is a side notification, not the operation itself) catch it.
* @param {{ to: string, subject: string, html: string, text: string }} email
*/
async function sendEmail({ to, subject, html, text }) {
const from = process.env.EMAIL_FROM
if (!from) {
throw new Error('EMAIL_FROM is not configured')
}
await sesClient.send(
new SendEmailCommand({
FromEmailAddress: from,
Destination: { ToAddresses: [to] },
...(process.env.EMAIL_REPLY_TO ? { ReplyToAddresses: [process.env.EMAIL_REPLY_TO] } : {}),
Content: {
Simple: {
Subject: { Data: subject, Charset: 'UTF-8' },
Body: {
Html: { Data: html, Charset: 'UTF-8' },
Text: { Data: text, Charset: 'UTF-8' }
}
}
}
})
)
}
module.exports = {
EMAIL_LANGUAGES,
pickEmailLanguage,
escapeHtml,
sendEmail
}
|