netmaker/pro/email/email.go
Abhishek K 309e4795a1
NET-1950: Persist Server Settings in the DB (#3419)
* feat: api access tokens

* revoke all user tokens

* redefine access token api routes, add auto egress option to enrollment keys

* add server settings apis, add db table for settigs

* handle server settings updates

* switch to using settings from DB

* fix sever settings migration

* revet force migration for settings

* fix server settings database write

* fix revoked tokens to be unauthorized

* remove unused functions

* convert access token to sql schema

* switch access token to sql schema

* fix merge conflicts

* fix server settings types

* bypass basic auth setting for super admin

* add TODO comment

* publish peer update on settings update

* chore(go): import style changes from migration branch;

1. Singular file names for table schema.
2. No table name method.
3. Use .Model instead of .Table.
4. No unnecessary tagging.

* remove nat check on egress gateway request

* Revert "remove nat check on egress gateway request"

This reverts commit 0aff12a189.

* feat(go): add db middleware;

* feat(go): restore method;

* feat(go): add user access token schema;

* fix user auth api:

* re initalise oauth and email config

* set verbosity

* sync auto update settings with hosts

* sync auto update settings with hosts

* mask secret and convert jwt duration to minutes

* convert jwt duration to minutes

* notify peers after settings update

* compare with curr settings before updating

* send host update to devices on auto update

---------

Co-authored-by: Vishal Dalwadi <dalwadivishal26@gmail.com>
2025-04-30 02:34:10 +04:00

60 lines
1.3 KiB
Go

package email
import (
"context"
"regexp"
"github.com/gravitl/netmaker/logic"
)
type EmailSenderType string
var client EmailSender
const (
Smtp EmailSenderType = "smtp"
Resend EmailSenderType = "resend"
)
func Init() {
smtpSender := &SmtpSender{
SmtpHost: logic.GetSmtpHost(),
SmtpPort: logic.GetSmtpPort(),
SenderEmail: logic.GetSenderEmail(),
SendUser: logic.GetSenderUser(),
SenderPass: logic.GetEmaiSenderPassword(),
}
if smtpSender.SendUser == "" {
smtpSender.SendUser = smtpSender.SenderEmail
}
client = smtpSender
}
// EmailSender - an interface for sending emails based on notifications and mail templates
type EmailSender interface {
// SendEmail - sends an email based on a context, notification and mail template
SendEmail(ctx context.Context, notification Notification, email Mail) error
}
type Mail interface {
GetBody(info Notification) string
GetSubject(info Notification) string
}
// Notification - struct for notification details
type Notification struct {
RecipientMail string
RecipientName string
ProductName string
}
func GetClient() (e EmailSender) {
return client
}
func IsValid(email string) bool {
emailRegex := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$`)
return emailRegex.MatchString(email)
}