2
0

smtp.go 975 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package email
  2. import (
  3. "context"
  4. "crypto/tls"
  5. gomail "gopkg.in/mail.v2"
  6. )
  7. type SmtpSender struct {
  8. SmtpHost string
  9. SmtpPort int
  10. SenderEmail string
  11. SendUser string
  12. SenderPass string
  13. }
  14. func (s *SmtpSender) SendEmail(ctx context.Context, n Notification, e Mail) error {
  15. m := gomail.NewMessage()
  16. // Set E-Mail sender
  17. m.SetHeader("From", s.SenderEmail)
  18. // Set E-Mail receivers
  19. m.SetHeader("To", n.RecipientMail)
  20. // Set E-Mail subject
  21. m.SetHeader("Subject", e.GetSubject(n))
  22. // Set E-Mail body. You can set plain text or html with text/html
  23. m.SetBody("text/html", e.GetBody(n))
  24. // Settings for SMTP server
  25. d := gomail.NewDialer(s.SmtpHost, s.SmtpPort, s.SendUser, s.SenderPass)
  26. // This is only needed when SSL/TLS certificate is not valid on server.
  27. // In production this should be set to false.
  28. d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
  29. // Now send E-Mail
  30. if err := d.DialAndSend(m); err != nil {
  31. return err
  32. }
  33. return nil
  34. }