Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update from the standard library #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 42 additions & 13 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"mime"
"mime/multipart"
"mime/quotedprintable"
"net"
"net/mail"
"net/smtp"
"net/textproto"
Expand Down Expand Up @@ -51,6 +52,7 @@ type Email struct {
Headers textproto.MIMEHeader
Attachments []*Attachment
ReadReceipt []string
timeout time.Duration
}

// part is a copyable representation of a multipart.Part
Expand Down Expand Up @@ -177,7 +179,7 @@ func NewEmailFromReader(r io.Reader) (*Email, error) {
return e, err
}
filename, filenameDefined := params["filename"]
if cd == "attachment" || (cd == "inline" && filenameDefined){
if cd == "attachment" || (cd == "inline" && filenameDefined) {
_, err = e.Attach(bytes.NewReader(p.body), filename, ct)
if err != nil {
return e, err
Expand Down Expand Up @@ -555,6 +557,13 @@ func (e *Email) parseSender() (string, error) {
}
}

// Timeout sets a timeout to use in custom SendWith* functions.
// Further calls to SendWith* functions reset this value.
func (e *Email) Timeout(t time.Duration) *Email {
e.timeout = t
return e
}

// SendWithTLS sends an email over tls with an optional TLS config.
//
// The TLS Config is helpful if you need to connect to a host that is used an untrusted
Expand Down Expand Up @@ -583,7 +592,16 @@ func (e *Email) SendWithTLS(addr string, a smtp.Auth, t *tls.Config) error {
return err
}

conn, err := tls.Dial("tcp", addr, t)
var conn *tls.Conn
if e.timeout == 0 {
conn, err = tls.Dial("tcp", addr, t)
} else {
dialer := net.Dialer{
Timeout: e.timeout,
}
e.timeout = 0
conn, err = tls.DialWithDialer(&dialer, "tcp", addr, t)
}
if err != nil {
return err
}
Expand All @@ -598,10 +616,11 @@ func (e *Email) SendWithTLS(addr string, a smtp.Auth, t *tls.Config) error {
}

if a != nil {
if ok, _ := c.Extension("AUTH"); ok {
if err = c.Auth(a); err != nil {
return err
}
if ok, _ := c.Extension("AUTH"); !ok {
return errors.New("smtp: server doesn't support AUTH")
}
if err = c.Auth(a); err != nil {
return err
}
}
if err = c.Mail(sender); err != nil {
Expand Down Expand Up @@ -657,26 +676,36 @@ func (e *Email) SendWithStartTLS(addr string, a smtp.Auth, t *tls.Config) error

// Taken from the standard library
// https://github.com/golang/go/blob/master/src/net/smtp/smtp.go#L328
c, err := smtp.Dial(addr)
var c *smtp.Client
if e.timeout == 0 {
c, err = smtp.Dial(addr)
} else {
conn, err := net.DialTimeout("tcp", addr, e.timeout)
e.timeout = 0
if err != nil {
return err
}
host, _, _ := net.SplitHostPort(addr)
c, err = smtp.NewClient(conn, host)
}
if err != nil {
return err
}
defer c.Close()
if err = c.Hello("localhost"); err != nil {
return err
}
// Use TLS if available
if ok, _ := c.Extension("STARTTLS"); ok {
if err = c.StartTLS(t); err != nil {
return err
}
}

if a != nil {
if ok, _ := c.Extension("AUTH"); ok {
if err = c.Auth(a); err != nil {
return err
}
if ok, _ := c.Extension("AUTH"); !ok {
return errors.New("smtp: server doesn't support AUTH")
}
if err = c.Auth(a); err != nil {
return err
}
}
if err = c.Mail(sender); err != nil {
Expand Down