Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate emails before adding MailJet contact #57714

Merged
merged 9 commits into from
Apr 29, 2024

Conversation

bethanyaconnor
Copy link
Contributor

@bethanyaconnor bethanyaconnor commented Apr 2, 2024

MailGun, a related product to MailJet, offers an API to validate email addresses and out technical account manager suggested we use this functionality. This checks to make sure the emails we add to MailJet are at least real addresses and that they're not known spammy addresses.

This PR adds the MailGun gem and utilizes this API. More info on this API is here.

This also turned into a bit of a refactor to ensure the email validation is only checked once; when I first wrote it, I was checking email validation on both contact creation and email send. I decided to do this by making it so emails must be sent to MailJet contacts, even though that's not strictly necessary. Definitely open to feedback here!

@bethanyaconnor bethanyaconnor force-pushed the bethany/lifecycle-marketing/email-validation branch from 6f471db to 4938891 Compare April 8, 2024 14:42
@bethanyaconnor bethanyaconnor force-pushed the bethany/lifecycle-marketing/email-validation branch from 4938891 to ba7c04e Compare April 12, 2024 15:55
@bethanyaconnor bethanyaconnor changed the title lifecycle marketing email validation Validate emails before adding MailJet contact Apr 15, 2024
@bethanyaconnor bethanyaconnor changed the base branch from staging to bethany/lifecycle-marketing/create-mailgun-secrets April 15, 2024 13:15
@bethanyaconnor bethanyaconnor marked this pull request as ready for review April 16, 2024 19:00
@bethanyaconnor bethanyaconnor requested review from cat5inthecradle and a team April 16, 2024 19:00
Copy link
Contributor

@hannahbergam hannahbergam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good to me- I left a few small questions!

@@ -16,7 +18,8 @@ def self.enabled?
DCDO.get('use_mailjet', false) &&
!Rails.env.test? &&
API_KEY.present? &&
SECRET_KEY.present?
SECRET_KEY.present? &&
MAILGUN_API_KEY.present?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now when I see api keys I'm called to ask: have we tested that the key is correctly configured? I'm sorry if this has already been tested elsewhere and I'm late to the game!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it's a good question! I've actually been checking in Secrets Manager for each key every time I update one, particularly for the exact issue that hit us with Statsig 😅 Obviously, testing that the key is definitely correct (ie I didn't copy all but the last character or something) is harder to test before this hits products, but, as this is behind a DCDO flag, hopefully it would be a short outage if there is one.

}
]
)
end

def self.send_template_email(to_email, to_name, email_config)
def self.send_template_email(contactdata, email_config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any world in which this returns an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to remember -- I actually had more issues with this failing silently instead of throwing errors. Do you think this should be wrapped in exception handling in some way? (Might also be feedback for me to incorporate into #58099)

def test_create_contact
email = 'fake.email@email.com'
def test_create_contactdata
email = 'fake.email@test.xx'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

refute MailJet.valid_email?('test@email.com')
end

def test_valid_email_do_not_send
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all these tests!

Base automatically changed from bethany/lifecycle-marketing/create-mailgun-secrets to staging April 23, 2024 17:53
@bethanyaconnor
Copy link
Contributor Author

@hannahbergam I found a bug when testing on the adhoc and updated this PR with that fix in 69c2e61

Copy link
Contributor

@hannahbergam hannahbergam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find! So I'm following- it sounds like MailJet has two different concepts for a 'Contact' and that contact's 'ContactData' (and prior to this commit, those were combined in ContactData). Now I'm curious about all the data that could live in 'data' that wouldn't be represented in 'Contact', but that's an aside! LGTM

@bethanyaconnor
Copy link
Contributor Author

Yeahhh, it seems like Contact is a really simple data structure with name and email and Contactdata is all the custom stuff. Some more info in their documentation.

@bethanyaconnor bethanyaconnor merged commit 7f370e1 into staging Apr 29, 2024
2 checks passed
@bethanyaconnor bethanyaconnor deleted the bethany/lifecycle-marketing/email-validation branch April 29, 2024 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants