README
¶
Gomail
Introduction
Gomail is a simple and efficient package to send emails. It is well tested and documented.
Gomail can only send emails using an SMTP server. But the API is flexible and it is easy to implement other methods for sending emails using a local Postfix, an API, etc.
It is versioned using gopkg.in so I promise they will never be backward incompatible changes within each version.
It requires Go 1.2 or newer. With Go 1.5, no external dependencies are used.
Features
Gomail supports:
- Attachments
- Embedded images
- HTML and text templates
- Automatic encoding of special characters
- SSL and TLS
- Sending multiple emails with the same SMTP connection
Documentation
https://godoc.org/gopkg.in/gomail.v2
Download
go get gopkg.in/gomail.v2
Examples
See the examples in the documentation.
FAQ
x509: certificate signed by unknown authority
If you get this error it means the certificate used by the SMTP server is not
considered valid by the client running Gomail. As a quick workaround you can
bypass the verification of the server's certificate chain and host name by using
SetTLSConfig
:
d := gomail.NewPlainDialer("smtp.example.com", "user", "123456", 587)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
Note, however, that this is insecure and should not be used in production.
Contribute
Contributions are more than welcome! See CONTRIBUTING.md for more info.
Change log
See CHANGELOG.md.
License
Contact
You can ask questions on the Gomail thread in the Go mailing-list.
Support
If you want to support the development of Gomail, I gladly accept donations.
I will give 100% of the money I receive to Enfants, Espoir Du Monde. EEDM is a French NGO which helps children in Bangladesh, Cameroun, Haiti, India and Madagascar.
All its members are volunteers so its operating costs are only 1.9%. So your money will directly help children of these countries.
As an added bonus, your donations will also tip me by lowering my taxes 😄
I will send an email with the receipt of the donation to EEDM annually to all donors.
Documentation
¶
Overview ¶
Package gomail provides a simple interface to compose emails and to mail them efficiently.
More info on Github: https://github.com/go-gomail/gomail
Example ¶
Output:
Example (Daemon) ¶
A daemon that listens to a channel and sends all incoming messages.
Output:
Example (Newsletter) ¶
Efficiently send a customized newsletter to a list of recipients.
Output:
Example (NoAuth) ¶
Send an email using a local SMTP server.
Output:
Example (NoSMTP) ¶
Send an email using an API or postfix.
Output: From: from@example.com To: [to@example.com]
Index ¶
- func Send(s Sender, msg ...*Message) error
- type Dialer
- type Encoding
- type FileSetting
- type Message
- func (m *Message) AddAlternative(contentType, body string)
- func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error)
- func (m *Message) Attach(filename string, settings ...FileSetting)
- func (m *Message) Embed(filename string, settings ...FileSetting)
- func (m *Message) FormatAddress(address, name string) string
- func (m *Message) FormatDate(date time.Time) string
- func (m *Message) GetHeader(field string) []string
- func (m *Message) Reset()
- func (m *Message) SetAddressHeader(field, address, name string)
- func (m *Message) SetBody(contentType, body string)
- func (m *Message) SetDateHeader(field string, date time.Time)
- func (m *Message) SetHeader(field string, value ...string)
- func (m *Message) SetHeaders(h map[string][]string)
- func (m *Message) WriteTo(w io.Writer) (int64, error)
- type MessageSetting
- type SendCloser
- type SendFunc
- type Sender
Examples ¶
- Package
- Package (Daemon)
- Package (Newsletter)
- Package (NoAuth)
- Package (NoSMTP)
- Message.AddAlternative
- Message.AddAlternativeWriter
- Message.Attach
- Message.Embed
- Message.FormatAddress
- Message.FormatDate
- Message.SetAddressHeader
- Message.SetBody
- Message.SetDateHeader
- Message.SetHeader
- Message.SetHeaders
- SetCharset
- SetCopyFunc
- SetEncoding
- SetHeader
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Dialer ¶
type Dialer struct { // Host represents the host of the SMTP server. Host string // Port represents the port of the SMTP server. Port int // Auth represents the authentication mechanism used to authenticate to the // SMTP server. Auth smtp.Auth // SSL defines whether an SSL connection is used. It should be false in // most cases since the authentication mechanism should use the STARTTLS // extension instead. SSL bool // TSLConfig represents the TLS configuration used for the TLS (when the // STARTTLS extension is used) or SSL connection. TLSConfig *tls.Config }
A Dialer is a dialer to an SMTP server.
func NewPlainDialer ¶
NewPlainDialer returns a Dialer. The given parameters are used to connect to the SMTP server via a PLAIN authentication mechanism.
It fallbacks to the LOGIN mechanism if it is the only mechanism advertised by the server.
func (*Dialer) Dial ¶
func (d *Dialer) Dial() (SendCloser, error)
Dial dials and authenticates to an SMTP server. The returned SendCloser should be closed when done using it.
func (*Dialer) DialAndSend ¶
DialAndSend opens a connection to the SMTP server, sends the given emails and closes the connection.
type Encoding ¶
type Encoding string
Encoding represents a MIME encoding scheme like quoted-printable or base64.
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" // 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" )
type FileSetting ¶
type FileSetting func(*file)
A FileSetting can be used as an argument in Message.Attach or Message.Embed.
func SetCopyFunc ¶
func SetCopyFunc(f func(io.Writer) error) FileSetting
SetCopyFunc is a file setting to replace the function that runs when the message is sent. It should copy the content of the file to the io.Writer.
The default copy function opens the file with the given filename, and copy its content to the io.Writer.
Example ¶
Output:
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
Message represents an email.
func NewMessage ¶
func NewMessage(settings ...MessageSetting) *Message
NewMessage creates a new message. It uses UTF-8 and quoted-printable encoding by default.
func (*Message) AddAlternative ¶
AddAlternative adds an alternative part to the message.
It is commonly used to send HTML emails that default to the plain text version for backward compatibility.
More info: http://en.wikipedia.org/wiki/MIME#Alternative
Example ¶
Output:
func (*Message) AddAlternativeWriter ¶
AddAlternativeWriter adds an alternative part to the message. It can be useful with the text/template or html/template packages.
Example ¶
Output:
func (*Message) Attach ¶
func (m *Message) Attach(filename string, settings ...FileSetting)
Attach attaches the files to the email.
Example ¶
Output:
func (*Message) Embed ¶
func (m *Message) Embed(filename string, settings ...FileSetting)
Embed embeds the images to the email.
Example ¶
Output:
func (*Message) FormatAddress ¶
FormatAddress formats an address and a name as a valid RFC 5322 address.
Example ¶
Output:
func (*Message) Reset ¶
func (m *Message) Reset()
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.
func (*Message) SetAddressHeader ¶
SetAddressHeader sets an address to the given header field.
Example ¶
Output:
func (*Message) SetDateHeader ¶
SetDateHeader sets a date to the given header field.
Example ¶
Output:
type MessageSetting ¶
type MessageSetting func(m *Message)
A MessageSetting can be used as an argument in NewMessage to configure an email.
func SetCharset ¶
func SetCharset(charset string) MessageSetting
SetCharset is a message setting to set the charset of the email.
Example ¶
Output:
func SetEncoding ¶
func SetEncoding(enc Encoding) MessageSetting
SetEncoding is a message setting to set the encoding of the email.
Example ¶
Output:
type SendCloser ¶
SendCloser is the interface that groups the Send and Close methods.