-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
76 additions
and
42 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,60 @@ | ||
/* @flow */ | ||
import nodemailer from "nodemailer"; | ||
import sgTransport from "nodemailer-sendgrid-transport"; | ||
import crypto from 'crypto' | ||
import fetch from 'node-fetch' | ||
// Types | ||
import type { EmailRequestType } from "../../models/notification-request"; | ||
import type {EmailRequestType} from '../../models/notification-request' | ||
|
||
export default class EmailSendGridProvider { | ||
id: string; | ||
transporter: Object; | ||
id: string | ||
apiKey: string | ||
|
||
constructor(config: Object) { | ||
this.id = "email-sendgrid-provider"; | ||
const sgOptions = { | ||
auth: { | ||
api_key: config.apiKey | ||
} | ||
}; | ||
this.transporter = nodemailer.createTransport(sgTransport(sgOptions)); | ||
constructor (config: Object) { | ||
this.id = 'email-sendgrid-provider' | ||
this.apiKey = config.apiKey | ||
} | ||
|
||
async send(request: EmailRequestType): Promise<string> { | ||
const result = await this.transporter.sendMail(request); | ||
return result.messageId; | ||
async send (request: EmailRequestType): Promise<string> { | ||
const {id, userId, from, replyTo, subject, html, text, headers, to, cc, bcc, attachments} = request | ||
const generatedId = id || crypto.randomBytes(16).toString('hex') | ||
const response = await fetch('https://api.sendgrid.com/v3/mail/send', { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${this.apiKey}`, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
personalizations: [{ | ||
to: [{email: to}], | ||
...(cc && cc.length > 1 ? {cc: cc.map((email) => ({email}))} : null), | ||
...(bcc && bcc.length > 1 ? {bcc: bcc.map((email) => ({email}))} : null) | ||
}], | ||
from: {email: from}, | ||
...(replyTo ? {reply_to: {email: replyTo}} : null), | ||
subject: subject, | ||
content: [ | ||
...(text ? [{type: 'text/plain', value: text}] : []), | ||
...(html ? [{type: 'text/html', value: html}] : []) | ||
], | ||
headers, | ||
custom_args: {id: generatedId, userId}, | ||
...(attachments && attachments.length > 1 ? { | ||
attachments: attachments.map(({contentType, filename, content}) => | ||
({ | ||
type: contentType, | ||
filename, | ||
content: (typeof content === 'string' ? Buffer.from(content) : content).toString('base64') | ||
})) | ||
} : null) | ||
}) | ||
}) | ||
|
||
if (response.ok) { | ||
return generatedId | ||
} else { | ||
const responseBody = await response.json() | ||
const [firstError] = responseBody.errors | ||
const message = Object.keys(firstError).map((key) => `${key}: ${firstError[key]}`).join(', ') | ||
throw new Error(`${response.status} - ${message}`) | ||
} | ||
} | ||
} |
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