2024-01-05 10:06:43 +00:00
|
|
|
package email
|
|
|
|
|
|
|
|
import (
|
2024-01-05 16:17:55 +00:00
|
|
|
"context"
|
2024-01-05 16:29:50 +00:00
|
|
|
"fmt"
|
2024-01-05 10:06:43 +00:00
|
|
|
"os"
|
2024-01-05 16:17:55 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
mailgun "github.com/mailgun/mailgun-go/v4"
|
2024-01-05 10:06:43 +00:00
|
|
|
|
2024-01-05 15:04:19 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-01-05 10:06:43 +00:00
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
)
|
|
|
|
|
|
|
|
const apiURL = "https://api.mailgun.net/v3/%s/messages"
|
|
|
|
|
2024-01-05 11:09:02 +00:00
|
|
|
func MailgunRest(to string, data map[string]interface{}, subject string, template string) error {
|
2024-01-05 10:06:43 +00:00
|
|
|
var mailgunAPIKey = os.Getenv("MAILGUN_API_KEY")
|
|
|
|
var mailgunDomain = os.Getenv("MAILGUN_DOMAIN")
|
2024-01-05 16:17:55 +00:00
|
|
|
sender := mailgunDomain + "<noreply@" + mailgunDomain + ">"
|
2024-01-05 16:27:59 +00:00
|
|
|
log.Printf("%r", data)
|
2024-01-05 16:17:55 +00:00
|
|
|
mg := mailgun.NewMailgun(mailgunDomain, mailgunAPIKey)
|
|
|
|
m := mg.NewMessage(sender, subject, "", to)
|
|
|
|
m.SetTemplate(template)
|
2024-01-05 16:43:45 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-01-05 15:26:58 +00:00
|
|
|
|
2024-01-05 16:17:55 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
|
|
|
defer cancel()
|
2024-01-05 10:06:43 +00:00
|
|
|
|
2024-01-05 16:29:50 +00:00
|
|
|
resp, id, err := mg.Send(ctx, m)
|
2024-01-05 10:06:43 +00:00
|
|
|
|
2024-01-05 16:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("ID: %s Resp: %s\n", id, resp)
|
2024-01-05 15:26:58 +00:00
|
|
|
|
2024-01-05 16:17:55 +00:00
|
|
|
return err
|
2024-01-05 10:06:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendMailgun function to send
|
|
|
|
func SendMailgun(to []string, event string, data map[string]interface{}) error {
|
2024-01-05 17:35:58 +00:00
|
|
|
template := "authorizer_email_confirmation"
|
2024-01-05 10:06:43 +00:00
|
|
|
|
|
|
|
switch event {
|
|
|
|
case constants.VerificationTypeBasicAuthSignup:
|
2024-01-05 17:35:58 +00:00
|
|
|
template = "authorizer_email_confirmation"
|
2024-01-05 10:06:43 +00:00
|
|
|
case constants.VerificationTypeForgotPassword:
|
2024-01-05 17:35:58 +00:00
|
|
|
template = "authorizer_password_reset"
|
2024-01-05 10:06:43 +00:00
|
|
|
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
|
|
|
|
|
2024-01-05 15:04:19 +00:00
|
|
|
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
|
2024-01-05 10:06:43 +00:00
|
|
|
}
|