Skip to content

Instantly share code, notes, and snippets.

@EmanuelCampos
Created September 29, 2021 10:30
Show Gist options
  • Save EmanuelCampos/70df22c96351feabcc384468c8782818 to your computer and use it in GitHub Desktop.
Save EmanuelCampos/70df22c96351feabcc384468c8782818 to your computer and use it in GitHub Desktop.
Mail Provider
import nodemailer, { Transporter } from 'nodemailer'
import fs from 'fs';
import handlebars from 'handlebars';
import { SES } from 'aws-sdk'
import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses';
import { IMailParams, IMailProvider } from '../protocols/IMailProvider';
import { config } from '@config/config';
export class SESMailProvider implements IMailProvider {
private client: Transporter;
constructor() {
this.client = nodemailer.createTransport({
SES: new SES({
apiVersion: "2010-12-01",
region: config.AWS_REGION,
})
})
}
async sendMail({ to, subject, variables, path }: IMailParams): Promise<void> {
const templateFileContent = fs.readFileSync(path).toString('utf-8');
const templateParse = handlebars.compile(templateFileContent);
const templateHtml = templateParse(variables);
await this.client.sendMail({
to,
from: "Emanuel Ferreira <contatoferreirads@gmail.com>",
subject,
html: templateHtml
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment