package email import ( "context" "fmt" "os" "time" mailgun "github.com/mailgun/mailgun-go/v4" log "github.com/sirupsen/logrus" "github.com/authorizerdev/authorizer/server/constants" ) const apiURL = "https://api.mailgun.net/v3/%s/messages" func MailgunRest(to string, data map[string]interface{}, subject string, template string) error { var mailgunAPIKey = os.Getenv("MAILGUN_API_KEY") var mailgunDomain = os.Getenv("MAILGUN_DOMAIN") sender := mailgunDomain + "" log.Printf("%r", data) mg := mailgun.NewMailgun(mailgunDomain, mailgunAPIKey) m := mg.NewMessage(sender, subject, "", to) m.SetTemplate(template) m.AddTemplateVariable("verification_url", data["verification_url"]) userMap, ok := data["user"].(map[string]interface{}) if !ok { log.Println("Error: Unable to retrieve user information from the data map.") } userName, ok := userMap["GivenName"].(string) if ok { m.AddTemplateVariable("username", userName) } ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() resp, id, err := mg.Send(ctx, m) if err != nil { log.Fatal(err) } fmt.Printf("ID: %s Resp: %s\n", id, resp) return err } // SendMailgun function to send func SendMailgun(to []string, event string, data map[string]interface{}) error { template := "authorizer_email_confirmation" switch event { case constants.VerificationTypeBasicAuthSignup: template = "authorizer_email_confirmation" case constants.VerificationTypeForgotPassword: template = "authorizer_password_reset" case constants.VerificationTypeInviteMember: template = "author_invited" case constants.VerificationTypeMagicLinkLogin: template = "magic_link_login" case constants.VerificationTypeOTP: template = "one_time_password" case constants.VerificationTypeUpdateEmail: template = "email_update" } subject := "Подтверждение почты" // TODO: language selection logic here err := MailgunRest(to[0], data, subject, template) // Log the response if err != nil { log.Printf("Error sending email: %v", err) } else { log.Println("Email sent successfully") } return err }