Parse email send response, log the email ID.

This commit is contained in:
Eli Ribble 2026-01-19 18:10:17 +00:00
parent 2c880568dd
commit 4ab3c355c5
No known key found for this signature in database

View file

@ -23,13 +23,18 @@ func SendEmailReportConfirmation(to string, report_id string) error {
if err != nil {
return fmt.Errorf("Failed to render template %s: %w", reportConfirmationT.name, err)
}
return sendEmail(emailRequest{
resp, err := sendEmail(emailRequest{
From: config.ForwardEmailReportAddress,
HTML: html,
Subject: fmt.Sprintf("Mosquito Report %s Submission", report_id),
Subject: fmt.Sprintf("Mosquito Report Submission - %s", report_id),
Text: text,
To: to,
})
if err != nil {
return fmt.Errorf("Failed to send email report confirmation to %s for report %s: %w", to, report_id, err)
}
log.Info().Str("id", resp.ID).Str("to", to).Str("report_id", report_id).Msg("Sent report confirmation email")
return nil
}
var (
@ -66,16 +71,44 @@ type emailRequest struct {
References []string `json:"references,omitempty"`
}
type emailEnvelope struct {
From string `json:"from"`
To []string `json:"to"`
}
type emailResponse struct {
IsRedacted bool `json:"is_redacted"`
CreatedAt string `json:"created_at"`
HardBounces []string `json:"hard_bounces"`
SoftBounces []string `json:"soft_bounces"`
IsBounce bool `json:"is_bounce"`
Alias string `json:"alias"`
Domain string `json:"domain"`
User string `json:"user"`
Status string `json:"status"`
IsLocked bool `json:"is_locked"`
Envelope emailEnvelope `json:"envelope"`
RequireTLS bool `json:"requireTLS"`
MessageID string `json:"messageId"`
Headers map[string]string `json:"headers"`
Date string `json:"date"`
Subject string `json:"subject"`
Accepted []string `json:"accepted"`
Deliveries []string `json:"deliveries"`
RejectedErrors []string `json:"rejectedErrors"`
ID string `json:"id"`
Object string `json:"object"`
UpdatedAt string `json:"updated_at"`
Link string `json:"link"`
Message string `json:"message"`
}
func sendEmail(email emailRequest) error {
func sendEmail(email emailRequest) (response emailResponse, err error) {
url := "https://api.forwardemail.net/v1/emails"
payload, err := json.Marshal(email)
if err != nil {
return fmt.Errorf("Failed to marshal email request: %w", err)
return response, fmt.Errorf("Failed to marshal email request: %w", err)
}
req, _ := http.NewRequest("POST", url, bytes.NewReader(payload))
@ -87,6 +120,11 @@ func sendEmail(email emailRequest) error {
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
log.Info().Str("status", res.Status).Str("response_body", string(body)).Msg("Attempted to send email")
return nil
// Parse the JSON response
err = json.Unmarshal(body, &response)
if err != nil {
log.Warn().Str("status", res.Status).Str("response_body", string(body)).Msg("Attempted to send email but couldn't parse the resulting JSON")
return response, fmt.Errorf("Failed to unmarshal JSON response: %w", err)
}
return response, nil
}