Send an email using the given host and SMTP auth (optional), returns any error thrown by smtp.SendMail This function merges the To, Cc, and Bcc fields and calls the smtp.SendMail function using the Email.Bytes() output as the message
(addr string, a smtp.Auth)
| 496 | // Send an email using the given host and SMTP auth (optional), returns any error thrown by smtp.SendMail |
| 497 | // This function merges the To, Cc, and Bcc fields and calls the smtp.SendMail function using the Email.Bytes() output as the message |
| 498 | func (e *Email) Send(addr string, a smtp.Auth) error { |
| 499 | // Merge the To, Cc, and Bcc fields |
| 500 | to := make([]string, 0, len(e.To)+len(e.Cc)+len(e.Bcc)) |
| 501 | to = append(append(append(to, e.To...), e.Cc...), e.Bcc...) |
| 502 | for i := 0; i < len(to); i++ { |
| 503 | addr, err := mail.ParseAddress(to[i]) |
| 504 | if err != nil { |
| 505 | return err |
| 506 | } |
| 507 | to[i] = addr.Address |
| 508 | } |
| 509 | // Check to make sure there is at least one recipient and one "From" address |
| 510 | if e.From == "" || len(to) == 0 { |
| 511 | return errors.New("Must specify at least one From address and one To address") |
| 512 | } |
| 513 | sender, err := e.parseSender() |
| 514 | if err != nil { |
| 515 | return err |
| 516 | } |
| 517 | raw, err := e.Bytes() |
| 518 | if err != nil { |
| 519 | return err |
| 520 | } |
| 521 | return smtp.SendMail(addr, a, sender, to, raw) |
| 522 | } |
| 523 | |
| 524 | // Select and parse an SMTP envelope sender address. Choose Email.Sender if set, or fallback to Email.From. |
| 525 | func (e *Email) parseSender() (string, error) { |