fix: validate email format and add defensive mailer checks#2571
Merged
olleolleolle merged 3 commits intocodebar:masterfrom Apr 14, 2026
Merged
fix: validate email format and add defensive mailer checks#2571olleolleolle merged 3 commits intocodebar:masterfrom
olleolleolle merged 3 commits intocodebar:masterfrom
Conversation
- Add email format validation to Member model (conditional on can_log_in) - Add defensive validation in EmailHeaderHelper with warning logs - Add tests for email format validation - Add tests for EmailHeaderHelper#mail_args The SMTP error was caused by invalid email addresses in the database. Member 2413 had 'emmalepinay.yahoo.com' (missing @) causing 501 errors. Resolves the root cause at three layers: 1. Database cleanup (set NULL for bad emails) 2. Member model validation (reject invalid at registration) 3. Mailer defense (skip invalid with log warning)
- Add email_validator gem for Rails-native email validation - Replace custom EMAIL_REGEX with gem's strict mode - Single source of truth for email validation
Use type: :helper to include EmailHeaderHelper automatically, making the spec more idiomatic. Removed verbose anonymous class construction.
Collaborator
Author
|
Thank you for the suggestion! You're right - I've updated the spec to use the idiomatic pattern which automatically includes the helper module. Much cleaner now. Commit: fcdf515 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
Net::SMTPSyntaxError: 501 Recipient syntax errorthat occurred in production when sending workshop attendance reminders.Root Cause Analysis
The error occurs in
WorkshopInvitationManagerConcerns#send_workshop_attendance_reminderswhen iterating overworkshop.attendances.not_remindedand sending reminder emails viaWorkshopInvitationMailer#attending_reminder.Root cause: Member email addresses are not validated before SMTP delivery. The
EmailHeaderHelper#mail_argsmethod passesmember.emaildirectly to the mailer without format validation.Affected Data
Found 18 members with malformed email addresses in production database. Examples include:
These records likely originated from:
Changes
1. Database Cleanup
Set invalid emails to NULL so they skip future mailings:
2. Member Model Validation
app/models/member.rb— Added email validation usingemail_validatorgem:3. Defensive Mailer Check
app/helpers/email_header_helper.rb— Returnsnilfor invalid emails with warning log:4. Added Gem
Added
email_validatorgem (32M+ downloads, MIT licensed) for::loose,:strict,:rfc)How to Verify
1. Run the cleanup SQL
Connect to production database (Heroku postgres or production Postgres):
2. Verify cleanup
3. Check logs
After deploying, watch for warning logs when rake tasks run:
Tests
spec/models/member_spec.rbEmailHeaderHelper#mail_argsinspec/helpers/email_header_helper_spec.rbRun tests locally:
bundle exec rspec spec/models/member_spec.rb spec/helpers/email_header_helper_spec.rbImpact
can_log_in=truemust provide valid email formatcan_log_incan still have NULL emails