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 certificate.
(addr string, a smtp.Auth, t *tls.Config)
| 543 | // The TLS Config is helpful if you need to connect to a host that is used an untrusted |
| 544 | // certificate. |
| 545 | func (e *Email) SendWithTLS(addr string, a smtp.Auth, t *tls.Config) error { |
| 546 | // Merge the To, Cc, and Bcc fields |
| 547 | to := make([]string, 0, len(e.To)+len(e.Cc)+len(e.Bcc)) |
| 548 | to = append(append(append(to, e.To...), e.Cc...), e.Bcc...) |
| 549 | for i := 0; i < len(to); i++ { |
| 550 | addr, err := mail.ParseAddress(to[i]) |
| 551 | if err != nil { |
| 552 | return err |
| 553 | } |
| 554 | to[i] = addr.Address |
| 555 | } |
| 556 | // Check to make sure there is at least one recipient and one "From" address |
| 557 | if e.From == "" || len(to) == 0 { |
| 558 | return errors.New("Must specify at least one From address and one To address") |
| 559 | } |
| 560 | sender, err := e.parseSender() |
| 561 | if err != nil { |
| 562 | return err |
| 563 | } |
| 564 | raw, err := e.Bytes() |
| 565 | if err != nil { |
| 566 | return err |
| 567 | } |
| 568 | |
| 569 | conn, err := tls.Dial("tcp", addr, t) |
| 570 | if err != nil { |
| 571 | return err |
| 572 | } |
| 573 | |
| 574 | c, err := smtp.NewClient(conn, t.ServerName) |
| 575 | if err != nil { |
| 576 | return err |
| 577 | } |
| 578 | defer c.Close() |
| 579 | if err = c.Hello("localhost"); err != nil { |
| 580 | return err |
| 581 | } |
| 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) |
nothing calls this directly
no test coverage detected