2024-01-05 10:06:43 +00:00
|
|
|
package email
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-01-05 15:04:19 +00:00
|
|
|
"io"
|
2024-01-05 10:06:43 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
// Create payload
|
|
|
|
payload := map[string]interface{}{
|
2024-01-05 15:45:13 +00:00
|
|
|
"from": "noreply@" + mailgunDomain,
|
2024-01-05 15:26:58 +00:00
|
|
|
"to": to,
|
|
|
|
"subject": subject,
|
|
|
|
"template": template,
|
|
|
|
}
|
|
|
|
|
|
|
|
vars, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
payload["h:X-Mailgun-Variables"] = string(vars)
|
2024-01-05 10:06:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert payload to JSON
|
|
|
|
payloadJSON, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-05 15:26:58 +00:00
|
|
|
log.Printf("Mailgun API request payload: %s", payloadJSON)
|
|
|
|
|
2024-01-05 10:06:43 +00:00
|
|
|
// 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()
|
|
|
|
|
2024-01-05 15:04:19 +00:00
|
|
|
// Log the Mailgun API response
|
|
|
|
responseBody, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-01-05 15:51:39 +00:00
|
|
|
log.Printf("Error %v", err)
|
2024-01-05 15:04:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-05 15:51:39 +00:00
|
|
|
log.Printf("Mailgun API response: %v", responseBody)
|
|
|
|
|
2024-01-05 10:06:43 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2024-01-05 11:17:56 +00:00
|
|
|
return fmt.Errorf("failed to send email, status: %d", resp.StatusCode)
|
2024-01-05 10:06:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2024-01-05 13:44:37 +00:00
|
|
|
template = "email_confirmation_ru"
|
2024-01-05 10:06:43 +00:00
|
|
|
case constants.VerificationTypeForgotPassword:
|
2024-01-05 13:44:37 +00:00
|
|
|
template = "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
|
|
|
}
|