簡體   English   中英

如何獲取有關 firebase 功能的傳入 sendgrid 電子郵件

[英]How to get incoming sendgrid email on firebase functions

我有一個 firebase 函數,我將它用作 sendgrid 的入站解析 webhook 的 webhook。 這意味着每當向我的域發送電子郵件時,它都會調用 webhook。 我知道正在調用 webhook,但我無法獲取 Sendgrid 發送的數據。 此鏈接指出所有信息(文本、發件人等)都應位於標題中。 但是,當我打印出req.headers我得到了這個:

{ host: 'us-central1-project-name.cloudfunctions.net',
  'user-agent': 'Sendlib/1.0 server.sendgrid.net',
  'transfer-encoding': 'chunked',
  'content-type': 'multipart/form-data; boundary=xYzZY',
  forwarded: 'for="ip";proto=https',
  'function-execution-id': 'id',
  'x-appengine-city': '?',
  'x-appengine-citylatlong': '0.000000,0.000000',
  'x-appengine-country': 'US',
  'x-appengine-default-version-hostname': ~~~~~~~~~~~~~.appspot.com',
  'x-appengine-https': 'on',
  'x-appengine-region': '?',
  'x-appengine-request-log-id': 'super-long-id',
  'x-appengine-user-ip': 'ip',
  'x-cloud-trace-context': 'id/number;o=1',
  'x-forwarded-for': 'ip',
  'x-forwarded-proto': 'https',
  'accept-encoding': 'gzip',
  connection: 'close' }'

(顯然我替換了所有 ID 和所有內容)

郵箱信息在哪里? 我已嘗試執行以下所有操作,但均未提供有關電子郵件的任何信息。

exports.reciever = functions.https.onRequest((req, res) => {
  try {
   console.log("Email recieved");
   console.log(req);
   console.log(req.headers);
   console.log(req.header.to);
   console.log(req.body);
   console.log(req.get('to'));
   console.log(req.body.to);
   console.log(req.rawBody);
   console.log(req.query);
   console.log(req.query.to);
   console.log(req.params);
   console.log(req.path);
   console.log(req.rawBody);
  } catch (e) {}
  finally {
      res.send("2xx");
  } 
})

所以,事實證明它實際上非常非常簡單。 添加

.toString()

req.bodyreq.rawBody對象的末尾。

該問題即將在無服務器環境(AWS Lambda、Firebase/Google Cloud Functions 等)和已處理請求的環境中啟用。 使用 MIME 解析電子郵件不是一個好主意。 因此,這里是我的解決方案:

import * as functions from 'firebase-functions';
import * as express from 'express';
const formidable = require('formidable-serverless');

export class InboundEmail {
  constructor(from, subject, text, html, to) {}
  doStrategy() {
     //Store inbound email
     //Send outbound email
     //...
  }
}

export class EmailPostWebHook {
  private form = new formidable.IncomingForm();
  private incomeEmail: IncomeEmail;

  async run(request: express.Request, res: express.Response) {
    try {
      this.parse(request);
      await this.incomeEmail.doStrategy();
    } catch (e) {
      console.log(e);
    }
    return res.sendStatus(200);
  }

  private parse(request: express.Request) {
    this.form.parse(request, (errors: any, fields: any) => {
      this.incomeEmail = new IncomeEmail(
        fields.from
        fields.subject,
        fiels.text
        fields.html,
        fields.to
      );
    });
  }
}


const app = express();

const emailPostWebHook = new EmailPostWebHook();
app.post('/', emailPostWebHook.run.bind(emailPostWebHook));

export const InboundEmailHook = functions
  .runWith({
    timeoutSeconds: 30,
    memory: '2GB',
  })
  .https.onRequest(app);

包.json

{
  "name": "temp",
  "main": "lib/index.js",
  "dependencies": {
    "express": "^4.17.1",
    "firebase": "^7.15.1",
    "firebase-admin": "^8.12.1",
    "firebase-functions": "^3.7.0",
    "formidable-serverless": "^1.0.3"
  },
  "devDependencies": {
    "@firebase/firestore-types": "^1.11.0",
    "@types/node": "^14.0.13",
    "@types/express": "^4.17.6",
    "tslint": "^6.1.2",
    "typescript": "^3.9.5"
  },
  "engines": {
    "node": "10"
  },
  "private": true
}

參考:

https://github.com/sendgrid/sendgrid-nodejs/blob/master/docs/examples/webhooks-docker/routes/inbound-parse.js

https://github.com/Amit-A/formidable-serverless/

https://www.npmjs.com/package/formidable

我終於設法通過在函數目錄中安裝 Busboy 然后使用此處提供的代碼使其工作: https ://stackoverflow.com/a/54026200/10372124

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM