Saturday, November 14, 2020

Inbound Email Handling with Google Cloud Functions

Summary

This post covers the intake of emails w/attachments into Google Cloud Functions (GCF).  The code here covers storing those attachments into a Cloud bucket.

There is no native SMTP trigger for GCF, so a 3rd party needs to be used to convert the email to an HTTP POST that can subsequently trigger a GCF.  In this case, I used CloudMailin.  They have a nice interface and are developer-friendly.  The GCF then needs to process the multipart form data and write the file attachments to Cloud Storage.

Part 1:  Inbound Email Handling with Google Cloud Functions

Architecture



Code Snippet - GCF Trigger

  1. exports.uploadRma = (req, res) => {
  2. if (req.method === 'POST') {
  3. if (req.query.key === process.env.API_KEY) {
  4. upload(req)
  5. .then(() => {
  6. res.status(200).send('');

Code Snippet - Upload function

The Busboy module is leveraged to do the heavy lifting of parsing the multi-part form.  Each file is written to a UUID "folder" in Cloud Storage.  Those writes are stored in a Promise array that is resolved when all the attachments of the form have been parsed.

  1. const busboy = new Busboy({headers: req.headers});
  2. const writes = [];
  3. const folder = uuidv4() + '/';
  4.  
  5. busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
  6. console.log(`File received: ${filename}`);
  7. writes.push(save(folder + filename, file));
  8. });
  9.  
  10. busboy.on('finish', async () => {
  11. console.log('Form parsed');
  12. await Promise.all(writes);
  13. resolve();
  14. });
  15.  
  16. busboy.end(req.rawBody);

Code Snippet - Save function

A read stream for the file attachment is piped to a write stream to Cloud Storage.
  1. function save(name, file) {
  2. return new Promise((resolve, reject) => {
  3. file.pipe(bucket.file(name).createWriteStream())
  4. .on('error', reject)
  5. .on('finish', resolve);
  6. });
  7. }

Results

Original Email


Cloud Logs


Cloud Storage




Source


Copyright ©1993-2024 Joey E Whelan, All rights reserved.