Parse RFC 5322 Messages With mail.ReadMessage
Parse RFC 5322 headers, sender and recipient lists, timestamps, and message body streams from an in-memory reader using net/mail.ReadMessage.
Code
package main
import (
"errors"
"fmt"
"io"
"net/mail"
"strings"
)
func inspectMessage(raw string) error {
msg, err := mail.ReadMessage(strings.NewReader(raw))
if err != nil {
return err
}
from, err := msg.Header.AddressList("From")
if err != nil {
return err
}
to, err := msg.Header.AddressList("To")
if err != nil {
return err
}
sentAt, err := msg.Header.Date()
if err != nil {
return err
}
body, err := io.ReadAll(msg.Body)
if err != nil {
return err
}
fmt.Printf("subject: %s\n", msg.Header.Get("Subject"))
fmt.Printf("from: %s <%s>\n", from[0].Name, from[0].Address)
for i, addr := range to {
fmt.Printf("to[%d]: %s <%s>\n", i, addr.Name, addr.Address)
}
fmt.Printf("date utc: %s\n", sentAt.UTC().Format("2006-01-02T15:04:05Z"))
fmt.Printf("body: %q\n", strings.TrimSpace(string(body)))
_, ccErr := msg.Header.AddressList("Cc")
fmt.Println("missing Cc sentinel:", errors.Is(ccErr, mail.ErrHeaderNotPresent))
return nil
}
func main() {
raw := "From: \"Build Bot\" <[email protected]>\r\n" +
"To: Alice <[email protected]>, Ops Team <[email protected]>\r\n" +
"Date: Tue, 24 Nov 2026 08:30:00 +0000\r\n" +
"Subject: Nightly digest\r\n" +
"\r\n" +
"All 42 checks passed.\r\n"
if err := inspectMessage(raw); err != nil {
fmt.Println("mail error:", err)
}
}Execution walkthrough — Parse RFC 5322 Messages With mail.ReadMessage
Everything happens inside the functions inspectMessage and main.
A single loop drives the file — for i, addr := range to. Every fallible call checks err != nil (6 sites), so an error is handled where it happens rather than bubbling invisibly.
What to watch for
- Runs on the standard library alone; nothing to install.
- Error values are returned, not thrown — every call site must inspect
errbefore using the other return values.
Using it
Control centers upon signature func inspectMessage(raw string) error
Printed rows surface annotated tags — subject:, from:, to[ — when the file runs.
Running the file executes main, which drives inspectMessage in order.
Key statements
for i, addr := range to {
fmt.Printf("to[%d]: %s <%s>\n", i, addr.Name, addr.Address)
}
if err != nil {
return err
}
Interactive Tools for This Topic
Base45 Encoder and DecoderEncode and decode text with the Base45 alphabet defined in RFC 9285, with UTF-8 support, padding-free output, and clear validation errors.cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.UUID Version DetectorValidate the 8-4-4-4-12 UUID layout and read its version (v1 to v8) and variant nibbles per RFC 9562, with full field decomposition.
FAQ
- How are errors reported?
- As returned `error` values, checked at 6 sites — callers must branch on `err` before using the other return values. No panics or exceptions in the snippet.
- What does Parse RFC 5322 Messages With mail.ReadMessage print when run?
- Execute the whole file; each printed line is prefixed with a label like `subject:`, `from:`, `to[`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.