mailtoolkit

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

mailtoolkit

CI Go Reference

GO mail toolkit to parse email content to an object.

Built on the standard library only (net/mail, mime, mime/multipart), requires Go 1.23 or later.

buffer, err := os.ReadFile("mail.eml")
if err != nil {
	log.Fatal(err)
}
mail, err := mailtoolkit.Parse(buffer)
if err != nil {
	// The mail is malformed or truncated, mail still holds what could be read.
	log.Print(err)
}
fmt.Println(mail.Header.From, mail.Header.Subject)
for name, attachment := range mail.Attachments {
	data, err := attachment.Decode() // removes base64 / quoted-printable
	...
}

Breaking changes since the 2018 version

  • Parse and ParseHeader return an error instead of panicking on malformed mails.
  • ParseContents and the exported regular expressions / MIMEContentTypes variables are gone.
  • Header.To, Cc and Bcc hold every address (comma separated), not only the first one. The new ToList, CcList, ... fields keep the display names.
  • Subject, Content-Description and attachment names are decoded (RFC 2047 / RFC 2231).
  • Part data no longer includes the line break preceding the next boundary.

Documentation

Overview

Package mailtoolkit parses a raw RFC 5322 / MIME email into a Mail object.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	ContentInfo ContentInfo
	Data        []byte
}

Attachment is an attached file. Data is still transfer-encoded, use Decode to get the raw bytes.

func (Attachment) Decode

func (a Attachment) Decode() ([]byte, error)

Decode returns the attachment data with its Content-Transfer-Encoding removed.

type Content

type Content struct {
	ContentInfo ContentInfo
	Data        []byte
}

Content is a displayable part of a mail. Data is still transfer-encoded, use Decode to get the raw bytes.

func (Content) Decode

func (c Content) Decode() ([]byte, error)

Decode returns the content data with its Content-Transfer-Encoding removed.

type ContentDisposition

type ContentDisposition struct {
	Type       string
	Parameters map[string]string
}

ContentDisposition is a parsed Content-Disposition field. Type and parameter names are lowercase.

type ContentInfo

type ContentInfo struct {
	Type             ContentType
	ID               string
	Description      string
	TransferEncoding string
	Disposition      ContentDisposition
}

ContentInfo describes a MIME part.

type ContentType

type ContentType struct {
	Type       string
	Subtype    string
	Parameters map[string]string
}

ContentType is a parsed Content-Type field. Type, Subtype and parameter names are lowercase.

type Header struct {
	IsMime bool
	// From, To, Cc and Bcc are comma separated lists of e-mail addresses.
	// When the mail has no To field, To falls back to Delivered-To.
	From string
	To   string
	Cc   string
	Bcc  string
	// FromList, ToList, CcList and BccList keep the display names.
	FromList []*mail.Address
	ToList   []*mail.Address
	CcList   []*mail.Address
	BccList  []*mail.Address
	// Date is the raw Date field, Time its parsed value (zero if unparsable).
	Date    string
	Time    time.Time
	Subject string
	// Elements contains every header field, keyed by lowercase name.
	// Repeated fields are joined with "\n".
	Elements    map[string]string
	ContentInfo ContentInfo
}

Header holds the top-level header fields of a mail.

func ParseHeader

func ParseHeader(buffer []byte) (Header, error)

ParseHeader parses only the header of a raw email.

type Mail

type Mail struct {
	Header Header
	// Contents are the displayable parts of the mail, keyed by Content-ID
	// (without angle brackets) or, when the part has no Content-ID, by its
	// position ("0", "1", ...).
	Contents map[string]Content
	// Attachments are keyed by their decoded file name, made unique when
	// several attachments share the same name.
	Attachments map[string]Attachment
}

Mail is a parsed email.

func Parse

func Parse(buffer []byte) (Mail, error)

Parse parses a raw email.

A malformed mail never makes Parse panic. When the body cannot be fully read (truncated multipart, missing boundary...), Parse returns everything it could extract together with a non-nil error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL