email.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package email
  2. import (
  3. "crypto/tls"
  4. gomail "gopkg.in/mail.v2"
  5. "github.com/gravitl/netmaker/servercfg"
  6. )
  7. var (
  8. smtpHost = servercfg.GetSmtpHost()
  9. smtpPort = servercfg.GetSmtpPort()
  10. senderEmail = servercfg.GetSenderEmail()
  11. senderPassword = servercfg.GetSenderEmailPassWord()
  12. )
  13. type Email interface {
  14. GetBody(info Notification) string
  15. GetSubject(info Notification) string
  16. }
  17. // Notification - struct for notification details
  18. type Notification struct {
  19. RecipientMail string
  20. RecipientName string
  21. ProductName string
  22. }
  23. func (n Notification) NewEmailSender(e Email) *gomail.Message {
  24. m := gomail.NewMessage()
  25. // Set E-Mail sender
  26. m.SetHeader("From", senderEmail)
  27. // Set E-Mail receivers
  28. m.SetHeader("To", n.RecipientMail)
  29. // Set E-Mail subject
  30. m.SetHeader("Subject", e.GetSubject(n))
  31. // Set E-Mail body. You can set plain text or html with text/html
  32. m.SetBody("text/html", e.GetBody(n))
  33. return m
  34. }
  35. func Send(m *gomail.Message) error {
  36. // Settings for SMTP server
  37. d := gomail.NewDialer(smtpHost, smtpPort, senderEmail, senderPassword)
  38. // This is only needed when SSL/TLS certificate is not valid on server.
  39. // In production this should be set to false.
  40. d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
  41. // Now send E-Mail
  42. if err := d.DialAndSend(m); err != nil {
  43. return err
  44. }
  45. return nil
  46. }