debug-mailgun
This commit is contained in:
parent
0331d34afc
commit
e62f356a79
|
@ -19,18 +19,17 @@ func MailgunRest(to string, data map[string]interface{}, subject string, templat
|
||||||
var mailgunAPIKey = os.Getenv("MAILGUN_API_KEY")
|
var mailgunAPIKey = os.Getenv("MAILGUN_API_KEY")
|
||||||
var mailgunDomain = os.Getenv("MAILGUN_DOMAIN")
|
var mailgunDomain = os.Getenv("MAILGUN_DOMAIN")
|
||||||
|
|
||||||
vars, err := json.Marshal(data)
|
|
||||||
if err != nil {
|
|
||||||
vars = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create payload
|
// Create payload
|
||||||
payload := map[string]interface{}{
|
payload := map[string]interface{}{
|
||||||
"from": mailgunDomain + " <noreply@" + mailgunDomain + ">",
|
"from": mailgunDomain + " <noreply@" + mailgunDomain + ">",
|
||||||
"to": to,
|
"to": to,
|
||||||
"subject": subject,
|
"subject": subject,
|
||||||
"template": template,
|
"template": template,
|
||||||
"h:X-Mailgun-Variables": string(vars),
|
}
|
||||||
|
|
||||||
|
vars, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
payload["h:X-Mailgun-Variables"] = string(vars)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert payload to JSON
|
// Convert payload to JSON
|
||||||
|
@ -39,6 +38,8 @@ func MailgunRest(to string, data map[string]interface{}, subject string, templat
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Mailgun API request payload: %s", payloadJSON)
|
||||||
|
|
||||||
// Make HTTP POST request
|
// Make HTTP POST request
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
req, err := http.NewRequest("POST", fmt.Sprintf(apiURL, mailgunDomain), bytes.NewBuffer(payloadJSON))
|
req, err := http.NewRequest("POST", fmt.Sprintf(apiURL, mailgunDomain), bytes.NewBuffer(payloadJSON))
|
||||||
|
@ -59,12 +60,10 @@ func MailgunRest(to string, data map[string]interface{}, subject string, templat
|
||||||
// Log the Mailgun API response
|
// Log the Mailgun API response
|
||||||
responseBody, err := io.ReadAll(resp.Body)
|
responseBody, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error reading Mailgun API response: %v", err)
|
log.Printf("Error %v reading Mailgun API response: %v", err, responseBody)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Mailgun API response: %s", responseBody)
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return fmt.Errorf("failed to send email, status: %d", resp.StatusCode)
|
return fmt.Errorf("failed to send email, status: %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user