Documentation ¶
Overview ¶
Package mail implements composing and parsing of mail messages.
Creating a message with multiple parts and attachments ends up being a surprisingly painful task in Go. That's because the mail package in the standard library only offers tools to parse mail messages and addresses.
This package can replace the net/mail package of the standard library without breaking your existing code.
Features ¶
- multipart support
- attachments
- quoted-printable encoding of body text
- quoted-printable decoding of headers
- getters and setters common headers
Known issues ¶
- Quoted-printable encoding does not respect the 76 characters per line limitation imposed by RFC 2045 (https://github.com/golang/go/issues/4943).
Installation ¶
Alex Cesaro's quotedprintable package (https://godoc.org/gopkg.in/alexcesaro/quotedprintable.v1) is the only external dependency. It's likely to be included in Go 1.5 in a new mime/quotedprintable package (https://codereview.appspot.com/132680044).
go get godoc.org/gopkg.in/alexcesaro/quotedprintable.v1 go get github.com/mohamedattahri/mail
Index ¶
- Variables
- type Address
- type AddressList
- type AttachmentType
- type Header
- type Message
- func (m *Message) Bcc() *AddressList
- func (m *Message) Bytes() []byte
- func (m *Message) Cc() *AddressList
- func (m *Message) ContentType() string
- func (m *Message) Date() time.Time
- func (m *Message) From() *Address
- func (m *Message) GetHeader(header string) string
- func (m *Message) InReplyTo() string
- func (m *Message) MessageID() string
- func (m *Message) ReplyTo() string
- func (m *Message) Sender() string
- func (m *Message) SetContentType(mediaType string)
- func (m *Message) SetFrom(address *Address)
- func (m *Message) SetHeader(header, value string)
- func (m *Message) SetInReplyTo(id string)
- func (m *Message) SetMessageID(id string)
- func (m *Message) SetReplyTo(address string)
- func (m *Message) SetSender(address string)
- func (m *Message) SetSubject(subject string)
- func (m *Message) String() string
- func (m *Message) Subject() string
- func (m *Message) To() *AddressList
- type Multipart
- func (p *Multipart) AddAttachment(attachType AttachmentType, filename, mediaType string, r io.Reader) (err error)
- func (p *Multipart) AddMultipart(mediaType string) (nested *Multipart, err error)
- func (p *Multipart) AddText(mediaType string, r io.Reader) error
- func (p *Multipart) Boundary() string
- func (p *Multipart) Close() error
- func (p *Multipart) Closed() bool
- func (p *Multipart) Header() textproto.MIMEHeader
- func (p *Multipart) MediaType() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrHeaderNotPresent = errors.New("mail: header not in message")
var ErrPartClosed = errors.New("mail: part has been closed")
Functions ¶
This section is empty.
Types ¶
type Address ¶
Address represents a single mail address. An address such as "Barry Gibbs <bg@example.com>" is represented as Address{Name: "Barry Gibbs", Address: "bg@example.com"}.
func ParseAddress ¶
ParseAddress parses a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>"
func ParseAddressList ¶
ParseAddressList parses the given string as a list of addresses.
type AddressList ¶
type AddressList struct {
// contains filtered or unexported fields
}
AddressList implements methods to manipulate a comma-separated list of mail addresses.
func (*AddressList) Addresses ¶
func (a *AddressList) Addresses() ([]*Address, error)
Addresses contained in the list as an array or an error if the underlying string is malformed.
func (*AddressList) Contain ¶
func (a *AddressList) Contain(address *Address) bool
Contain returns a value indicating whether address is in the list.
func (*AddressList) Remove ¶
func (a *AddressList) Remove(address *Address)
Remove address from the list.
func (*AddressList) String ¶
func (a *AddressList) String() string
String returns the addresses in the list in a comma-separated string.
type AttachmentType ¶
type AttachmentType string
AttachmentType indicates to the mail user agent how an attachment should be treated.
const ( // Attachment indicates that the attachment should be offered as an optional // download. Attachment AttachmentType = "attachment" // Inline indicates that the attachment should be rendered in the body of // the message. Inline AttachmentType = "inline" )
type Header ¶
A Header represents the key-value pairs in a mail message header.
func (Header) AddressList ¶
AddressList parses the named header field as a list of addresses.
type Message ¶
type Message struct { Header Header Body io.ReadWriter // contains filtered or unexported fields }
A Message represents a mail message.
Example ¶
Example of a simple message with plain text.
msg := NewMessage() msg.SetFrom(&Address{"Al Bumin", "a.bumin@example.name"}) msg.To().Add(&Address{"Polly Ester", "p.ester@example.com"}) msg.SetSubject("Simple message with plain text") msg.SetContentType("text/plain") fmt.Fprintf(msg.Body, "Hello, World!") fmt.Println(msg)
Output:
Example (Alternative) ¶
Example of a message with HTML and alternative plain text.
text := bytes.NewReader([]byte("Hello, World!")) html := bytes.NewReader([]byte("<html><body>Hello, World!</body></html>")) msg := NewMessage() msg.SetFrom(&Address{"Al Bumin", "a.bumin@example.name"}) msg.To().Add(&Address{"Polly Ester", "p.ester@example.com"}) msg.SetSubject("Message with HTML and alternative text") alternative := NewMultipart("multipart/alternative", msg) alternative.AddText("text/plain", text) alternative.AddText("text/html", html) alternative.Close() fmt.Println(msg)
Output:
Example (Cid) ¶
This example shows how to use the cid URI Scheme to use an attachment as a data source for an HTML img tag.
msg := NewMessage() msg.SetFrom(&Address{"Al Bumin", "a.bumin@example.name"}) msg.To().Add(&Address{"Polly Ester", "p.ester@example.com"}) msg.SetSubject("Message with HTML, alternative text, and an attachment") mixed := NewMultipart("multipart/mixed", msg) // filename is the name that will be suggested to a user who would like to // download the attachment, but also the ID with which you can refer to the // attachment in a cid URI scheme. filename := "gopher.jpg" // The src of the image in this HTML is set to use the attachment with the // Content-ID filename. html := fmt.Sprintf("<html><body><img src=\"cid:%s\"/></body></html>", filename) mixed.AddText("text/html", bytes.NewReader([]byte(html))) // Load the photo and add the attachment with filename. attachment, _ := ioutil.ReadFile("path/of/image.jpg") mixed.AddAttachment(Attachment, filename, "image/jpeg", bytes.NewReader(attachment)) // Closing mixed, the parent part. mixed.Close() fmt.Println(msg)
Output:
Example (Mixed) ¶
Example of a message with HTML content, alternative text, and an attachment.
text := bytes.NewReader([]byte("Hello, World!")) html := bytes.NewReader([]byte("<html><body>Hello, World!</body></html>")) data, _ := ioutil.ReadFile("path/of/photo.jpg") attachment := bytes.NewReader(data) msg := NewMessage() msg.SetFrom(&Address{"Al Bumin", "a.bumin@example.name"}) msg.To().Add(&Address{"Polly Ester", "p.ester@example.com"}) msg.SetSubject("Message with HTML, alternative text, and an attachment") mixed := NewMultipart("multipart/mixed", msg) alternative, _ := mixed.AddMultipart("multipart/alternative") alternative.AddText("text/plain", text) alternative.AddText("text/html", html) alternative.Close() mixed.AddAttachment(Attachment, "Photo", "image/jpeg", attachment) mixed.Close() fmt.Println(msg)
Output:
func ReadMessage ¶
ReadMessage reads a message from r. The headers are parsed, and the body of the message will be available for reading from r.
func (*Message) Bcc ¶
func (m *Message) Bcc() *AddressList
Bcc gives access to the list of recipients Bcced in the message.
func (*Message) Cc ¶
func (m *Message) Cc() *AddressList
Cc gives access to the list of recipients Cced in the message.
func (*Message) ContentType ¶
ContentType returns the MIME type of the message.
func (*Message) GetHeader ¶
GetHeader returns the undecoded value of header if found. To access the raw (potentially encoded) value of header, use the Message.Header.
func (*Message) InReplyTo ¶
InReplyTo returns the Message-Id of the message that this message is a reply to.
func (*Message) Sender ¶
Sender returns the address of the actual sender acting on behalf of the author listed in From.
func (*Message) SetContentType ¶
SetContentType returns the MIME type of the message.
func (*Message) SetHeader ¶
SetHeader adds header to the list of headers and sets it to quoted-printable encoded value.
func (*Message) SetInReplyTo ¶
SetInReplyTo sets the Message-Id of the message that this message is a reply to.
func (*Message) SetMessageID ¶
SetMessageID sets the unique identifier of this message.
func (*Message) SetReplyTo ¶
SetReplyTo sets the address that should be used to reply to the message.
func (*Message) SetSender ¶
SetSender sets the address of the actual sender acting on behalf of the author listed in From.
func (*Message) SetSubject ¶
SetSubject sets the subject line of the message.
func (*Message) To ¶
func (m *Message) To() *AddressList
To gives access to the list of recipients of the message.
type Multipart ¶
type Multipart struct {
// contains filtered or unexported fields
}
Multipart repreents a multipart message body. It can other nest multiparts, texts, and attachments.
func NewMultipart ¶
NewMultipart modifies msg to become a multipart message and returns the root part inside which other parts, texts and attachments can be nested.
Example:
multipart := NewMultipart("multipart/alternative", msg) multipart.AddPart("text/plain", text) multipart.AddPart("text/html", html) multipart.Close()
func (*Multipart) AddAttachment ¶
func (p *Multipart) AddAttachment(attachType AttachmentType, filename, mediaType string, r io.Reader) (err error)
AddAttachment encodes the content of r in base64 and writes it as an attachment of type attachType in this part.
filename is the file name that will be suggested by the mail user agent to a user who would like to download the attachment. It's also the value to which the Content-ID header will be set. A name with an extension such as "report.docx" or "photo.jpg" is recommended. RFC 5987 is not supported, so the charset is restricted to ASCII characters.
mediaType indicates the content type of the attachment. If an empty string is passed, mime.TypeByExtension will first be called to deduce a value from the extension of filemame before defaulting to "application/octet-stream".
In the following example, the media MIME type will be set to "image/png" based on the ".png" extension of the filename "gopher.png":
part.AddAttachment(Inline, "gopher.png", "", image)
func (*Multipart) AddMultipart ¶
AddMultipart creates a nested part with mediaType and a randomly generated boundary. The returned nested part can then be used to add a text or an attachment.
Example:
alt, _ := part.AddMultipart("multipart/mixed") alt.AddText("text/plain", text) alt.AddAttachment("gopher.png", "", image) alt.Close()
func (*Multipart) AddText ¶
AddText applies quoted-printable encoding to the content of r before writing the encoded result in a new sub-part with media MIME type set to mediaType.
Specifying the charset in the mediaType string is recommended ("plain/text; charset=utf-8").
func (*Multipart) Close ¶
Close adds a closing boundary to the part.
Calling AddText, AddAttachment or AddMultipart on a closed part will return ErrPartClosed.
func (*Multipart) Header ¶
func (p *Multipart) Header() textproto.MIMEHeader
Header map of the part.