mail

package
v2.3.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2019 License: AGPL-3.0, MIT Imports: 16 Imported by: 0

README

Gomail

Build Status Code Coverage Documentation

This is an actively maintained fork of Gomail and includes fixes and improvements for a number of outstanding issues. The current progress is as follows:

  • Timeouts and retries can be specified outside of the 10 second default.
  • Proxying is supported through specifying a custom NetDialTimeout.
  • Filenames are properly encoded for non-ASCII characters.
  • Email addresses are properly encoded for non-ASCII characters.
  • Embedded files and attachments are tested for their existence.
  • An io.Reader can be supplied when embedding and attaching files.

See Transitioning Existing Codebases for more information on switching.

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 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/github.com/go-mail/mail

Download

If you're already using a dependency manager, like dep, use the following import path:

github.com/go-mail/mail

If you aren't using vendoring, go get the Gopkg.in import path:

gopkg.in/mail.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:

package main

import (
	"crypto/tls"

	"gopkg.in/mail.v2"
)

func main() {
	d := mail.NewDialer("smtp.example.com", 587, "user", "123456")
	d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

	// Send emails using d.
}

Note, however, that this is insecure and should not be used in production.

Transitioning Existing Codebases

If you're already using the original Gomail, switching is as easy as updating the import line to:

import gomail "gopkg.in/mail.v2"

Contribute

Contributions are more than welcome! See CONTRIBUTING.md for more info.

Change log

See CHANGELOG.md.

License

MIT

Support & Contact

You can ask questions on the Gomail thread in the Go mailing-list.

Documentation

Overview

Package gomail provides a simple interface to compose emails and to mail them efficiently.

More info on Github: https://github.com/go-mail/mail

Index

Constants

This section is empty.

Variables

View Source
var NetDialTimeout = net.DialTimeout

NetDialTimeout specifies the DialTimeout function to establish a connection to the SMTP server. This can be used to override dialing in the case that a proxy or other special behavior is needed.

Functions

func Send

func Send(s Sender, msg ...*Message) error

Send sends emails using the given Sender.

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
	// Username is the username to use to authenticate to the SMTP server.
	Username string
	// Password is the password to use to authenticate to the SMTP server.
	Password string
	// 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
	// TLSConfig represents the TLS configuration used for the TLS (when the
	// STARTTLS extension is used) or SSL connection.
	TLSConfig *tls.Config
	// StartTLSPolicy represents the TLS security level required to
	// communicate with the SMTP server.
	//
	// This defaults to OpportunisticStartTLS for backwards compatibility,
	// but we recommend MandatoryStartTLS for all modern SMTP servers.
	//
	// This option has no effect if SSL is set to true.
	StartTLSPolicy StartTLSPolicy
	// LocalName is the hostname sent to the SMTP server with the HELO command.
	// By default, "localhost" is sent.
	LocalName string
	// Timeout to use for read/write operations. Defaults to 10 seconds, can
	// be set to 0 to disable timeouts.
	Timeout time.Duration
	// Whether we should retry mailing if the connection returned an error,
	// defaults to true.
	RetryFailure bool
}

A Dialer is a dialer to an SMTP server.

func NewDialer

func NewDialer(host string, port int, username, password string) *Dialer

NewDialer returns a new SMTP Dialer. The given parameters are used to connect to the SMTP server.

func NewPlainDialer deprecated

func NewPlainDialer(host string, port int, username, password string) *Dialer

NewPlainDialer returns a new SMTP Dialer. The given parameters are used to connect to the SMTP server.

Deprecated: Use NewDialer instead.

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

func (d *Dialer) DialAndSend(m ...*Message) error

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 Rename

func Rename(name string) FileSetting

Rename is a file setting to set the name of the attachment if the name is different than the filename on disk.

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.

func SetHeader

func SetHeader(h map[string][]string) FileSetting

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.

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

func (m *Message) AddAlternative(contentType, body string, settings ...PartSetting)

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. 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

func (*Message) AddAlternativeWriter

func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error, settings ...PartSetting)

AddAlternativeWriter adds an alternative part to the message. It can be useful with the text/template or html/template packages.

func (*Message) Attach

func (m *Message) Attach(filename string, settings ...FileSetting)

Attach attaches the files to the email.

func (*Message) Embed

func (m *Message) Embed(filename string, settings ...FileSetting)

Embed embeds the images to the email.

func (*Message) FormatAddress

func (m *Message) FormatAddress(address, name string) string

FormatAddress formats an address and a name as a valid RFC 5322 address.

func (*Message) FormatDate

func (m *Message) FormatDate(date time.Time) string

FormatDate formats a date as a valid RFC 5322 date.

func (*Message) GetHeader

func (m *Message) GetHeader(field string) []string

GetHeader gets a header field.

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

func (m *Message) SetAddressHeader(field, address, name string)

SetAddressHeader sets an address to the given header field.

func (*Message) SetBody

func (m *Message) SetBody(contentType, body string, settings ...PartSetting)

SetBody sets the body of the message. It replaces any content previously set by SetBody, SetBodyWriter, AddAlternative or AddAlternativeWriter.

func (*Message) SetBodyWriter

func (m *Message) SetBodyWriter(contentType string, f func(io.Writer) error, settings ...PartSetting)

SetBodyWriter sets the body of the message. It can be useful with the text/template or html/template packages.

func (*Message) SetBoundary

func (m *Message) SetBoundary(boundary string)

SetBoundary sets a custom multipart boundary.

func (*Message) SetDateHeader

func (m *Message) SetDateHeader(field string, date time.Time)

SetDateHeader sets a date to the given header field.

func (*Message) SetHeader

func (m *Message) SetHeader(field string, value ...string)

SetHeader sets a value to the given header field.

func (*Message) SetHeaders

func (m *Message) SetHeaders(h map[string][]string)

SetHeaders sets the message headers.

func (*Message) WriteTo

func (m *Message) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo. It dumps the whole message into w.

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.

func SetEncoding

func SetEncoding(enc Encoding) MessageSetting

SetEncoding is a message setting to set the encoding of the email.

type PartSetting

type PartSetting func(*part)

A PartSetting can be used as an argument in Message.SetBody, Message.SetBodyWriter, Message.AddAlternative or Message.AddAlternativeWriter to configure the part added to a message.

func SetPartEncoding

func SetPartEncoding(e Encoding) PartSetting

SetPartEncoding sets the encoding of the part added to the message. By default, parts use the same encoding than the message.

type SendCloser

type SendCloser interface {
	Sender
	Close() error
}

SendCloser is the interface that groups the Send and Close methods.

type SendFunc

type SendFunc func(from string, to []string, msg io.WriterTo) error

A SendFunc is a function that sends emails to the given addresses.

The SendFunc type is an adapter to allow the use of ordinary functions as email senders. If f is a function with the appropriate signature, SendFunc(f) is a Sender object that calls f.

func (SendFunc) Send

func (f SendFunc) Send(from string, to []string, msg io.WriterTo) error

Send calls f(from, to, msg).

type Sender

type Sender interface {
	Send(from string, to []string, msg io.WriterTo) error
}

Sender is the interface that wraps the Send method.

Send sends an email to the given addresses.

type StartTLSPolicy

type StartTLSPolicy int

StartTLSPolicy constants are valid values for Dialer.StartTLSPolicy.

const (
	// OpportunisticStartTLS means that SMTP transactions are encrypted if
	// STARTTLS is supported by the SMTP server. Otherwise, messages are
	// sent in the clear. This is the default setting.
	OpportunisticStartTLS StartTLSPolicy = iota
	// MandatoryStartTLS means that SMTP transactions must be encrypted.
	// SMTP transactions are aborted unless STARTTLS is supported by the
	// SMTP server.
	MandatoryStartTLS
	// NoStartTLS means encryption is disabled and messages are sent in the
	// clear.
	NoStartTLS = -1
)

func (*StartTLSPolicy) String

func (policy *StartTLSPolicy) String() string

type StartTLSUnsupportedError

type StartTLSUnsupportedError struct {
	Policy StartTLSPolicy
}

StartTLSUnsupportedError is returned by Dial when connecting to an SMTP server that does not support STARTTLS.

func (StartTLSUnsupportedError) Error

func (e StartTLSUnsupportedError) Error() string

Jump to

Keyboard shortcuts

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