Last active
April 29, 2024 02:17
-
-
Save m-arrieta-r/3f08fecffd3e621e6c902668dd26ec71 to your computer and use it in GitHub Desktop.
Email Sender with attachments using: - SES AWS SDK v3 (SendRawEmail) - Nodemailer (MailComposer)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Logger } from "@aws-lambda-powertools/logger"; | |
import { SESClient, SendRawEmailCommand } from "@aws-sdk/client-ses"; | |
import MailComposer from 'nodemailer/lib/mail-composer/index.js'; | |
type RawEmailCommandInput = { | |
from: string; | |
to: string; | |
subject: string; | |
text?: string; | |
html?: string; | |
attachments?: { | |
filename?: string; | |
content?: string; | |
path?: string; | |
}[] | |
} | |
export class EmailSender { | |
private client: SESClient; | |
constructor(private logger: Logger) { | |
this.client = new SESClient({}); | |
} | |
public async execute(xxx: any) { | |
const command = await this.createSendRawEmailCommand({ | |
from: 'xx@xx.xx', | |
to: 'xx@xx.xx', | |
subject: 'xx yy', | |
html: `xyz`, | |
attachments: [ | |
{ | |
filename: 'license.txt', | |
path: 'https://raw.github.com/andris9/Nodemailer/master/LICENSE' | |
}, | |
{ | |
filename: `xxx.xyz`, | |
content: xxx | |
}, | |
] | |
}); | |
const data = await this.client.send(command); | |
} | |
private async createSendRawEmailCommand(input: RawEmailCommandInput) { | |
// https://nodemailer.com/extras/mailcomposer/ | |
const rawMessageData = await new MailComposer({ | |
from: input.from, | |
sender: input.from, | |
to: input.to, | |
subject: input.subject, | |
text: input.text, | |
html: input.html, | |
attachments: input.attachments | |
}).compile().build(); | |
return new SendRawEmailCommand({ | |
Source: input.from, | |
Destinations: [ input.to ], | |
RawMessage: { Data: rawMessageData } | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment