Documentation ¶
Overview ¶
Package gomail provides a simple interface to send emails.
More info on Github: https://github.com/go-gomail/gomail
Index ¶
- func LoginAuth(username, password, host string) smtp.Auth
- type Encoding
- type File
- type Mailer
- type MailerSetting
- type Message
- func (msg *Message) AddAlternative(contentType, body string)
- func (msg *Message) Attach(f ...*File)
- func (msg *Message) DelHeader(field string)
- func (msg *Message) Embed(image ...*File)
- func (msg *Message) Export() *mail.Message
- func (msg *Message) FormatAddress(address, name string) string
- func (msg *Message) FormatDate(date time.Time) string
- func (msg *Message) GetBodyWriter(contentType string) io.Writer
- func (msg *Message) GetHeader(field string) []string
- func (msg *Message) SetAddressHeader(field, address, name string)
- func (msg *Message) SetBody(contentType, body string)
- func (msg *Message) SetDateHeader(field string, date time.Time)
- func (msg *Message) SetHeader(field string, value ...string)
- func (msg *Message) SetHeaders(h map[string][]string)
- type MessageSetting
- type SendMailFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
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 File ¶
A File represents a file that can be attached or embedded in an email.
func CreateFile ¶
CreateFile creates a gomail.File from the given name and content.
type Mailer ¶
A Mailer represents an SMTP server.
func NewCustomMailer ¶
func NewCustomMailer(addr string, auth smtp.Auth, settings ...MailerSetting) *Mailer
NewCustomMailer creates a mailer with the given authentication mechanism.
Example:
gomail.NewCustomMailer("host:587", smtp.CRAMMD5Auth("username", "secret"))
type MailerSetting ¶
type MailerSetting func(m *Mailer)
A MailerSetting can be used in a mailer constructor to configure it.
func SetLocalHost ¶
func SetLocalHost(h string) MailerSetting
SetLocalHost allows HELO msg to be send with customized string.
func SetSendMail ¶
func SetSendMail(s SendMailFunc) MailerSetting
SetSendMail allows to set the email-sending function of a mailer.
Example:
myFunc := func(addr string, a smtp.Auth, from string, to []string, msg []byte) error { // Implement your email-sending function similar to smtp.SendMail } mailer := gomail.NewMailer("host", "user", "pwd", 465, SetSendMail(myFunc))
func SetTLSConfig ¶
func SetTLSConfig(c *tls.Config) MailerSetting
SetTLSConfig allows to set the TLS configuration used to connect the SMTP server.
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 body to the message. Commonly used to send HTML emails that default to the plain text version for backward compatibility.
Example:
msg.SetBody("text/plain", "Hello!") msg.AddAlternative("text/html", "<p>Hello!</p>")
func (*Message) Embed ¶
Embed embeds the images to the email.
Example:
f, err := gomail.OpenFile("/tmp/image.jpg") if err != nil { panic(err) } msg.Embed(f) msg.SetBody("text/html", `<img src="cid:image.jpg" alt="My image" />`)
func (*Message) FormatAddress ¶
FormatAddress formats an address and a name as a valid RFC 5322 address.
func (*Message) FormatDate ¶
FormatDate formats a date as a valid RFC 5322 date.
func (*Message) GetBodyWriter ¶
GetBodyWriter gets a writer that writes to the body. It can be useful with the templates from packages text/template or html/template.
Example:
w := msg.GetBodyWriter("text/plain") t := template.Must(template.New("example").Parse("Hello {{.}}!")) t.Execute(w, "Bob")
func (*Message) SetAddressHeader ¶
SetAddressHeader sets an address to the given header field.
func (*Message) SetDateHeader ¶
SetDateHeader sets a date to the given header field.
func (*Message) SetHeaders ¶
SetHeaders sets the message headers.
Example:
msg.SetHeaders(map[string][]string{ "From": {"alex@example.com"}, "To": {"bob@example.com", "cora@example.com"}, "Subject": {"Hello"}, })
type MessageSetting ¶
type MessageSetting func(msg *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:
msg := gomail.NewMessage(SetCharset("ISO-8859-1"))
func SetEncoding ¶
func SetEncoding(enc Encoding) MessageSetting
SetEncoding is a message setting to set the encoding of the email.
Example:
msg := gomail.NewMessage(SetEncoding(gomail.Base64))