mailgun-rest
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
@@ -95,6 +96,15 @@ func SendEmail(to []string, event string, data map[string]interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
mailgunAPIKey := os.Getenv("MAILGUN_API_KEY")
|
||||
|
||||
if len(mailgunAPIKey) > 0 {
|
||||
err = SendMailgun(to, event, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
m := gomail.NewMessage()
|
||||
senderEmail, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeySenderEmail)
|
||||
if err != nil {
|
||||
|
96
server/email/mailgun.go
Normal file
96
server/email/mailgun.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
// log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
)
|
||||
|
||||
const apiURL = "https://api.mailgun.net/v3/%s/messages"
|
||||
|
||||
func mailgun_send(to []string, data map[string]interface{}, subject string, template string) error {
|
||||
var mailgunAPIKey = os.Getenv("MAILGUN_API_KEY")
|
||||
var mailgunDomain = os.Getenv("MAILGUN_DOMAIN")
|
||||
|
||||
// Create custom variables JSON
|
||||
customVariablesJSON, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create payload
|
||||
payload := map[string]interface{}{
|
||||
"from": mailgunDomain + " <noreply@" + mailgunDomain + ">",
|
||||
"to": to,
|
||||
"subject": subject,
|
||||
"template": template,
|
||||
"h:X-Mailgun-Variables": string(customVariablesJSON),
|
||||
}
|
||||
|
||||
// Convert payload to JSON
|
||||
payloadJSON, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Make HTTP POST request
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("POST", fmt.Sprintf(apiURL, mailgunDomain), bytes.NewBuffer(payloadJSON))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.SetBasicAuth("api", mailgunAPIKey)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("failed to send email, status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendMailgun function to send
|
||||
func SendMailgun(to []string, event string, data map[string]interface{}) error {
|
||||
template := "email_confirmation"
|
||||
|
||||
switch event {
|
||||
case constants.VerificationTypeBasicAuthSignup:
|
||||
template = "email_confirmation"
|
||||
case constants.VerificationTypeForgotPassword:
|
||||
template = "reset_password"
|
||||
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 := mailgun_send(to, data, subject, template)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user