smtp.go 958 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. SenderPass string
  12. }
  13. func (s *SmtpSender) SendEmail(ctx context.Context, n Notification, e Mail) error {
  14. m := gomail.NewMessage()
  15. // Set E-Mail sender
  16. m.SetHeader("From", s.SenderEmail)
  17. // Set E-Mail receivers
  18. m.SetHeader("To", n.RecipientMail)
  19. // Set E-Mail subject
  20. m.SetHeader("Subject", e.GetSubject(n))
  21. // Set E-Mail body. You can set plain text or html with text/html
  22. m.SetBody("text/html", e.GetBody(n))
  23. // Settings for SMTP server
  24. d := gomail.NewDialer(s.SmtpHost, s.SmtpPort, s.SenderEmail, s.SenderPass)
  25. // This is only needed when SSL/TLS certificate is not valid on server.
  26. // In production this should be set to false.
  27. d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
  28. // Now send E-Mail
  29. if err := d.DialAndSend(m); err != nil {
  30. return err
  31. }
  32. return nil
  33. }