How to Automatically Forward Emails in Gmail Including Attachments to a Robot Email Address Online?

This topic was automatically generated from Slack. You can find the original thread here.

Hi folks, I’m running in a pretty basic issue when trying to automatically forward emails in gmail including their attachments.
I’ve setup filters in gmail to automatically label incoming invoices, but unfortunately I cannot handle the forwarding in gmail since the recipient is a robot email I cannot confirm it as an authorized forwarding email address.

So I’m trying the following (since there’s no email forwarding action available)

  1. Trigger: when new labelled email (label=“Incoming-Invoice”)
  2. JS: Find all attachments & return those files in a single array of objects [{filename:file},{filename2:file2},…]
  3. Send Email to my accountant’s robot email with attachment = array of files from step 2.
  4. (Optional) If worklfow succeeded Add label to email from step 1 “sent-to-accounting”
    Where I’m stuck → At step 2 I believe I need my gmail user id and I’m not sure how to get it, I tried
1.
export default defineComponent({
  async run({ steps, $ }) {
    const emailId = steps.trigger.event.messageId; 
    const message = await $.google.gmail.users.messages.get({
      userId: "me",
      id: emailId,
    });

    const parts = message.payload.parts || [];
    const foundAttachments = parts
      .filter(part => part.filename && part.body && part.body.attachmentId)
      .map(part => ({
        filename: part.filename,
        attachmentId: part.body.attachmentId,
      }));

    return { foundAttachments };
  },
});

2. const message = await $.service('gmail').getMessage({ id: emailId, userId: 'me' });

Alternatively I could also break it up by attachment do this:

  1. Trigger: when new attachment received + condition label = “Incoming-Invoice”)
  2. Download attachment
  3. Send Email to my accountant’s robot email with single invoice file
  4. (Optional) If worklfow succeeded Add label to email from step 1 “sent-to-accounting”
    This approach is maybe simpler, but harder to monitor? What if a single email with 3 invoices triggers the workflow 3 times but it doesnt go through for one single file, the email will either be marked as treated or not treated and I have no idea which files will be missing?

I read through the other thread you were asking Pi, looks like you were getting an error related to missing scopes yeah? Our Gmail app doesn’t have restricted scopes yet (cc ) but I believe we will be able to add those soon. The alternative is to use our Gmail Developer App

But I’m curious where is $.google.gmail and $.service('gmail') coming from? Are you importing some google sdks as $?

Feel free to look and adapt our registry code in your workflow steps:
pipedream/components/gmail/gmail.app.mjs at master · PipedreamHQ/pipedream · GitHub
pipedream/components/gmail_custom_oauth/gmail_custom_oauth.app.mjs at master · PipedreamHQ/pipedream · GitHub