mail.go 897 B

1234567891011121314151617181920212223242526272829303132333435
  1. package dao
  2. import (
  3. "strings"
  4. "context"
  5. "gopkg.in/gomail.v2"
  6. )
  7. const (
  8. _MailBoxNotFound = "Mailbox not found"
  9. )
  10. // SendMail asynchronous send mail.
  11. func (d *Dao) SendMail(message *gomail.Message) {
  12. message.SetAddressHeader("From", d.email.Username, "merlin")
  13. d.cache.Do(context.Background(), func(c context.Context) {
  14. d.SendMailIfFailed(message)
  15. })
  16. }
  17. // SendMailIfFailed Send Mail If Failed
  18. func (d *Dao) SendMailIfFailed(message *gomail.Message) {
  19. if err := d.email.DialAndSend(message); err != nil {
  20. if strings.Contains(err.Error(), _MailBoxNotFound) {
  21. headerMsg := message.GetHeader("Subject")
  22. headerMsg = append(headerMsg, "Mail Send Error:"+err.Error()+",Receiver:")
  23. headerMsg = append(headerMsg, message.GetHeader("To")...)
  24. message.SetHeader("To", d.c.Mail.NoticeOwner...)
  25. message.SetHeader("Subject", headerMsg...)
  26. d.email.DialAndSend(message)
  27. }
  28. }
  29. }