2014-10-22 23:47:24 +08:00
|
|
|
package gomail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"io"
|
2014-10-22 23:55:36 +08:00
|
|
|
"net"
|
2014-10-22 23:47:24 +08:00
|
|
|
"net/smtp"
|
|
|
|
)
|
|
|
|
|
2014-10-22 23:55:36 +08:00
|
|
|
func (m *Mailer) getSendMailFunc(ssl bool) SendMailFunc {
|
2014-10-22 23:47:24 +08:00
|
|
|
return func(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
|
2014-10-22 23:55:36 +08:00
|
|
|
var c smtpClient
|
|
|
|
var err error
|
|
|
|
if ssl {
|
|
|
|
c, err = sslDial(addr, m.host, m.config)
|
|
|
|
} else {
|
|
|
|
c, err = starttlsDial(addr, m.config)
|
|
|
|
}
|
2014-10-22 23:47:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
if a != nil {
|
|
|
|
if ok, _ := c.Extension("AUTH"); ok {
|
|
|
|
if err = c.Auth(a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.Mail(from); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range to {
|
|
|
|
if err = c.Rcpt(addr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := c.Data()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = w.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Quit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 23:55:36 +08:00
|
|
|
func sslDial(addr, host string, config *tls.Config) (smtpClient, error) {
|
|
|
|
conn, err := initTLS("tcp", addr, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newClient(conn, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
func starttlsDial(addr string, config *tls.Config) (smtpClient, error) {
|
|
|
|
c, err := initSMTP(addr)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok, _ := c.Extension("STARTTLS"); ok {
|
|
|
|
return c, c.StartTLS(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2014-10-22 23:47:24 +08:00
|
|
|
var initSMTP = func(addr string) (smtpClient, error) {
|
|
|
|
return smtp.Dial(addr)
|
|
|
|
}
|
|
|
|
|
2014-10-22 23:55:36 +08:00
|
|
|
var initTLS = func(network, addr string, config *tls.Config) (*tls.Conn, error) {
|
|
|
|
return tls.Dial(network, addr, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
var newClient = func(conn net.Conn, host string) (smtpClient, error) {
|
|
|
|
return smtp.NewClient(conn, host)
|
|
|
|
}
|
|
|
|
|
2014-10-22 23:47:24 +08:00
|
|
|
type smtpClient interface {
|
|
|
|
Extension(string) (bool, string)
|
|
|
|
StartTLS(*tls.Config) error
|
|
|
|
Auth(smtp.Auth) error
|
|
|
|
Mail(string) error
|
|
|
|
Rcpt(string) error
|
|
|
|
Data() (io.WriteCloser, error)
|
|
|
|
Quit() error
|
|
|
|
Close() error
|
|
|
|
}
|