Reads each PDF that arrives by email and files it into the right Box folder by what it says, or leaves it for you when unsure.

UsesBoxGmailOpenAI

File emailed PDFs into the right Box folder

Mail gets filed by who sent it, so the contract from your accountant ends up in the accounts folder. This reads each PDF that arrives by email for what kind of document it is, what it concerns, its reference, its date and any total, and an AI picks one of the Box folders you have described from those answers alone. The sender and subject are never shown to it, so an invoice is filed as an invoice whoever sent it. Each copy keeps its name plus a reference to its email, so a second Invoice.pdf never clashes with the first.

When the answer is not exactly one of your folders, or nothing could be read, nothing is copied and the email is labeled 'Needs sorting', a short queue you work through by hand. So is an older email Gmail cannot open because the same sender's newer one is still in the inbox. The run's last message lists each one. If reading, choosing or copying fails, the email is labeled the same way and the run ends as failed, saying what went wrong.

You need Gmail, Box and OpenAI connected, and up to three folders described in advance: a name, its Box folder and a line saying what belongs there. The Gmail search starts as any email from the past week with a PDF attached.

GmailGmailWhen an email arrives.When an email arrives.
Set documentSearch to “has:attachment filename:pdf newer_than:7d”.Set documentSearch to “has:attachment filename:pdf newer_than:7d”.
Set categoryOneName to blank.Set categoryOneName to blank.
Set categoryOneFolder to blank.Set categoryOneFolder to blank.
Set categoryOneAbout to blank.Set categoryOneAbout to blank.
Set categoryTwoName to blank.Set categoryTwoName to blank.
Set categoryTwoFolder to blank.Set categoryTwoFolder to blank.
Set categoryTwoAbout to blank.Set categoryTwoAbout to blank.
Set categoryThreeName to blank.Set categoryThreeName to blank.
Set categoryThreeFolder to blank.Set categoryThreeFolder to blank.
Set categoryThreeAbout to blank.Set categoryThreeAbout to blank.
Set processedLabel to “Document filing”.Set processedLabel to “Document filing”.
Set needsSortingLabel to “Needs sorting”.Set needsSortingLabel to “Needs sorting”.
Set maxDocumentsPerRun to 5.Set maxDocumentsPerRun to 5.
Set documentCap to maxDocumentsPerRun multiplied by 1.Set documentCap to maxDocumentsPerRun multiplied by 1.
If documentCap is greater than 25If documentCap is greater than 25
Yes
Stop and report a problem: The 'maxDocumentsPerRun' step needs a whole number between 1 and 25 - it is how many documents one run is allowed to handle.Stop and report a problem: The 'maxDocumentsPerRun' step needs a whole number between 1 and 25 - it is how many documents one run is allowed to handle.
No
No steps
If documentCap is less than 1If documentCap is less than 1
Yes
Stop and report a problem: The 'maxDocumentsPerRun' step needs a whole number between 1 and 25 - it is how many documents one run is allowed to handle.Stop and report a problem: The 'maxDocumentsPerRun' step needs a whole number between 1 and 25 - it is how many documents one run is allowed to handle.
No
No steps
Set destinations to an empty list.Set destinations to an empty list.
If categoryOneName isn't empty and categoryOneFolder isn't emptyIf categoryOneName isn't empty and categoryOneFolder isn't empty
Yes
Add a record of name, folder, and about to the end of destinations and save it as destinations.Add a record of name, folder, and about to the end of destinations and save it as destinations.
No
No steps
If categoryTwoName isn't empty and categoryTwoFolder isn't emptyIf categoryTwoName isn't empty and categoryTwoFolder isn't empty
Yes
Add a record of name, folder, and about to the end of destinations and save it as destinations.Add a record of name, folder, and about to the end of destinations and save it as destinations.
No
No steps
If categoryThreeName isn't empty and categoryThreeFolder isn't emptyIf categoryThreeName isn't empty and categoryThreeFolder isn't empty
Yes
Add a record of name, folder, and about to the end of destinations and save it as destinations.Add a record of name, folder, and about to the end of destinations and save it as destinations.
No
No steps
Set filingDestinations to an empty list.Set filingDestinations to an empty list.
For each destination in destinations:For each destination in destinations:
Trim destination.folder and save it as folderValue.Trim destination.folder and save it as folderValue.
If folderValue contains “/folder/” (ignoring case)If folderValue contains “/folder/” (ignoring case)
Yes
Trim the first item of the last item of folderValue split by “/folder/” split by “/” and save it as folderValue.Trim the first item of the last item of folderValue split by “/folder/” split by “/” and save it as folderValue.
No
No steps
If folderValue is empty or folderValue multiplied by 1 is not folderValueIf folderValue is empty or folderValue multiplied by 1 is not folderValue
Yes
Show in the output: Ignoring the '@destination.name' category: '@destination.folder' is not a Box folder. Paste the folder's address in Box, or just the number after '/folder/'.Show in the output: Ignoring the '@destination.name' category: '@destination.folder' is not a Box folder. Paste the folder's address in Box, or just the number after '/folder/'.
No
Add a record of name, folder, and about to the end of filingDestinations and save it as filingDestinations.Add a record of name, folder, and about to the end of filingDestinations and save it as filingDestinations.
If filingDestinations is emptyIf filingDestinations is empty
Yes
Stop and report a problem: Fill in at least one category - a name in 'categoryOneName' and the Box folder it files into in 'categoryOneFolder' - then run it again.Stop and report a problem: Fill in at least one category - a name in 'categoryOneName' and the Box folder it files into in 'categoryOneFolder' - then run it again.
No
No steps
Set choicesText to blank.Set choicesText to blank.
For each destination in filingDestinations:For each destination in filingDestinations:
Set choiceLine to “- destination.name”.Set choiceLine to “- destination.name”.
Trim destination.about and save it as choiceAbout.Trim destination.about and save it as choiceAbout.
If choiceAbout isn't emptyIf choiceAbout isn't empty
Yes
Change choiceLine to “@choiceLine: choiceAbout”.Change choiceLine to “@choiceLine: choiceAbout”.
No
No steps
Change choicesText to choicesText@choiceLineChange choicesText to choicesText@choiceLine .
GmailGmailList emails matching '@documentSearch has:attachment -label:“@processedLabel” -label:“@needsSortingLabel”'. Save it as found.List emails matching '@documentSearch has:attachment -label:“@processedLabel” -label:“@needsSortingLabel”'. Save it as found.
Set handledCount to 0.Set handledCount to 0.
Set filedCount to 0.Set filedCount to 0.
Set messagesLeft to 0.Set messagesLeft to 0.
Set leftForYou to an empty list.Set leftForYou to an empty list.
Set problems to an empty list.Set problems to an empty list.
For each message in found.emails:For each message in found.emails:
If handledCount is greater than or equal to documentCapIf handledCount is greater than or equal to documentCap
Yes
Add 1 to messagesLeft.Add 1 to messagesLeft.
Skip to the next item.Skip to the next item.
No
No steps
Set messageDescription to “an email”.Set messageDescription to “an email”.
If a step below fails, save the error as messageErrorIf a step below fails, save the error as messageError
Try
Set messageId to blank.Set messageId to blank.
If a step below fails, save the error as idErrorIf a step below fails, save the error as idError
Try
Trim message.id and save it as messageId.Trim message.id and save it as messageId.
On error
No steps
If messageId is emptyIf messageId is empty
Yes
Show in the output: “Skipped a message Gmail returned without an id.”.Show in the output: “Skipped a message Gmail returned without an id.”.
Skip to the next item.Skip to the next item.
No
No steps
Set rawFrom to blank.Set rawFrom to blank.
If a step below fails, save the error as fromErrorIf a step below fails, save the error as fromError
Try
Trim message.from and save it as rawFrom.Trim message.from and save it as rawFrom.
On error
No steps
Set senderAddress to rawFrom.Set senderAddress to rawFrom.
If rawFrom contains “<” (ignoring case)If rawFrom contains “<” (ignoring case)
Yes
Trim the first item of the last item of rawFrom split by “<” split by “>” and save it as senderAddress.Trim the first item of the last item of rawFrom split by “<” split by “>” and save it as senderAddress.
No
No steps
Set subject to “(no subject)”.Set subject to “(no subject)”.
If a step below fails, save the error as subjectErrorIf a step below fails, save the error as subjectError
Try
Trim message.subject and save it as subjectText.Trim message.subject and save it as subjectText.
If subjectText isn't emptyIf subjectText isn't empty
Yes
Change subject to subjectText.Change subject to subjectText.
No
No steps
On error
No steps
Build the text “'@subject' from @senderAddress” and save it as messageDescription.Build the text “'@subject' from @senderAddress” and save it as messageDescription.
If senderAddress doesn't contain “@” (ignoring case)If senderAddress doesn't contain “@” (ignoring case)
Yes
GmailGmailAdd label processedLabel to email messageId.Add label processedLabel to email messageId.
GmailGmailAdd label needsSortingLabel to email messageId.Add label needsSortingLabel to email messageId.
Add messageDescription: its sender could not be read to the end of leftForYou and save it as leftForYou.Add messageDescription: its sender could not be read to the end of leftForYou and save it as leftForYou.
Skip to the next item.Skip to the next item.
No
No steps
Set latestId to blank.Set latestId to blank.
GmailGmailFind email matching 'from:senderAddress'. Save it as latestFromSender.Find email matching 'from:senderAddress'. Save it as latestFromSender.
If a step below fails, save the error as latestErrorIf a step below fails, save the error as latestError
Try
Trim latestFromSender.email.id and save it as latestId.Trim latestFromSender.email.id and save it as latestId.
On error
No steps
If latestId is not messageIdIf latestId is not messageId
Yes
GmailGmailAdd label processedLabel to email messageId.Add label processedLabel to email messageId.
GmailGmailAdd label needsSortingLabel to email messageId.Add label needsSortingLabel to email messageId.
Add messageDescription: only a sender's newest email can be opened, and a newer one from this sender is in the inbox to the end of leftForYou and save it as leftForYou.Add messageDescription: only a sender's newest email can be opened, and a newer one from this sender is in the inbox to the end of leftForYou and save it as leftForYou.
Skip to the next item.Skip to the next item.
No
No steps
GmailGmailDownload the attachments on the latest email from senderAddress. Save it as downloaded.Download the attachments on the latest email from senderAddress. Save it as downloaded.
GmailGmailAdd label processedLabel to email messageId.Add label processedLabel to email messageId.
Set attachmentNumber to 0.Set attachmentNumber to 0.
Set namesOnMessage to an empty list.Set namesOnMessage to an empty list.
For each attachment in downloaded.documents:For each attachment in downloaded.documents:
Set fileName to blank. Set fileName to blank.
If a step below fails, save the error as nameError If a step below fails, save the error as nameError
Try
Trim attachment.filename and save it as fileName.Trim attachment.filename and save it as fileName.
On error
No steps
Set attachmentLabel to “(unnamed attachment)”. Set attachmentLabel to “(unnamed attachment)”.
If fileName isn't empty If fileName isn't empty
Yes
Change attachmentLabel to fileName.Change attachmentLabel to fileName.
No
No steps
If fileName in lowercase doesn't end with “.pdf” (ignoring case) If fileName in lowercase doesn't end with “.pdf” (ignoring case)
Yes
Show in the output: Skipped attachmentLabel: only PDF attachments are treated as documents.Show in the output: Skipped attachmentLabel: only PDF attachments are treated as documents.
Skip to the next item.Skip to the next item.
No
No steps
Set documentRef to blank. Set documentRef to blank.
If a step below fails, save the error as refError If a step below fails, save the error as refError
Try
Trim attachment.document_ref and save it as documentRef.Trim attachment.document_ref and save it as documentRef.
On error
No steps
If documentRef is empty If documentRef is empty
Yes
Show in the output: Could not open attachmentLabel; it was left alone.Show in the output: Could not open attachmentLabel; it was left alone.
Skip to the next item.Skip to the next item.
No
No steps
Set documentDetails to an empty list. Set documentDetails to an empty list.
Set failure to blank. Set failure to blank.
Set setAsideReason to blank. Set setAsideReason to blank.
If a step below fails, save the error as readError If a step below fails, save the error as readError
Try
Run an AI agent: Read the attached document and pull out each of these fields, in this order: - What kind… and save it as reading.Run an AI agent: Read the attached document and pull out each of these fields, in this order: - What kind… and save it as reading.
For each answer in reading.output.fields:For each answer in reading.output.fields:
Set answerName to blank. Set answerName to blank.
If a step below fails, save the error as answerNameError If a step below fails, save the error as answerNameError
Try
Trim answer.name and save it as answerName.Trim answer.name and save it as answerName.
On error
No steps
Set answerValue to blank. Set answerValue to blank.
If a step below fails, save the error as answerValueError If a step below fails, save the error as answerValueError
Try
Trim answer.value and save it as answerValue.Trim answer.value and save it as answerValue.
On error
If a step below fails, save the error as answerListErrorIf a step below fails, save the error as answerListError
Try
Join answer.value with “, ” and save it as answerValue.Join answer.value with “, ” and save it as answerValue.
On error
No steps
If answerValue isn't empty If answerValue isn't empty
Yes
Add answerName: answerValue truncated to 300 characters using “…” as the ellipsis to the end of documentDetails and save it as documentDetails.Add answerName: answerValue truncated to 300 characters using “…” as the ellipsis to the end of documentDetails and save it as documentDetails.
No
No steps
On error
Build the text “it could not be read (@readError.message)” and save it as failure.Build the text “it could not be read (@readError.message)” and save it as failure.
Set chosenName to blank. Set chosenName to blank.
Set chosenFolder to blank. Set chosenFolder to blank.
If documentDetails isn't empty If documentDetails isn't empty
Yes
Set choiceBrief to:Set choiceBrief to:Decide which folder a document belongs in, using only what the document itself says. The folders:
Set choiceRules to:Set choiceRules to:Reply with exactly one folder name from the list above and nothing else - no explanation, no punctuation. If the document does not clearly belong in exactly one of them, or you are not sure, reply with the single word UNCLEAR. Do not guess.
Set choicePrompt to choiceBrief@choicesTextSet choicePrompt to choiceBrief@choicesText What the document says: documentDetails joined by a new line@choiceRules.
Set verdict to blank.Set verdict to blank.
If a step below fails, save the error as choiceErrorIf a step below fails, save the error as choiceError
Try
OpenAIOpenAIAsk OpenAI: choicePrompt. Save it as chosen.Ask OpenAI: choicePrompt. Save it as chosen.
TextMake chosen.response_text lowercase and save it as verdict.Make chosen.response_text lowercase and save it as verdict.
On error
Build the text “the folder could not be chosen (@choiceError.message)” and save it as failure.Build the text “the folder could not be chosen (@choiceError.message)” and save it as failure.
Trim verdict with “”“ replaced by ”" and save it as verdict.Trim verdict with “”“ replaced by ”" and save it as verdict.
If verdict ends with “.” (ignoring case)If verdict ends with “.” (ignoring case)
Yes
Trim verdict truncated to the length of verdict minus 1 characters and save it as verdict.Trim verdict truncated to the length of verdict minus 1 characters and save it as verdict.
No
No steps
For each destination in filingDestinations:For each destination in filingDestinations:
If destination.name in lowercase is verdict If destination.name in lowercase is verdict
Yes
Change chosenName to destination.name.Change chosenName to destination.name.
Change chosenFolder to destination.folder.Change chosenFolder to destination.folder.
No
No steps
Else if @failure is empty
Change setAsideReason to “nothing could be read out of it”.Change setAsideReason to “nothing could be read out of it”.
Otherwise
No steps
If chosenFolder is empty and failure is empty If chosenFolder is empty and failure is empty
Yes
If setAsideReason is emptyIf setAsideReason is empty
Yes
Change setAsideReason to “its contents did not clearly match one of your folders”.Change setAsideReason to “its contents did not clearly match one of your folders”.
No
No steps
No
No steps
If chosenFolder isn't empty If chosenFolder isn't empty
Yes
Add 1 to attachmentNumber.Add 1 to attachmentNumber.
Replace the pattern “\.pdf$” with “” in attachmentLabel (ignoring case) and save it as fileStem.Replace the pattern “\.pdf$” with “” in attachmentLabel (ignoring case) and save it as fileStem.
Build the text “@fileStem (@messageId).pdf” and save it as boxName.Build the text “@fileStem (@messageId).pdf” and save it as boxName.
If namesOnMessage contains boxNameIf namesOnMessage contains boxName
Yes
Build the text “@fileStem (@messageId @attachmentNumber).pdf” and save it as boxName.Build the text “@fileStem (@messageId @attachmentNumber).pdf” and save it as boxName.
No
No steps
Add boxName to the end of namesOnMessage and save it as namesOnMessage.Add boxName to the end of namesOnMessage and save it as namesOnMessage.
Set storedLink to blank.Set storedLink to blank.
Set uploadProblem to blank.Set uploadProblem to blank.
If a step below fails, save the error as uploadErrorIf a step below fails, save the error as uploadError
Try
BoxBoxUpload chosenFolder to Box folder documentRef. Save it as stored.Upload chosenFolder to Box folder documentRef. Save it as stored.
Trim stored.file_link and save it as storedLink.Trim stored.file_link and save it as storedLink.
On error
Split uploadError.message by a new line and save it as uploadErrorLines.Split uploadError.message by a new line and save it as uploadErrorLines.
Get the first item of uploadErrorLines and save it as uploadProblem.Get the first item of uploadErrorLines and save it as uploadProblem.
If a step below fails, save the error as lookupErrorIf a step below fails, save the error as lookupError
Try
BoxBoxList items in Box folder chosenFolder. Save it as folderItems.List items in Box folder chosenFolder. Save it as folderItems.
Keep each item in folderItems.items where item.name is boxName and save it as sameName.Keep each item in folderItems.items where item.name is boxName and save it as sameName.
If sameName isn't emptyIf sameName isn't empty
Yes
Build the text “https://app.box.com/file/@sameName[0].id” and save it as storedLink.Build the text “https://app.box.com/file/@sameName[0].id” and save it as storedLink.
No
No steps
On error
No steps
If storedLink isn't emptyIf storedLink isn't empty
Yes
Add 1 to filedCount.Add 1 to filedCount.
Add 1 to handledCount.Add 1 to handledCount.
Show in the output: Filed attachmentLabel under chosenName as boxName.Show in the output: Filed attachmentLabel under chosenName as boxName.
Skip to the next item.Skip to the next item.
No
No steps
Build the text “it could not be copied into the '@chosenName' folder in Box (@uploadProblem)” and save it as failure.Build the text “it could not be copied into the '@chosenName' folder in Box (@uploadProblem)” and save it as failure.
No
No steps
Add 1 to handledCount. Add 1 to handledCount.
If a step below fails, save the error as sortingLabelError If a step below fails, save the error as sortingLabelError
Try
GmailGmailAdd label needsSortingLabel to email messageId.Add label needsSortingLabel to email messageId.
On error
Add attachmentLabel on messageDescription: the '@needsSortingLabel' label could not be added (sortingLabelError.message) to the end of problems and save it as problems.Add attachmentLabel on messageDescription: the '@needsSortingLabel' label could not be added (sortingLabelError.message) to the end of problems and save it as problems.
If failure isn't empty If failure isn't empty
Yes
Add attachmentLabel on messageDescription: failure to the end of problems and save it as problems.Add attachmentLabel on messageDescription: failure to the end of problems and save it as problems.
If filedCount is 0If filedCount is 0
Yes
Stop and report a problem: Stopped before filing anything: attachmentLabel on messageDescription was not filed because failure. It is labeled '@needsSortingLabel' for you. Check the app or setting this points to; the rest of your mail is left for the next run.Stop and report a problem: Stopped before filing anything: attachmentLabel on messageDescription was not filed because failure. It is labeled '@needsSortingLabel' for you. Check the app or setting this points to; the rest of your mail is left for the next run.
No
No steps
No
Add attachmentLabel on messageDescription: setAsideReason to the end of leftForYou and save it as leftForYou.Add attachmentLabel on messageDescription: setAsideReason to the end of leftForYou and save it as leftForYou.
Show in the output: Left for you: attachmentLabel. Show in the output: Left for you: attachmentLabel.
On error
Add messageDescription: messageError.message to the end of problems and save it as problems.Add messageDescription: messageError.message to the end of problems and save it as problems.
If a step below fails, save the error as setAsideErrorIf a step below fails, save the error as setAsideError
Try
GmailGmailAdd label processedLabel to email message.Add label processedLabel to email message.
GmailGmailAdd label needsSortingLabel to email message.Add label needsSortingLabel to email message.
On error
No steps
If filedCount is 0If filedCount is 0
Yes
Stop and report a problem: Stopped before filing anything: messageDescription could not be handled (messageError.message). The rest of your mail is left for the next run.Stop and report a problem: Stopped before filing anything: messageDescription could not be handled (messageError.message). The rest of your mail is left for the next run.
No
No steps
If handledCount is 0 and leftForYou is empty and problems is emptyIf handledCount is 0 and leftForYou is empty and problems is empty
Yes
Finish the run: No new documents arrived to sort.Finish the run: No new documents arrived to sort.
No
No steps
Set reportLines to an empty list.Set reportLines to an empty list.
Add Filed filedCount document(s) into Box. to the end of reportLines and save it as reportLines.Add Filed filedCount document(s) into Box. to the end of reportLines and save it as reportLines.
If messagesLeft is greater than 0If messagesLeft is greater than 0
Yes
Add messagesLeft more email(s) are waiting: this run reached its limit of maxDocumentsPerRun documents, and the next run carries on from there. to the end of reportLines and save it as reportLines.Add messagesLeft more email(s) are waiting: this run reached its limit of maxDocumentsPerRun documents, and the next run carries on from there. to the end of reportLines and save it as reportLines.
No
No steps
If leftForYou isn't emptyIf leftForYou isn't empty
Yes
Join leftForYou with "Join leftForYou with “ - ” and save it as leftForYouList.
Add Labeled '@needsSortingLabel' for you to file by hand:Add Labeled '@needsSortingLabel' for you to file by hand: - leftForYouList to the end of reportLines and save it as reportLines.
No
No steps
If problems isn't emptyIf problems isn't empty
Yes
Join problems with "Join problems with “ - ” and save it as problemList.
Add These were not filed because something went wrong, and are labeled '@needsSortingLabel' too:Add These were not filed because something went wrong, and are labeled '@needsSortingLabel' too: - problemList to the end of reportLines and save it as reportLines.
No
No steps
Join reportLines with a new line and save it as runReport.Join reportLines with a new line and save it as runReport.
If problems isn't emptyIf problems isn't empty
Yes
Stop and report a problem: runReport.Stop and report a problem: runReport.
No
No steps
Finish the run: runReport.Finish the run: runReport.