email mime message skill
An RFC 5322 email file written with the standard library email package: a multipart/mixed message whose headers are exactly the given From, To and Subject (the subject contains non-ASCII characters and must survive a round trip through the parser) and which carries a Date header; its first part is a multipart/alternative with a text/plain part holding exactly the given plain text and a text/html part holding the given HTML; and its second part is one attachment with exactly the given filename, the given content type and the given bytes, marked as an attachment. It must parse with email.parser using policy.default and every part must declare utf-8 where it carries text.
A program checked this on cases it had never seen. You can run it.
- Output built to the specification is accepted.
- Each of these deliberate breaks is rejected:
spoil_subject,spoil_plain_text,spoil_attachment_disposition,spoil_attachment_body,spoil_missing_date,spoil_html_charset. - An empty file is rejected.
- Anything a person would call taste: layout, tone, whether it looks good. No program can check that, and this one does not claim to.
- Behaviour outside the specification's clauses.
- Inputs the checker was never given. See the evidence line for the corpus.
A MIME email with a non-ASCII subject, a plain and an HTML part, and one attachment.
- One attachment.
- Signing and encryption are out of scope.
Every figure comes from one run, kept in the repository as probe6b_forged_mime_message_result.json.
Evidence
- held-out 16 specs, best-of-2, deepseek-chat: cold 0.312 -> 0.750 with the rung (CI +0.188..+0.688); helper reuse 1.00
- checker forged and mutation-gated, not hand-written: references accepted, every spoiler rejected, empty rejected, on 2 given specs plus 6 generated ($0.17)
- stage-1 gate before forging: 4/8 for the cheap tier cold, so the gate opened
Use it
# in Claude Code (MCP tools from neruva-mcp)
rung_search(q="email_mime_message_rungs")
rung_install(id="rec_d0107eda77bd4c19ade8c9074df8d3ef", dir="~/.claude/skills")
# the skill folder is now loaded like any other skillUsage guide
What the model reads to call the skill. This is the whole interface.
Show the guide
make_attachment(filename, content_type, content) -> (filename, content_type, bytes)
Normalizes an attachment's content (str or bytes) into a tuple ready
for build_email_message's attachments argument.
Example: make_attachment("report.csv", "text/csv", "a,b\n1,2\n")
build_email_message(from_addr, to_addr, subject, plain_text, html_text,
attachments=None, date=None, policy=None)
-> email.message.EmailMessage
Builds a multipart/mixed message whose first part is a
multipart/alternative (text/plain + text/html, both utf-8) and whose
remaining parts are attachments (each marked Content-Disposition:
attachment). From/To/Subject/Date headers are set as given; Subject
may contain non-ASCII and will round-trip via policy.default parsing.
Example:
msg = build_email_message(
"sysadmin@neruva.io", "jane@acme.test",
"R\u00e9sum\u00e9 du d\u00e9ploiement \u2014 \u5efa\u7acb",
"All systems operational.",
"<html><body><p>Your invoice is <b>paid</b>.</p></body></html>",
attachments=[make_attachment("report.csv", "text/csv",
"step,status\nbuild,ok\n")],
)
write_message_to_file(msg, path) -> None
Serializes msg (an EmailMessage) to RFC 5322 bytes and writes them to
the given filesystem path.
Example: write_message_to_file(msg, "/tmp/out.eml")
Code
100 lines of python, hashed and signed below.
Show the code
import os
import datetime as dt
from email.message import EmailMessage
from email.policy import default as default_policy
from email.utils import format_datetime
def make_attachment(filename, content_type, content):
"""
Build a (filename, content_type, bytes) tuple suitable for
build_email_message's `attachments` argument.
filename: str, e.g. "report.csv"
content_type: str, e.g. "text/csv"
content: str or bytes -- if str, it is UTF-8 encoded.
Returns: (filename, content_type, bytes)
Example:
att = make_attachment("report.csv", "text/csv", "a,b\n1,2\n")
"""
if isinstance(content, str):
content = content.encode("utf-8")
return (filename, content_type, content)
def build_email_message(from_addr, to_addr, subject, plain_text, html_text,
attachments=None, date=None, policy=None):
"""
Build an email.message.EmailMessage representing:
multipart/mixed
-> multipart/alternative
-> text/plain (utf-8) = plain_text
-> text/html (utf-8) = html_text
-> one or more attachments (each as its own part, marked
Content-Disposition: attachment)
from_addr, to_addr, subject: str -- placed verbatim into headers
(subject may contain non-ASCII; policy.default handles RFC2047
encoding so it round-trips through the parser).
plain_text: str -- exact text/plain body.
html_text: str -- exact text/html body.
attachments: iterable of (filename, content_type, bytes) tuples,
e.g. as produced by make_attachment(). Defaults to no attachments
(but the spec requires exactly one -- pass a single-item list).
date: optional datetime.datetime; defaults to now (UTC-aware).
policy: optional email policy object; defaults to email.policy.default.
Returns: email.message.EmailMessage
Example:
msg = build_email_message(
"a@x.test", "b@x.test", "Subj \u00e9",
"hello", "<p>hello</p>",
attachments=[make_attachment("f.csv", "text/csv", "a,b\n")],
)
"""
if policy is None:
policy = default_policy
if attachments is None:
attachments = []
if date is None:
date = dt.datetime.now(dt.timezone.utc)
msg = EmailMessage(policy=policy)
msg["From"] = from_addr
msg["To"] = to_addr
msg["Subject"] = subject
msg["Date"] = format_datetime(date)
# First part: multipart/alternative (plain + html), both utf-8.
msg.set_content(plain_text, subtype="plain", charset="utf-8")
msg.add_alternative(html_text, subtype="html", charset="utf-8")
# Second (and further) part(s): attachments.
for (filename, content_type, data) in attachments:
maintype, subtype = content_type.split("/", 1)
msg.add_attachment(
data,
maintype=maintype,
subtype=subtype,
filename=filename,
)
return msg
def write_message_to_file(msg, path):
"""
Serialize an email.message.EmailMessage to RFC 5322 bytes and write
it to `path`.
msg: email.message.EmailMessage (already built with a policy).
path: str, filesystem path to write to.
Returns: None
Example:
write_message_to_file(msg, os.environ["OUTPUT"])
"""
with open(path, "wb") as f:
f.write(msg.as_bytes())
Certificate
Re-checked today. The signature was recomputed from the code served by the API, and the checking program was run again on freshly generated cases: 4/4 references accepted, 24/24 deliberate corruptions rejected.
A certificate proves the code has not changed. Re-running the check proves it still works today, on today's libraries.