Skip to content

Commit

Permalink
Add meta() info to DMARC, spam, and parse failures
Browse files Browse the repository at this point in the history
This may help diagnose potential issues by more clearly seeing where the
message was from, where it was intended, and what the subject looked
like.
  • Loading branch information
mbland committed Mar 31, 2023
1 parent 2caf845 commit 2f74afb
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"strings"

"github.com/aws/aws-lambda-go/events"
"github.com/mbland/elistman/ops"
Expand Down Expand Up @@ -157,26 +158,35 @@ func newMailtoEvent(ses *events.SimpleEmailService) *mailtoEvent {
// - https://docs.aws.amazon.com/ses/latest/dg/receiving-email-notifications-contents.html
// - https://docs.aws.amazon.com/ses/latest/dg/receiving-email-notifications-examples.html
func (h *Handler) handleMailtoEvent(ev *mailtoEvent) error {
prefix := "message " + ev.MessageId + ": "
prefix := "unsubscribe message " + ev.MessageId + ": "

if bounced, err := h.bounceIfDmarcFails(ev); err != nil {
return fmt.Errorf("%serror while bouncing: %s", prefix, err)
return fmt.Errorf("%sDMARC bounce fail: %s: %s", prefix, meta(ev), err)
} else if bounced {
log.Printf("%sbounced", prefix)
log.Printf("%sDMARC bounce: %s", prefix, meta(ev))
} else if isSpam(ev) {
log.Printf("%sspam, ignoring", prefix)
log.Printf("%smarked as spam, ignored: %s", prefix, meta(ev))
} else if op, err := parseMailtoEvent(ev, h.UnsubscribeAddr); err != nil {
log.Printf("%sparse error, ignoring: %s", prefix, err)
log.Printf("%sfailed to parse, ignoring: %s: %s", prefix, meta(ev), err)
} else if result, err := h.Agent.Unsubscribe(op.Email, op.Uid); err != nil {
return fmt.Errorf("%serror unsubscribing %s: %s", prefix, op.Email, err)
return fmt.Errorf("%serror: %s: %s", prefix, op.Email, err)
} else if result != ops.Unsubscribed {
log.Printf("%snot unsubscribed: %s: %s", prefix, op.Email, result)
log.Printf("%sfailed: %s: %s", prefix, op.Email, result)
} else {
log.Printf("%ssuccessfully unsubscribed: %s", prefix, op.Email)
log.Printf("%ssuccess: %s", prefix, op.Email)
}
return nil
}

func meta(ev *mailtoEvent) string {
return fmt.Sprintf(
"[From:\"%s\" To:\"%s\" Subject:\"%s\"]",
strings.Join(ev.From, ","),
strings.Join(ev.To, ","),
ev.Subject,
)
}

func (h *Handler) bounceIfDmarcFails(ev *mailtoEvent) (bool, error) {
return false, nil
}
Expand Down

0 comments on commit 2f74afb

Please sign in to comment.