Skip to content

Commit 8694dac

Browse files
authored
Split SendWithTLS from SendWithStartTLS(#87)
A bug was introduced in 27742b0 where, in the process of trying to add TLS support, broke the way the connection is set up since it still tried to use `STARTTLS` under the hood. Fixes #71.
1 parent 93c0018 commit 8694dac

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

email.go

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,9 @@ func (e *Email) parseSender() (string, error) {
538538
}
539539
}
540540

541-
// SendWithTLS sends an email with an optional TLS config.
542-
// This is helpful if you need to connect to a host that is used an untrusted
541+
// SendWithTLS sends an email over tls with an optional TLS config.
542+
//
543+
// The TLS Config is helpful if you need to connect to a host that is used an untrusted
543544
// certificate.
544545
func (e *Email) SendWithTLS(addr string, a smtp.Auth, t *tls.Config) error {
545546
// Merge the To, Cc, and Bcc fields
@@ -578,6 +579,75 @@ func (e *Email) SendWithTLS(addr string, a smtp.Auth, t *tls.Config) error {
578579
if err = c.Hello("localhost"); err != nil {
579580
return err
580581
}
582+
583+
if a != nil {
584+
if ok, _ := c.Extension("AUTH"); ok {
585+
if err = c.Auth(a); err != nil {
586+
return err
587+
}
588+
}
589+
}
590+
if err = c.Mail(sender); err != nil {
591+
return err
592+
}
593+
for _, addr := range to {
594+
if err = c.Rcpt(addr); err != nil {
595+
return err
596+
}
597+
}
598+
w, err := c.Data()
599+
if err != nil {
600+
return err
601+
}
602+
_, err = w.Write(raw)
603+
if err != nil {
604+
return err
605+
}
606+
err = w.Close()
607+
if err != nil {
608+
return err
609+
}
610+
return c.Quit()
611+
}
612+
613+
// SendWithStartTLS sends an email over TLS using STARTTLS with an optional TLS config.
614+
//
615+
// The TLS Config is helpful if you need to connect to a host that is used an untrusted
616+
// certificate.
617+
func (e *Email) SendWithStartTLS(addr string, a smtp.Auth, t *tls.Config) error {
618+
// Merge the To, Cc, and Bcc fields
619+
to := make([]string, 0, len(e.To)+len(e.Cc)+len(e.Bcc))
620+
to = append(append(append(to, e.To...), e.Cc...), e.Bcc...)
621+
for i := 0; i < len(to); i++ {
622+
addr, err := mail.ParseAddress(to[i])
623+
if err != nil {
624+
return err
625+
}
626+
to[i] = addr.Address
627+
}
628+
// Check to make sure there is at least one recipient and one "From" address
629+
if e.From == "" || len(to) == 0 {
630+
return errors.New("Must specify at least one From address and one To address")
631+
}
632+
sender, err := e.parseSender()
633+
if err != nil {
634+
return err
635+
}
636+
raw, err := e.Bytes()
637+
if err != nil {
638+
return err
639+
}
640+
641+
// Taken from the standard library
642+
// https://github.com/golang/go/blob/master/src/net/smtp/smtp.go#L328
643+
c, err := smtp.Dial(addr)
644+
if err != nil {
645+
return err
646+
}
647+
defer c.Close()
648+
if err = c.Hello("localhost"); err != nil {
649+
return err
650+
}
581651
// Use TLS if available
582652
if ok, _ := c.Extension("STARTTLS"); ok {
583653
if err = c.StartTLS(t); err != nil {

0 commit comments

Comments
 (0)