<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8">
</head>
<body><div style="font-family: sans-serif;"><div class="markdown" style="white-space: normal;">
<p dir="auto">I developed a MailMate Bundle which would take the from, subject and body of the e-mail, send it to OpenAI for analysis and returns a one-word TAG which is subsequently applied to the e-mail. The Bundle worked when executed manually, however, it would crash MailMate on startup if I created a rule on the Inbox folder which would execute the Bundle automatically - so I have not released the Bundle.</p>
<p dir="auto">I used identical logic and developed a python script which performs the same activity. While I would prefer to have the Bundle kick-off automatically based upon a new e-mail entering the Inbox folder(s); I simply have cron execute this every 5 minutes.</p>
<p dir="auto">So, comments on the code - enjoy for yourself, I'm just sharing since it was fun to do and it works enough for my personal satisfaction. It's running in my cron right now and working as intended; I'd like to get the Bundle working and not have MM crash on startup, but I think that gonna take a bit more time since I can't really trace that out (yet).</p>
<p dir="auto">One thing I did learn through all this fun - I was sending MORE OpenAI tokens with each e-mail than I realized - I had to change my model from gpt-4o (accepts 30k tokens) to gpt-4o-mini (accepts 200k) tokens; I also hit a rate limit since I was applying this logic to ALL my previous e-mails :) 1st time for that one...</p>
<p dir="auto">Oh, I spent about $0.03 with OpenAI credits doing all this too; so it's not a real financial deal breaker!</p>
<h2><strong>Code Below</strong></h2>
<pre style="margin-left: 15px; margin-right: 15px; padding: 5px; background-color: #F7F7F7; border-radius: 5px 5px 5px 5px; overflow-x: auto; max-width: 90vw;"><code style="margin: 0 0; border-radius: 3px; background-color: #F7F7F7; padding: 0px;">import imaplib
import email
from email.header import decode_header
from openai import OpenAI
client = OpenAI(api_key = "sk-...")
def decode_hdr(value):
if not value:
return ""
parts = decode_header(value)
decoded = ''
for part, encoding in parts:
if isinstance(part, bytes):
decoded += part.decode(encoding or 'utf-8', errors='ignore')
else:
decoded += part
return decoded
def extract_email_fields(raw_email_bytes):
msg = email.message_from_bytes(raw_email_bytes)
subject = decode_hdr(msg.get("Subject"))
from_ = decode_hdr(msg.get("From"))
to = decode_hdr(msg.get("To"))
date = decode_hdr(msg.get("Date"))
# Get plain text body
body = ""
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
content_dispo = str(part.get("Content-Disposition", "")).lower()
if content_type == "text/plain" and "attachment" not in content_dispo:
charset = part.get_content_charset() or "utf-8"
body = part.get_payload(decode=True).decode(charset, errors="ignore")
break
else:
if msg.get_content_type() == "text/plain":
charset = msg.get_content_charset() or "utf-8"
body = msg.get_payload(decode=True).decode(charset, errors="ignore")
return {
"subject": subject.strip(),
"from": from_.strip(),
"to": to.strip(),
"date": date.strip(),
"body": body.strip()
}
# Connect and login
imap = imaplib.IMAP4_SSL("imap.fastmail.com")
imap.login("user@fastmail.com", "abcd1234")
# Select the mailbox
imap.select("INBOX")
# Search for unseen emails
status, messages = imap.search(None, 'UNKEYWORD', 'Processed')
for num in messages[0].split():
result, data = imap.fetch(num, "(RFC822)")
raw_email = data[0][1]
fields = extract_email_fields(raw_email)
strings = [fields["from"], fields ["subject"], fields["body"] ]
query = " ".join(strings)
response = client.chat.completions.create(
model="gpt-4o",
messages=[ {"role": "system", "content": "You are an email classification system. Based on the email's subject and body, assign one of the following categories: Ads, Finance, Medical, News, Other, Radio, Shipping, Update. Return only the category name, nothing else."},
{"role": "user", "content": f"{query}" }
]
)
result = response.choices[0].message.content
result = result.strip()
imap.store(num, '+FLAGS', f'{result}')
imap.store(num, '+FLAGS', 'Processed')
imap.logout()
</code></pre>
<hr style="border: 0; height: 1px; background: #333; background-image: linear-gradient(to right, #ccc, #333, #ccc);">
<p dir="auto">--<br>
Jim Bates<br>
(804) 690-9143 (Cell/Signal)</p>
<p dir="auto">On 17 Jul 2025, at 11:05, Jim Bates via mailmate wrote:</p>
<blockquote style="margin: 0 0 5px; padding-left: 5px; border-left: 2px solid #777777; color: #777777;">
<p dir="auto"><strong>TLDR:</strong></p>
<p dir="auto">Does anyone have a machine learning, Sieve, MailMate rule system or any other systematic process which will [create tags | move mail] based on machine learning algorithms (or alternative logic) instead of hard coded word/syntax matches?</p>
<hr style="border: 0; height: 1px; background: #333; background-image: linear-gradient(to right, #ccc, #333, #ccc);">
<p dir="auto">I have grown weary sorting e-mail based upon e-mail addresses and a few, select searchable keywords.</p>
<p dir="auto">I was playing around with some machine learning (ML) analysis of my e-mail corpus and had various levels of failure :) No real success so far.</p>
<p dir="auto">I thought I'd send a note out to the MailMate team and see if anyone had developed their own solution.</p>
<p dir="auto">On a slightly different note, I have purchased and use Spam Sieve - it's integrated into MailMate and though I receive very little Spam anyway, it does a nice job of “learning” about my mail content. I have asked the Spam Sieve team if they are planning on developing a feature set that I've described - they say they're considering it in the product, but does not exist today...</p>
<p dir="auto">Jim Bates<br>
(804) 690-9143 (Cell/Signal)</p>
<hr style="border: 0; height: 1px; background: #333; background-image: linear-gradient(to right, #ccc, #333, #ccc);">
<p dir="auto">mailmate mailing list<br>
Unsubscribe: <a href="https://lists.freron.com/listinfo/mailmate" style="color: #777777;">https://lists.freron.com/listinfo/mailmate</a></p>
</blockquote>
</div>
</div>
</body>
</html>