SendWithStartTLS sends an email over TLS using STARTTLS with an optional TLS config. The TLS Config is helpful if you need to connect to a host that is used an untrusted certificate.
(addr string, a smtp.Auth, t *tls.Config)
| 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 | } |
| 651 | // Use TLS if available |
| 652 | if ok, _ := c.Extension("STARTTLS"); ok { |
| 653 | if err = c.StartTLS(t); err != nil { |
| 654 | return err |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | if a != nil { |
| 659 | if ok, _ := c.Extension("AUTH"); ok { |
| 660 | if err = c.Auth(a); err != nil { |
| 661 | return err |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | if err = c.Mail(sender); err != nil { |
| 666 | return err |
| 667 | } |
| 668 | for _, addr := range to { |
| 669 | if err = c.Rcpt(addr); err != nil { |
| 670 | return err |
| 671 | } |
| 672 | } |
| 673 | w, err := c.Data() |
| 674 | if err != nil { |
nothing calls this directly
no test coverage detected