email.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package email
  2. import (
  3. "context"
  4. "regexp"
  5. "github.com/gravitl/netmaker/logic"
  6. )
  7. type EmailSenderType string
  8. var client EmailSender
  9. const (
  10. Smtp EmailSenderType = "smtp"
  11. Resend EmailSenderType = "resend"
  12. )
  13. func Init() {
  14. smtpSender := &SmtpSender{
  15. SmtpHost: logic.GetSmtpHost(),
  16. SmtpPort: logic.GetSmtpPort(),
  17. SenderEmail: logic.GetSenderEmail(),
  18. SendUser: logic.GetSenderUser(),
  19. SenderPass: logic.GetEmaiSenderPassword(),
  20. }
  21. if smtpSender.SendUser == "" {
  22. smtpSender.SendUser = smtpSender.SenderEmail
  23. }
  24. client = smtpSender
  25. }
  26. // EmailSender - an interface for sending emails based on notifications and mail templates
  27. type EmailSender interface {
  28. // SendEmail - sends an email based on a context, notification and mail template
  29. SendEmail(ctx context.Context, notification Notification, email Mail) error
  30. }
  31. type Mail interface {
  32. GetBody(info Notification) string
  33. GetSubject(info Notification) string
  34. }
  35. // Notification - struct for notification details
  36. type Notification struct {
  37. RecipientMail string
  38. RecipientName string
  39. ProductName string
  40. }
  41. func GetClient() (e EmailSender) {
  42. return client
  43. }
  44. func IsValid(email string) bool {
  45. emailRegex := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$`)
  46. return emailRegex.MatchString(email)
  47. }