DEV Community

Cover image for Flag Your Apple Mails with OpenAI GPT
AIRabbit
AIRabbit

Posted on • Edited on

Flag Your Apple Mails with OpenAI GPT

As a developer, my inbox is my lifeline – and a constant source of headaches. I'm drowning in emails, and frankly, I'm tired of manually sorting through the chaos. I rely heavily on the Apple ecosystem, and while Apple's AI features are slowly improving, they haven't made a dent in my Mail situation yet. It's been over two years since generative AI took the world by storm, and it's frustrating that something as basic as intelligent email categorization is still missing in action.

So, I decided to leverage the power of GPT-4, particularly the cost-effective gpt-4o-mini model, to automatically categorize and flag my incoming emails right within Apple Mail. This tutorial will walk you through how I created an Apple Mail rule that triggers a custom script, utilizing GPT to determine the appropriate category (flag) for each email.

The result? A significantly more organized inbox, less time wasted on manual triage, and the peace of mind that comes with knowing my data stays under my control.

Do you want to sort your emails with a private model instead? Check out sorting email with Ollama

Let's dive in and reclaim our inboxes!

Prerequisites

  1. Get an OpenAI API key from OpenAI's platform ## Setup Instructions

1. Create the AppleScript File

Create a new file named flag-gpt.scpt in your Mail scripts directory:

~/Library/Application\ Scripts/com.apple.mail/flag-gpt.scpt
Enter fullscreen mode Exit fullscreen mode

2. Add the Script Content

Copy the following code into flag-gpt.scpt:

3. Customization Options

Configure the API Key and Model

Replace <YOUR API KEY HERE> in the script with your OpenAI API key. You can also change the model if desired, though gpt-4o-mini provides a good balance of performance and cost:

set jsonData to "{\"model\":\"gpt-4o-mini\",\"messages\":[{\"role\":\"user\",\"content\":\"" & escapedPrompt & "\"}],\"temperature\":0}"
Enter fullscreen mode Exit fullscreen mode

Configure Email Categories

Configure your email categories by modifying this line:

set colorCategories to { {"Blue", "<CATEGORY1>"}, {"Purple","<CATEGORY2>"}, {"Grey","<CATEGORY3>"}}
Enter fullscreen mode Exit fullscreen mode

Category Configuration

Make sure you rename the flags according to the categories you set up above. Here is how you can rename Flags names
https://support.apple.com/en-au/guide/mail/mlhlp1052/mac

4. Configure Mail Rules

  1. Open Apple Mail
  2. Go to Mail > Settings > Rules
  3. Click "Add Rule"
  4. Configure the rule:
    1. Description: "Flag-GPT"
    2. If: "any" of the following conditions are met
    3. Condition: "Every Message"
    4. Perform the following actions:
    5. Action: "Run AppleScript"
    6. Script: Select "flag-gpt"
  5. Click OK to save

Important: Testing and Application

  • Do NOT apply the rule to all existing emails
  • To test: Select a single email and click "Apply Rule"
  • The flagging will only be applied to the selected email
  • Check the log file to verify the response and categorization

If all goes well, a few seconds later you should be able to see the flag set for the selected email, if applicable. All new emails will be automatically flagged.

5. Logging

The script logs all analyses to:

~/Library/Logs/openai-mail-flags.log
Enter fullscreen mode Exit fullscreen mode

View the logs using:

tail -f ~/Library/Logs/openai-mail-flags.log
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

If emails aren't being flagged:

  1. Verify your OpenAI API key is correct and has sufficient credits
  2. Check the log file for API errors or rate limiting
  3. Verify script execution permissions
  4. Test the rule manually on individual emails
  5. Check your Python installation

Tip: Remember to restart Mail after making changes to the script or rules.

Please also note that if there is a syntax error in the script, Apple Mail will simply do nothing. To see potential errors, simply copy and paste the script into the "Script Editor" and run it.

Tips

  • Test the rule with individual sample emails first
  • Monitor the log file to understand AI categorization
  • Adjust the prompt if categorization needs tuning
  • Consider using different GPT models based on your needs
  • Keep an eye on your API usage and costs

Wrap-UP
And there you have it! We've walked through a practical solution to a common problem: email overload. By harnessing the power of GPT and a bit of AppleScript magic, we've built a system that intelligently categorizes and flags our emails directly within Apple Mail, all while keeping our data private and under our control.

Top comments (0)