mime

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package mime encodes electronic mail parts for delivery.

One way to encode the message is as either mixed or related, depending on the presence of inline attachments:

multipart/related
	|- multipart/alternative
	| |- text/plain
	| `- text/html
	|- attachments...
	`- inline_attachments...

This approach may not work in older clients, like Apple Mail.

A better way to structure the multipart MIME message is by subordinating the inline attachments to the alternative related part. The packages uses this approach to ensure compatibility with older clients:

multipart/mixed
|- multipart/alternative
|  |- text/plain
|  `- multipart/related
|     |- text/html
|     `- inline_attachments...
`- attachments...

Inline images can be encoded into the HTML image tag as base64 encoded data. However, Outlook completely blocks such images. Therefore, this package always encodes inline images as attachments and references them via a content ID in the HTML image source attribute.

All output of this package is wrapped to LineLengthLimit, not just the headers. RFC 5321 requires that electronic mail text lines must not exceed 1,000 characters. This requirement is full satisfied. The content of plain text and HTML sections are wrapped by `mime/quotedprintable` encoding of the stardard library.

Index

Constants

View Source
const (
	// LineLengthLimit is the maximum length of a line
	// for MIME encoding.
	//
	// RFC 5322 2.1.1 limits to 78, excluding CRLF.
	// `mime/quotedprintable` sets this to 76.
	LineLengthLimit = 76

	// BoundaryLengthLimit is the maximum length of a boundary string
	// for MIME encoding according to RFC 1341 set to 70 characters. Boundary can never
	// wrap to the next line, as space characters are not allowed in boundaries.
	// Therefore, the maximum length must be reduced by the length of the parameter name.
	BoundaryLengthLimit = 70 - len(`boundary`) + 2

	BEncodingPrefix = "=?UTF-8?b?"
	BEncodingSuffix = "?="
	CRNL            = "\r\n"

	ContentTypeOctetStream = "application/octet-stream"
	ContentTypeTextPlain   = "text/plain"
	ContentTypeTextHTML    = "text/html"
	ContentTypeImageJPEG   = "image/jpeg"
	ContentTypeImageBMP    = "image/bmp"
	ContentTypeImagePNG    = "image/png"
	ContentTypeImageGIF    = "image/gif"
	ContentTypeImageWEBP   = "image/webp"
	ContentTypeZip         = "application/zip"
)

Variables

This section is empty.

Functions

func FileContentDisposition

func FileContentDisposition(fieldname, filename string) string

FileContentDisposition returns the value of a Content-Disposition header with the provided field name and file name.

func NewBoundary

func NewBoundary(r *rand.Rand) string

func NewEncoderBase64

func NewEncoderBase64(w io.Writer) io.WriteCloser

NewEncoderBase64 encodes data to standard Base64 encoding while keeping each line under LineLengthLimit. Encoder must be closed to work correctly.

func SplitAttachments

func SplitAttachments(s string, attachments []cachedAttachment) (normal []cachedAttachment, inline []inlineAttachment)

func WriteAddressHeader

func WriteAddressHeader(w io.Writer, name string, addresses ...mail.Address) (err error)

WriteAddressHeader writes a list of MIME-mail encoded address header to the given writer. It respects LineLengthLimit by wrapping long lines. If no addresses are provided, it does nothing.

TODO: if the name is too long, it will not be wrapped. TODO: if the email address is too long, it will not be wrapped.

func WriteHeader

func WriteHeader(w io.Writer, name, value string) (n int, err error)

TODO: remove <n int> from WriteHeader signature

func WriteTimeHeader

func WriteTimeHeader(w io.Writer, name string, t time.Time) (int, error)

Types

type AttachmentRepository

type AttachmentRepository interface {
	ListAttachments(ctx context.Context, letterID string) iter.Seq2[mdsend.Attachment, error]
}

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

func NewWriter

func NewWriter(attachments AttachmentRepository, entropy *rand.Rand) Writer

func (Writer) Write

func (w Writer) Write(
	ctx context.Context,
	out io.Writer,
	m mdsend.Message,
) (err error)

func (Writer) WriteMixedBoundaryHeader

func (w Writer) WriteMixedBoundaryHeader(out io.Writer) (err error)

func (Writer) WriteRelatedBoundaryHeader

func (w Writer) WriteRelatedBoundaryHeader(out io.Writer) (err error)

Jump to

Keyboard shortcuts

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