import config from "./config" ; import dotenv from "dotenv" ; import htmlToText from "html-to-text" ; dotenv . config ( ) ; import sendgridEmail from "@sendgrid/mail" ; import { log } from "./utils" ; const EMBARASSING_PATTERNS = [ /\{/ , /\}/ , /\[object/ ] ; const from = config . email . fromAddress ; export function _checkEmbarassing ( plainText ) { EMBARASSING_PATTERNS . forEach ( function ( embarassingPattern ) { const isEmbarassing = plainText . match ( new RegExp ( embarassingPattern ) ) ; if ( isEmbarassing ) { throw new Error ( `Refusing to send message: possible embarassing pattern in text: ${ plainText }` ) ; } } ) ; return ; } const convertHTMLToText = function ( string ) { return htmlToText . fromString ( string , { wordwrap : false , } ) ; } ; export async function sendEmail ( { recipient , subject , html , text , } ) { if ( html ) { if ( ! text ) { text = convertHTMLToText ( html ) ; } } _checkEmbarassing ( text ) ; const message = { to : recipient , from , subject , html , text , } ; if ( ! config . isProduction ) { log ( `Refusing to send email outside production` ) ; log ( `Would have sent email to '${ recipient }', subject '${ subject }', with body\n${ text }` ) ; return ; } try { sendgridEmail . setApiKey ( process . env . SENDGRID_API_KEY ) ; await sendgridEmail . send ( message ) ; } catch ( error ) { console . error ( error ) ; } }