Reads every document that arrives by email, logs a short summary of each in a Google Doc and emails you what came in.

UsesGmailGoogle DocsOpenAI

Summarize the documents people email you

Whenever new mail arrives, the messages matching your search that carry attachments and have not been read yet are swept, each PDF attached is read, a short summary of it is written into a running Google Doc, and one email listing what came in is sent to you at the end.

Every document is asked the same questions: what kind it is, who issued it, who it is for, its date, reference, what it asks of you, any deadline and any total. The answers come from the document itself, and a question it does not answer is recorded as not stated rather than filled in. The summary is written from those answers and nothing else, and offers no advice. Each message is labeled once its documents are logged and the sweep skips anything already labeled, so nothing is summarized twice, and nothing is emailed when nothing arrived.

Before the first run you need a Gmail search that finds the mail carrying documents, a Google Doc to keep the summaries in, and your own address for the note.

GmailGmailWhen an email arrives.When an email arrives.
Set documentSearch to blank.Set documentSearch to blank.
Set logDocumentUrl to blank.Set logDocumentUrl to blank.
Set notifyEmail to blank.Set notifyEmail to blank.
Set processedLabel to “Document summaries”.Set processedLabel to “Document summaries”.
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 logDocumentId to blank.Set logDocumentId to blank.
If logDocumentUrl contains “/d/” (ignoring case)If logDocumentUrl contains “/d/” (ignoring case)
Yes
Trim the first item of the last item of logDocumentUrl split by “/d/” split by “/” and save it as logDocumentId.Trim the first item of the last item of logDocumentUrl split by “/d/” split by “/” and save it as logDocumentId.
No
No steps
If logDocumentId is emptyIf logDocumentId is empty
Yes
Stop and report a problem: The 'logDocumentUrl' step needs the address of a Google Doc - open the document and copy the address bar, which contains '/d/'.Stop and report a problem: The 'logDocumentUrl' step needs the address of a Google Doc - open the document and copy the address bar, which contains '/d/'.
No
No steps
GmailGmailList emails matching '@documentSearch has:attachment -label:“@processedLabel”'. Save it as found.List emails matching '@documentSearch has:attachment -label:“@processedLabel”'. Save it as found.
Set summarisedCount to 0.Set summarisedCount to 0.
Set digestLines to an empty list.Set digestLines to an empty list.
For each message in found.emails:For each message in found.emails:
If summarisedCount is greater than or equal to documentCapIf summarisedCount is greater than or equal to documentCap
Yes
Stop repeating.Stop repeating.
No
No steps
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
If senderAddress doesn't contain “@” (ignoring case)If senderAddress doesn't contain “@” (ignoring case)
Yes
Show in the output: Skipped a message whose sender could not be read: rawFrom.Show in the output: Skipped a message whose sender could not be read: rawFrom.
Skip to the next item.Skip to the next item.
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
Set latestId to blank.Set latestId to blank.
If a step below fails, save the error as latestErrorIf a step below fails, save the error as latestError
Try
GmailGmailFind email matching 'from:senderAddress'. Save it as latestFromSender.Find email matching 'from:senderAddress'. Save it as latestFromSender.
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
Show in the output: Left alone: '@subject' from senderAddress. Only a sender's newest message in the inbox can be opened, and a newer one has arrived - forward this message to yourself to have it read.Show in the output: Left alone: '@subject' from senderAddress. Only a sender's newest message in the inbox can be opened, and a newer one has arrived - forward this message to yourself to have it read.
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.
Set messageDocuments to 0.Set messageDocuments to 0.
For each attachment in downloaded.documents:For each attachment in downloaded.documents:
If summarisedCount is greater than or equal to documentCap If summarisedCount is greater than or equal to documentCap
Yes
Stop repeating.Stop repeating.
No
No steps
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 detailLines to an empty list. Set detailLines to an empty list.
Set documentKind to “not stated in the document”. Set documentKind to “not stated in the document”.
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 is empty If answerValue is empty
Yes
Change answerValue to “not stated in the document”.Change answerValue to “not stated in the document”.
Else if @answerName is "What kind of document this is"
Change documentKind to answerValue.Change documentKind to answerValue.
Otherwise
No steps
Add answerName: answerValue truncated to 300 characters using “…” as the ellipsis to the end of detailLines and save it as detailLines. Add answerName: answerValue truncated to 300 characters using “…” as the ellipsis to the end of detailLines and save it as detailLines.
On error
No steps
If detailLines is empty If detailLines is empty
Yes
Show in the output: Could not read attachmentLabel; nothing was written to the log and the mail was left alone.Show in the output: Could not read attachmentLabel; nothing was written to the log and the mail was left alone.
Skip to the next item.Skip to the next item.
No
No steps
Set summaryBrief to: Set summaryBrief to:Write a short plain summary of a document for the person who received it. These are the only details that were read out of the document. Use nothing but these. Do not add anything, do not work anything out, and do not guess. Where a detail says it is not stated in the document, either say so plainly or leave it out - never fill it in.
Set summaryRules to: Set summaryRules to:Write at most three sentences, describing only what the document says. Do not give advice, do not say what should be done about it, and do not comment on whether it is good, bad, risky or urgent.
Set summaryText to blank. Set summaryText to blank.
If a step below fails, save the error as summaryError If a step below fails, save the error as summaryError
Try
OpenAIOpenAIAsk OpenAI: '@summaryBrief plus detailLines joined by a new line plus summaryRules'. Save it as written.Ask OpenAI: '@summaryBrief plus detailLines joined by a new line plus summaryRules'. Save it as written.
Trim written.response_text and save it as summaryText.Trim written.response_text and save it as summaryText.
On error
No steps
If summaryText is empty If summaryText is empty
Yes
Join detailLines with a new line and save it as summaryText.Join detailLines with a new line and save it as summaryText.
Show in the output: No summary could be written for attachmentLabel; the log has the document's own answers instead.Show in the output: No summary could be written for attachmentLabel; the log has the document's own answers instead.
No
No steps
Set entryHeading to “@attachmentLabel - from senderAddress - the current date and time”. Set entryHeading to “@attachmentLabel - from senderAddress - the current date and time”.
Set entryText to Set entryText to entryHeading summaryText .
Google DocsGoogle Docs Add text to logDocumentId. Add text to logDocumentId.entryText
Add 1 to summarisedCount. Add 1 to summarisedCount.
Add 1 to messageDocuments. Add 1 to messageDocuments.
Add attachmentLabel from senderAddress - documentKind to the end of digestLines and save it as digestLines. Add attachmentLabel from senderAddress - documentKind to the end of digestLines and save it as digestLines.
GmailGmailAdd label processedLabel to email messageId.Add label processedLabel to email messageId.
If messageDocuments is 0If messageDocuments is 0
Yes
Show in the output: Nothing to summarize on '@subject' - it carried no readable PDF.Show in the output: Nothing to summarize on '@subject' - it carried no readable PDF.
No
No steps
On error
Show in the output: Could not read one document email: messageError as text.Show in the output: Could not read one document email: messageError as text.
If summarisedCount is greater than 0If summarisedCount is greater than 0
Yes
Set digestBody to digestLines joined by a new lineSet digestBody to digestLines joined by a new line The summaries are in your log: https://docs.google.com/document/d/@logDocumentId/edit.
GmailGmailSend an email to notifyEmail.Send an email to notifyEmail. Subject: Documents read from your mail: summarisedCountdigestBody
No
Show in the output: “No new documents arrived to summarize.”.Show in the output: “No new documents arrived to summarize.”.