2014-10-15 23:47:07 +08:00
|
|
|
package gomail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2015-07-09 01:53:11 +08:00
|
|
|
"os"
|
2014-10-15 23:47:07 +08:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Message represents an email.
|
|
|
|
type Message struct {
|
|
|
|
header header
|
2015-12-12 23:40:02 +08:00
|
|
|
parts []*part
|
2015-08-04 04:57:38 +08:00
|
|
|
attachments []*file
|
|
|
|
embedded []*file
|
2014-10-15 23:47:07 +08:00
|
|
|
charset string
|
|
|
|
encoding Encoding
|
2015-07-17 01:33:38 +08:00
|
|
|
hEncoder mimeEncoder
|
2015-07-15 04:24:56 +08:00
|
|
|
buf bytes.Buffer
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type header map[string][]string
|
|
|
|
|
|
|
|
type part struct {
|
2015-12-12 23:40:02 +08:00
|
|
|
contentType string
|
|
|
|
copier func(io.Writer) error
|
|
|
|
encoding Encoding
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessage creates a new message. It uses UTF-8 and quoted-printable encoding
|
|
|
|
// by default.
|
|
|
|
func NewMessage(settings ...MessageSetting) *Message {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := &Message{
|
2014-10-15 23:47:07 +08:00
|
|
|
header: make(header),
|
|
|
|
charset: "UTF-8",
|
|
|
|
encoding: QuotedPrintable,
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
m.applySettings(settings)
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
if m.encoding == Base64 {
|
2015-07-17 01:33:38 +08:00
|
|
|
m.hEncoder = bEncoding
|
2014-10-15 23:47:07 +08:00
|
|
|
} else {
|
2015-07-17 01:33:38 +08:00
|
|
|
m.hEncoder = qEncoding
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
return m
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 04:26:22 +08:00
|
|
|
// Reset resets the message so it can be reused. The message keeps its previous
|
|
|
|
// settings so it is in the same state that after a call to NewMessage.
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) Reset() {
|
|
|
|
for k := range m.header {
|
|
|
|
delete(m.header, k)
|
2015-07-15 04:26:22 +08:00
|
|
|
}
|
2015-07-15 05:34:21 +08:00
|
|
|
m.parts = nil
|
|
|
|
m.attachments = nil
|
|
|
|
m.embedded = nil
|
2015-07-15 04:26:22 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) applySettings(settings []MessageSetting) {
|
2014-10-15 23:47:07 +08:00
|
|
|
for _, s := range settings {
|
2015-07-15 05:34:21 +08:00
|
|
|
s(m)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A MessageSetting can be used as an argument in NewMessage to configure an
|
|
|
|
// email.
|
2015-07-15 05:34:21 +08:00
|
|
|
type MessageSetting func(m *Message)
|
2014-10-15 23:47:07 +08:00
|
|
|
|
|
|
|
// SetCharset is a message setting to set the charset of the email.
|
|
|
|
func SetCharset(charset string) MessageSetting {
|
2015-07-15 05:34:21 +08:00
|
|
|
return func(m *Message) {
|
|
|
|
m.charset = charset
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEncoding is a message setting to set the encoding of the email.
|
|
|
|
func SetEncoding(enc Encoding) MessageSetting {
|
2015-07-15 05:34:21 +08:00
|
|
|
return func(m *Message) {
|
|
|
|
m.encoding = enc
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encoding represents a MIME encoding scheme like quoted-printable or base64.
|
|
|
|
type Encoding string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// QuotedPrintable represents the quoted-printable encoding as defined in
|
|
|
|
// RFC 2045.
|
|
|
|
QuotedPrintable Encoding = "quoted-printable"
|
|
|
|
// Base64 represents the base64 encoding as defined in RFC 2045.
|
|
|
|
Base64 Encoding = "base64"
|
2014-10-28 15:35:50 +08:00
|
|
|
// Unencoded can be used to avoid encoding the body of an email. The headers
|
|
|
|
// will still be encoded using quoted-printable encoding.
|
|
|
|
Unencoded Encoding = "8bit"
|
2014-10-15 23:47:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetHeader sets a value to the given header field.
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) SetHeader(field string, value ...string) {
|
2015-08-04 04:57:38 +08:00
|
|
|
m.encodeHeader(value)
|
2015-07-15 05:34:21 +08:00
|
|
|
m.header[field] = value
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-08-04 04:57:38 +08:00
|
|
|
func (m *Message) encodeHeader(values []string) {
|
|
|
|
for i := range values {
|
|
|
|
values[i] = m.encodeString(values[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) encodeString(value string) string {
|
|
|
|
return m.hEncoder.Encode(m.charset, value)
|
|
|
|
}
|
|
|
|
|
2014-10-15 23:47:07 +08:00
|
|
|
// SetHeaders sets the message headers.
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) SetHeaders(h map[string][]string) {
|
2014-10-15 23:47:07 +08:00
|
|
|
for k, v := range h {
|
2015-07-15 05:34:21 +08:00
|
|
|
m.SetHeader(k, v...)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddressHeader sets an address to the given header field.
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) SetAddressHeader(field, address, name string) {
|
|
|
|
m.header[field] = []string{m.FormatAddress(address, name)}
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatAddress formats an address and a name as a valid RFC 5322 address.
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) FormatAddress(address, name string) string {
|
2016-03-21 00:20:50 +08:00
|
|
|
if name == "" {
|
|
|
|
return address
|
|
|
|
}
|
|
|
|
|
2015-08-04 04:57:38 +08:00
|
|
|
enc := m.encodeString(name)
|
2015-06-27 23:20:04 +08:00
|
|
|
if enc == name {
|
2015-07-15 05:34:21 +08:00
|
|
|
m.buf.WriteByte('"')
|
2015-07-15 04:24:56 +08:00
|
|
|
for i := 0; i < len(name); i++ {
|
|
|
|
b := name[i]
|
|
|
|
if b == '\\' || b == '"' {
|
2015-07-15 05:34:21 +08:00
|
|
|
m.buf.WriteByte('\\')
|
2015-07-15 04:24:56 +08:00
|
|
|
}
|
2015-07-15 05:34:21 +08:00
|
|
|
m.buf.WriteByte(b)
|
2015-07-15 04:24:56 +08:00
|
|
|
}
|
2015-07-15 05:34:21 +08:00
|
|
|
m.buf.WriteByte('"')
|
2015-06-27 23:20:04 +08:00
|
|
|
} else if hasSpecials(name) {
|
2015-07-17 01:33:38 +08:00
|
|
|
m.buf.WriteString(bEncoding.Encode(m.charset, name))
|
2015-03-15 02:03:17 +08:00
|
|
|
} else {
|
2015-07-15 05:34:21 +08:00
|
|
|
m.buf.WriteString(enc)
|
2015-03-15 01:41:58 +08:00
|
|
|
}
|
2015-07-15 05:34:21 +08:00
|
|
|
m.buf.WriteString(" <")
|
|
|
|
m.buf.WriteString(address)
|
|
|
|
m.buf.WriteByte('>')
|
2015-03-15 01:41:58 +08:00
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
addr := m.buf.String()
|
|
|
|
m.buf.Reset()
|
2015-07-15 04:24:56 +08:00
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasSpecials(text string) bool {
|
|
|
|
for i := 0; i < len(text); i++ {
|
|
|
|
switch c := text[i]; c {
|
|
|
|
case '(', ')', '<', '>', '[', ']', ':', ';', '@', '\\', ',', '.', '"':
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-10-15 23:47:07 +08:00
|
|
|
// SetDateHeader sets a date to the given header field.
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) SetDateHeader(field string, date time.Time) {
|
|
|
|
m.header[field] = []string{m.FormatDate(date)}
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatDate formats a date as a valid RFC 5322 date.
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) FormatDate(date time.Time) string {
|
2015-02-28 23:46:44 +08:00
|
|
|
return date.Format(time.RFC1123Z)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeader gets a header field.
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) GetHeader(field string) []string {
|
|
|
|
return m.header[field]
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-12-08 21:20:19 +08:00
|
|
|
// SetBody sets the body of the message. It replaces any content previously set
|
|
|
|
// by SetBody, AddAlternative or AddAlternativeWriter.
|
2015-12-12 23:40:02 +08:00
|
|
|
func (m *Message) SetBody(contentType, body string, settings ...PartSetting) {
|
|
|
|
m.parts = []*part{m.newPart(contentType, newCopier(body), settings)}
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-08-24 00:55:53 +08:00
|
|
|
// AddAlternative adds an alternative part to the message.
|
|
|
|
//
|
|
|
|
// It is commonly used to send HTML emails that default to the plain text
|
2015-12-08 21:20:19 +08:00
|
|
|
// version for backward compatibility. AddAlternative appends the new part to
|
|
|
|
// the end of the message. So the plain text part should be added before the
|
|
|
|
// HTML part. See http://en.wikipedia.org/wiki/MIME#Alternative
|
2015-12-12 23:40:02 +08:00
|
|
|
func (m *Message) AddAlternative(contentType, body string, settings ...PartSetting) {
|
|
|
|
m.AddAlternativeWriter(contentType, newCopier(body), settings...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCopier(s string) func(io.Writer) error {
|
|
|
|
return func(w io.Writer) error {
|
|
|
|
_, err := io.WriteString(w, s)
|
|
|
|
return err
|
|
|
|
}
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-07-08 06:26:19 +08:00
|
|
|
// AddAlternativeWriter adds an alternative part to the message. It can be
|
2015-08-24 00:55:53 +08:00
|
|
|
// useful with the text/template or html/template packages.
|
2015-12-12 23:40:02 +08:00
|
|
|
func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error, settings ...PartSetting) {
|
|
|
|
m.parts = append(m.parts, m.newPart(contentType, f, settings))
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-12-12 23:40:02 +08:00
|
|
|
func (m *Message) newPart(contentType string, f func(io.Writer) error, settings []PartSetting) *part {
|
|
|
|
p := &part{
|
|
|
|
contentType: contentType,
|
|
|
|
copier: f,
|
|
|
|
encoding: m.encoding,
|
2015-07-24 05:22:08 +08:00
|
|
|
}
|
2015-12-12 23:40:02 +08:00
|
|
|
|
|
|
|
for _, s := range settings {
|
|
|
|
s(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// A PartSetting can be used as an argument in Message.SetBody,
|
|
|
|
// Message.AddAlternative or Message.AddAlternativeWriter to configure the part
|
|
|
|
// added to a message.
|
|
|
|
type PartSetting func(*part)
|
|
|
|
|
|
|
|
// SetPartEncoding sets the encoding of the part added to the message. By
|
|
|
|
// default, parts use the same encoding than the message.
|
|
|
|
func SetPartEncoding(e Encoding) PartSetting {
|
|
|
|
return PartSetting(func(p *part) {
|
|
|
|
p.encoding = e
|
|
|
|
})
|
2015-07-24 05:22:08 +08:00
|
|
|
}
|
|
|
|
|
2015-08-04 04:57:38 +08:00
|
|
|
type file struct {
|
|
|
|
Name string
|
|
|
|
Header map[string][]string
|
|
|
|
CopyFunc func(w io.Writer) error
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-08-04 04:57:38 +08:00
|
|
|
func (f *file) setHeader(field, value string) {
|
|
|
|
f.Header[field] = []string{value}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A FileSetting can be used as an argument in Message.Attach or Message.Embed.
|
|
|
|
type FileSetting func(*file)
|
|
|
|
|
|
|
|
// SetHeader is a file setting to set the MIME header of the message part that
|
|
|
|
// contains the file content.
|
|
|
|
//
|
|
|
|
// Mandatory headers are automatically added if they are not set when sending
|
|
|
|
// the email.
|
|
|
|
func SetHeader(h map[string][]string) FileSetting {
|
|
|
|
return func(f *file) {
|
|
|
|
for k, v := range h {
|
|
|
|
f.Header[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 06:24:17 +08:00
|
|
|
// Rename is a file setting to set the name of the attachment if the name is
|
|
|
|
// different than the filename on disk.
|
|
|
|
func Rename(name string) FileSetting {
|
|
|
|
return func(f *file) {
|
|
|
|
f.Name = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 04:57:38 +08:00
|
|
|
// SetCopyFunc is a file setting to replace the function that runs when the
|
2015-08-24 00:55:53 +08:00
|
|
|
// message is sent. It should copy the content of the file to the io.Writer.
|
|
|
|
//
|
2015-08-04 04:57:38 +08:00
|
|
|
// The default copy function opens the file with the given filename, and copy
|
|
|
|
// its content to the io.Writer.
|
|
|
|
func SetCopyFunc(f func(io.Writer) error) FileSetting {
|
|
|
|
return func(fi *file) {
|
|
|
|
fi.CopyFunc = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) appendFile(list []*file, name string, settings []FileSetting) []*file {
|
|
|
|
f := &file{
|
|
|
|
Name: filepath.Base(name),
|
2015-07-09 01:53:11 +08:00
|
|
|
Header: make(map[string][]string),
|
2015-08-04 04:57:38 +08:00
|
|
|
CopyFunc: func(w io.Writer) error {
|
|
|
|
h, err := os.Open(name)
|
2015-07-09 01:53:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(w, h); err != nil {
|
|
|
|
h.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return h.Close()
|
|
|
|
},
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-08-04 04:57:38 +08:00
|
|
|
for _, s := range settings {
|
|
|
|
s(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
if list == nil {
|
|
|
|
return []*file{f}
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(list, f)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attach attaches the files to the email.
|
2015-08-04 04:57:38 +08:00
|
|
|
func (m *Message) Attach(filename string, settings ...FileSetting) {
|
|
|
|
m.attachments = m.appendFile(m.attachments, filename, settings)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Embed embeds the images to the email.
|
2015-08-04 04:57:38 +08:00
|
|
|
func (m *Message) Embed(filename string, settings ...FileSetting) {
|
|
|
|
m.embedded = m.appendFile(m.embedded, filename, settings)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|