文档:https://pkg.go.dev/gopkg.in/gomail.v2

Func

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

type Dialer

  1. type Dialer struct {
  2. // Host represents the host of the SMTP server.
  3. Host string
  4. // Port represents the port of the SMTP server.
  5. Port int
  6. // Username is the username to use to authenticate to the SMTP server.
  7. Username string
  8. // Password is the password to use to authenticate to the SMTP server.
  9. Password string
  10. // Auth represents the authentication mechanism used to authenticate to the
  11. // SMTP server.
  12. Auth smtp.Auth
  13. // SSL defines whether an SSL connection is used. It should be false in
  14. // most cases since the authentication mechanism should use the STARTTLS
  15. // extension instead.
  16. SSL bool
  17. // TSLConfig represents the TLS configuration used for the TLS (when the
  18. // STARTTLS extension is used) or SSL connection.
  19. TLSConfig *tls.Config
  20. // LocalName is the hostname sent to the SMTP server with the HELO command.
  21. // By default, "localhost" is sent.
  22. LocalName string
  23. }
  24. func NewDialer(host string, port int, username, password string) *Dialer
  25. func (d *Dialer) Dial() (SendCloser, error)

type FileSetting

  1. // 设置附件名称
  2. func Rename(name string) FileSetting
  3. // 设置附件内容
  4. func SetCopyFunc(f func(io.Writer) error) FileSetting
  5. // 设置包含文件内容的消息部分的MIME头
  6. func SetHeader(h map[string][]string) FileSetting

type Message

  1. func NewMessage(settings ...MessageSetting) *Message
  2. // 增加附件
  3. func (m *Message) Attach(filename string, settings ...FileSetting)
  4. // 将图像嵌入到电子邮件中
  5. func (m *Message) Embed(filename string, settings ...FileSetting)
  6. m.Embed("/tmp/image.jpg")
  7. m.SetBody("text/html", `<img src="cid:image.jpg" alt="My image" />`)
  8. // 将地址和名称格式化为有效的RFC 5322地址
  9. func (m *Message) FormatAddress(address, name string) string
  10. func (m *Message) SetHeader(field string, value ...string)
  11. func (m *Message) SetHeaders(h map[string][]string)
  12. m.SetHeaders(map[string][]string{
  13. "From": {m.FormatAddress("alex@example.com", "Alex")},
  14. "To": {"bob@example.com", "cora@example.com"},
  15. "Subject": {"Hello"},
  16. })
  17. // 设置消息的主体。
  18. // 它替换任何先前设置的SetBody, AddAlternative或AddAlternativeWriter的内容
  19. func (m *Message) SetBody(contentType, body string, settings ...PartSetting)
  20. func (m *Message) Reset() // 情况设置,保持与NewMessage一样的状态

Demo

  1. package main
  2. import (
  3. "gopkg.in/gomail.v2"
  4. "log"
  5. )
  6. func main() {
  7. d := gomail.NewDialer("smtp.qq.com", 587, "xxx@qq.com", "xxx")
  8. s, err := d.Dial()
  9. if err != nil {
  10. panic(err)
  11. }
  12. m := gomail.NewMessage()
  13. m.SetHeader("From", "xxx@qq.com")
  14. m.SetHeader("To", "yyy@qq.com", "macho")
  15. m.SetHeader("Subject", "Newsletter #1")
  16. m.SetBody("text/html", "happy every day!")
  17. m.Attach("./upaiyun.gif")
  18. if err := gomail.Send(s, m); err != nil {
  19. log.Printf("Could not send email: %v", err)
  20. }
  21. }