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)
- Trigger: when new labelled email (label=“Incoming-Invoice”)
- JS: Find all attachments & return those files in a single array of objects [{filename:file},{filename2:file2},…]
- Send Email to my accountant’s robot email with attachment = array of files from step 2.
- (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:
- Trigger: when new attachment received + condition label = “Incoming-Invoice”)
- Download attachment
- Send Email to my accountant’s robot email with single invoice file
- (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?