From 4ab3c355c5187ef5e6bb803f63cc4819dec6060c Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Mon, 19 Jan 2026 18:10:17 +0000 Subject: [PATCH] Parse email send response, log the email ID. --- comms/email.go | 54 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/comms/email.go b/comms/email.go index cd1e930e..86accbaf 100644 --- a/comms/email.go +++ b/comms/email.go @@ -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 emailResponse struct { - Message string `json:"message"` +type emailEnvelope struct { + From string `json:"from"` + To []string `json:"to"` } -func sendEmail(email emailRequest) error { +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) (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 }