showing results for - "inbound email sendgrid cloud functions"
Sara
08 Jul 2016
1import * as functions from 'firebase-functions';
2import * as express from 'express';
3const formidable = require('formidable-serverless');
4
5export class InboundEmail {
6  constructor(from, subject, text, html, to) {}
7  doStrategy() {
8     //Store inbound email
9     //Send outbound email
10     //...
11  }
12}
13
14export class EmailPostWebHook {
15  private form = new formidable.IncomingForm();
16  private incomeEmail: IncomeEmail;
17
18  async run(request: express.Request, res: express.Response) {
19    try {
20      this.parse(request);
21      await this.incomeEmail.doStrategy();
22    } catch (e) {
23      console.log(e);
24    }
25    return res.sendStatus(200);
26  }
27
28  private parse(request: express.Request) {
29    this.form.parse(request, (errors: any, fields: any) => {
30      this.incomeEmail = new IncomeEmail(
31        fields.from
32        fields.subject,
33        fiels.text
34        fields.html,
35        fields.to
36      );
37    });
38  }
39}
40
41
42const app = express();
43
44const emailPostWebHook = new EmailPostWebHook();
45app.post('/', emailPostWebHook.run.bind(emailPostWebHook));
46
47export const InboundEmailHook = functions
48  .runWith({
49    timeoutSeconds: 30,
50    memory: '2GB',
51  })
52  .https.onRequest(app);
53