-
Notifications
You must be signed in to change notification settings - Fork 3
/
email-service.ts
executable file
·37 lines (29 loc) · 1.09 KB
/
email-service.ts
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
export interface EmailService {
send(to: string, subject: string, body: string, base64Attachment?: string, attachmentName?: string): Promise<any>;
}
import * as nodemailer from 'nodemailer';
const emailAddress = process.env.EMAIL_ADDRESS;
const emailPassword = process.env.EMAIL_PASSWORD;
const smtpUrl = `smtps://${emailAddress}:${emailPassword}@smtp.gmail.com`;
const mailTransport = nodemailer.createTransport(smtpUrl);
export class NodeEmailService implements EmailService {
public send(to: string, subject: string, body: string, base64Attachment?: string, attachmentName?: string): Promise<any> {
const attachments = [];
if (base64Attachment) {
attachments.push({
filename: attachmentName || 'Report.pdf',
content: base64Attachment,
encoding: 'base64',
});
}
const mailOptions = {
from: emailAddress,
to: to,
subject: subject,
text: body,
attachments: attachments,
};
console.log(`Sending email\nTo: ${to}\nSubject ${subject}\nBody ${body}`);
return mailTransport.sendMail(mailOptions);
}
}