He Unsubscribed Once. My Laravel App Kept Emailing Him Weekly
I run Boat Build Log, a free site where amateur boat builders keep a log of their build. The site sends a few optional emails: a weekly digest of builds you follow, a community digest, and a short nudge when your own log
I run Boat Build Log, a free site where amateur boat builders keep a log of their build. The site sends a few optional emails: a weekly digest of builds you follow, a community digest, and a short nudge when your own log has gone quiet for a couple of weeks.
Every one of those has an unsubscribe link, and every one carries the List-Unsubscribe headers so the Unsubscribe button in Gmail or Apple Mail works too. I thought that was the whole job. It wasn't.
The alert that kept coming back
Once a week I'd get an alert about a failed queue job. Same job class, same recipient, a build reminder that the mail provider refused. I'd look, shrug, and move on, because one failed email out of a batch didn't seem worth an afternoon.
It took me a few weeks to actually read the error. The provider wasn't failing. It was refusing, with a message that the recipient was marked as inactive.
Here's what had happened. A builder pressed Unsubscribe on a digest in his mail client. My mail provider handled that request itself, put his address on its own suppression list for that stream, and never told my app. On my side his digest preference was off for a different reason, but his build reminder preference was still on. So every week the reminder command queued him an email, the provider refused it, the job failed, and I got an alert.
He'd said no once, and the site kept asking. The only thing protecting him was the provider, and the only signal I had was an alert I'd been ignoring.
Two routes in, one answer
A provider puts an address on its suppression list for three reasons: a hard bounce, a spam complaint, or an unsubscribe it handled on its own. In all three cases it's telling you the same thing: it won't deliver to this person, so stop trying.
I wanted the app to hear that in two ways:
- A daily sync that pulls the suppression lists and switches off optional mail for anyone on them.
- A catch on the refusal itself, so if a send is refused before the sync has run, that one failure is the last.
Both end in the same method.
Switching a person off
Preferences on the site are plain boolean columns on the user, one per category, described in a single constant. That made the "silence" step short:
public static function silence(User $user, string $why): bool
{
$changes = [];
foreach (MailPreferences::CATEGORIES as $category => $meta) {
// only touch what is actually on, so we can report honestly
if (MailPreferences::wants($user, $category)) {
$changes[$meta['column']] = false;
}
}
if (! $changes) {
return false; // nothing was on, nothing changed
}
$user->forceFill($changes)->save();
Log::info('mail suppressions: optional mail switched off', [
'user_id' => $user->id, 'why' => $why, 'categories' => array_keys($changes),
]);
return true;
}
Notice it turns off every optional category, not just the one that bounced. My first version only switched off the category of the email that failed. That's wrong. A suppression isn't about one kind of email, it's the provider saying it won't deliver to this address on that stream at all. If I'd left the digest on when the reminder was refused, I'd have just moved the failure from Monday to Saturday.
Transactional mail isn't touched. Verification codes and password resets go down a separate stream and they aren't a preference. An unsubscribe link on a password reset would be offering to lock someone out of their own log.
The daily sync
The sync asks the provider for the suppression list of each stream the site sends on, collects the addresses, and silences the matching users:
foreach ($this->streams() as $stream) {
try {
$response = Http::withHeaders($this->authHeaders())
->acceptJson()->timeout(20)
->get($this->suppressionsUrl($stream));
} catch (Throwable $e) {
Log::warning('mail suppressions: provider unreachable', ['stream' => $stream]);
return null; // could not ask, so change nothing
}
if (! $response->ok()) {
return null;
}
foreach ($response->json('Suppressions', []) as $row) {
// providers return whatever case the address was sent in
$email = strtolower(trim((string) ($row['EmailAddress'] ?? '')));
if ($email !== '') {
$addresses[$email] = ($row['SuppressionReason'] ?? 'unknown').' on '.$stream;
}
}
}
A few choices in there that I'd make again:
-
Returning
nullis different from returning zero. If the provider can't be reached, the command reports failure and changes nothing. "I couldn't ask" must never look like "nobody is suppressed". -
Lowercase before comparing. The first test I wrote used
[email protected]on the provider's side and[email protected]in my database, because that's exactly the kind of mismatch real data has. -
Include soft-deleted users.
User::withTrashed()is on purpose. A closed account that gets restored shouldn't come back with its emails switched on. - It's safe to run twice. The second run finds nothing to change and says so. The command prints how many addresses it checked, how many it switched off now, and how many were already off.
It's scheduled daily, a few minutes before the morning reminder commands, so someone who unsubscribed through their mail client yesterday isn't queued a reminder today.
Catching the refusal as it happens
The sync leaves a gap of up to a day. To close it, every optional mailable uses one trait, and Laravel calls failed() on a queued mailable when its job fails:
public function failed(Throwable $e): void
{
// only a refusal is the recipient's doing; a timeout or a 500 is ours
if (MailSuppressions::looksLikeRefusal($e)) {
MailSuppressions::silence($this->unsubscribeRecipient(), 'refused: '.class_basename($this));
}
}
looksLikeRefusal() walks the exception chain with getPrevious(), because by the time the error reaches the mailable it has been wrapped a couple of times by the mailer and the queue. It matches on the provider's refusal message and status code. That's string matching, and I don't love it, but the transport doesn't give me a typed exception for "inactive recipient". The test builds the real transport exception and wraps it, so if the wording changes in a library upgrade I'll find out from a failing test and not from another weekly alert.
Any other failure changes nothing. If the provider has an outage, I don't want to wake up to a site that has unsubscribed everybody.
What I'd tell you to check
If you send mail through any provider, go look at its suppression list today. Then pick a few addresses on it and check what your own database thinks those people want. If the two disagree, your app is still queuing mail that can only fail, and somebody who said no is being asked again every week.
The other lesson was about alerts. A recurring alert you've learned to ignore isn't noise, it's a bug report with a schedule. This one was telling me the same thing every week and I didn't read it.
The builders on the build logs signed up to write about their boats, not to manage my email settings. Feel free to lift the shape of this for your own app: one place that knows what "optional" means, one method that switches it off, and two ways to get there.
If you're building a boat, or thinking about it, the site is at boatbuildlog.com.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.