mail

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2018 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package mail implements reading and writing mail messages.

This package assumes that a mail message contains one or more text parts and zero or more attachment parts. Each text part represents a different version of the message content (e.g. a different type, a different language and so on).

RFC 5322 defines the Internet Message Format.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Address

type Address mail.Address

Address represents a single mail address.

func (*Address) String

func (a *Address) String() string

String formats the address as a valid RFC 5322 address. If the address's name contains non-ASCII characters the name will be rendered according to RFC 2047.

type AttachmentHeader

type AttachmentHeader struct {
	message.Header
}

An AttachmentHeader represents an attachment's header.

func NewAttachmentHeader

func NewAttachmentHeader() AttachmentHeader

NewAttachmentHeader creates a new AttachmentHeader.

func (AttachmentHeader) Filename

func (h AttachmentHeader) Filename() (string, error)

Filename parses the attachment's filename.

func (AttachmentHeader) SetFilename

func (h AttachmentHeader) SetFilename(filename string)

SetFilename formats the attachment's filename.

type Header struct {
	message.Header
}

A Header is a mail header.

func NewHeader

func NewHeader() Header

NewHeader creates a new mail header.

func (Header) AddressList

func (h Header) AddressList(key string) ([]*Address, error)

AddressList parses the named header field as a list of addresses. If the header is missing, it returns nil.

func (Header) Date

func (h Header) Date() (time.Time, error)

Date parses the Date header field.

func (Header) SetAddressList

func (h Header) SetAddressList(key string, addrs []*Address)

SetAddressList formats the named header to the provided list of addresses.

func (Header) SetDate

func (h Header) SetDate(t time.Time)

SetDate formats the Date header field.

func (Header) SetSubject

func (h Header) SetSubject(s string)

SetSubject formats the Subject header field.

func (Header) Subject

func (h Header) Subject() (string, error)

Subject parses the Subject header field. If there is an error, the raw field value is returned alongside the error.

type Part

type Part struct {
	Header PartHeader
	Body   io.Reader
}

A Part is either a mail text or an attachment. Header is either a TextHeader or an AttachmentHeader.

type PartHeader

type PartHeader interface {
	// Add adds the key, value pair to the header.
	Add(key, value string)
	// Del deletes the values associated with key.
	Del(key string)
	// Get gets the first value associated with the given key. If there are no
	// values associated with the key, Get returns "".
	Get(key string) string
	// Set sets the header entries associated with key to the single element
	// value. It replaces any existing values associated with key.
	Set(key, value string)
}

A PartHeader is a mail part header. It contains convenience functions to get and set header fields.

type Reader

type Reader struct {
	Header Header
	// contains filtered or unexported fields
}

A Reader reads a mail message.

Example
package main

import (
	"io"
	"io/ioutil"
	"log"

	"github.com/emersion/go-message/mail"
)

func main() {
	// Let's assume r is an io.Reader that contains a mail.
	var r io.Reader

	// Create a new mail reader
	mr, err := mail.CreateReader(r)
	if err != nil {
		log.Fatal(err)
	}

	// Read each mail's part
	for {
		p, err := mr.NextPart()
		if err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		}

		switch h := p.Header.(type) {
		case mail.TextHeader:
			b, _ := ioutil.ReadAll(p.Body)
			log.Println("Got text: %v", string(b))
		case mail.AttachmentHeader:
			filename, _ := h.Filename()
			log.Println("Got attachment: %v", filename)
		}
	}
}
Output:

func CreateReader

func CreateReader(r io.Reader) (*Reader, error)

CreateReader reads a mail header from r and returns a new mail reader.

If the message uses an unknown transfer encoding or charset, CreateReader returns an error that verifies message.IsUnknownEncoding, but also returns a Reader that can be used.

func NewReader

func NewReader(e *message.Entity) *Reader

NewReader creates a new mail reader.

func (*Reader) Close

func (r *Reader) Close() error

Close finishes the reader.

func (*Reader) NextPart

func (r *Reader) NextPart() (*Part, error)

NextPart returns the next mail part. If there is no more part, io.EOF is returned as error.

The returned Part.Body must be read completely before the next call to NextPart, otherwise it will be discarded.

If the part uses an unknown transfer encoding or charset, NextPart returns an error that verifies message.IsUnknownEncoding, but also returns a Part that can be used.

type TextHeader

type TextHeader struct {
	message.Header
}

A TextHeader represents a message text header.

func NewTextHeader

func NewTextHeader() TextHeader

NewTextHeader creates a new message text header.

type TextWriter

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

TextWriter writes a mail message's text.

func (*TextWriter) Close

func (w *TextWriter) Close() error

Close finishes the TextWriter.

func (*TextWriter) CreatePart

func (w *TextWriter) CreatePart(header TextHeader) (io.WriteCloser, error)

CreatePart creates a new text part with the provided header. The body of the part should be written to the returned io.WriteCloser.

type Writer

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

A Writer writes a mail message. A mail message contains one or more text parts and zero or more attachments.

Example
package main

import (
	"bytes"
	"io"
	"log"
	"time"

	"github.com/emersion/go-message/mail"
)

func main() {
	var b bytes.Buffer

	from := []*mail.Address{{"Mitsuha Miyamizu", "mitsuha.miyamizu@example.org"}}
	to := []*mail.Address{{"Taki Tachibana", "taki.tachibana@example.org"}}

	// Create our mail header
	h := mail.NewHeader()
	h.SetDate(time.Now())
	h.SetAddressList("From", from)
	h.SetAddressList("To", to)

	// Create a new mail writer
	mw, err := mail.CreateWriter(&b, h)
	if err != nil {
		log.Fatal(err)
	}

	// Create a text part
	tw, err := mw.CreateText()
	if err != nil {
		log.Fatal(err)
	}
	th := mail.NewTextHeader()
	th.Set("Content-Type", "text/plain")
	w, err := tw.CreatePart(th)
	if err != nil {
		log.Fatal(err)
	}
	io.WriteString(w, "Who are you?")
	w.Close()
	tw.Close()

	// Create an attachment
	ah := mail.NewAttachmentHeader()
	ah.Set("Content-Type", "image/jpeg")
	ah.SetFilename("picture.jpg")
	w, err = mw.CreateAttachment(ah)
	if err != nil {
		log.Fatal(err)
	}
	// TODO: write a JPEG file to w
	w.Close()

	mw.Close()

	log.Println(b.String())
}
Output:

func CreateWriter

func CreateWriter(w io.Writer, header Header) (*Writer, error)

CreateWriter writes a mail header to w and creates a new Writer.

func (*Writer) Close

func (w *Writer) Close() error

Close finishes the Writer.

func (*Writer) CreateAttachment

func (w *Writer) CreateAttachment(header AttachmentHeader) (io.WriteCloser, error)

CreateAttachment creates a new attachment with the provided header. The body of the part should be written to the returned io.WriteCloser.

func (*Writer) CreateSingleText

func (w *Writer) CreateSingleText(header TextHeader) (io.WriteCloser, error)

CreateSingleText creates a new single text part with the provided header. The body of the part should be written to the returned io.WriteCloser. Only one single text part should be written, use CreateText if you want multiple text parts.

func (*Writer) CreateText

func (w *Writer) CreateText() (*TextWriter, error)

CreateText creates a TextWriter. One or more parts representing the same text in different formats can be written to a TextWriter.

Jump to

Keyboard shortcuts

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