import AWS from "aws-sdk" ; import MailComposer from "nodemailer/lib/mail-composer" ; import config from "../config" ; import { HapiServer } from "../types" ; import { UploadService } from "./uploadService" ; export class EmailService { experimental uploadService : UploadService ; constructor ( server ) { const { uploadService } = server . services ( [ ] ) ; this . uploadService = uploadService ; } param emailAddress param subject param options param options async sendEmail ( emailAddress , subject , options = { } ) { const mailOptions = { from : config . fromEmailAddress , to : emailAddress , subject , text : options . message || "" , } ; if ( options . attachments ) { mailOptions . attachments = await this . uploadService . downloadDocuments ( options . attachments ) ; } const mailComposer = new MailComposer ( mailOptions ) ; const message = await mailComposer . compile ( ) . build ( ) ; return new AWS . SES ( { apiVersion : "2010-12-01" , region : "eu-west-1" } ) . sendRawEmail ( { RawMessage : { Data : message } } ) . promise ( ) ; } }