How to not accidentally send thousands of 'beta invites'
Posted by Matt Jankowski
Nov 30
So you’re building a sweet new web 2.0 app and you need an exclusive pre-launch beta test. It’s gotta be wicked pretentious and exclusive. I don’t know, maybe you’ve spent too much time on the west coast and are disconnected from the realities of the world. Alternately, you’ve already launched a site and your staging environment server is regularly getting dumps of production data to simulate real load and real use cases.
You’ve got a simple ActionMailer in app/models/notifier.rb like…
1 2 3 4 5 6 7 8 9 10 11 12 |
class Notifier < ActionMailer::Base def invitation(invitation) recipients invitation.email from 'invitations@accountdetails.info' subject "[Account Details] - #{invitation.user.name} has invited you to use account details!" body :invitation => invitation end end |
...but you have to test the staging version of the site without actually sending email to people. Either you’re in pre launch and you don’t want Aunt Sally to get an email ahead of time; or you’re in post launch and you don’t want user 13345345 to accidentally get a weekly email reminder about some fake activity you’re simulating in his site groups.
But wait, you can’t disable outgoing smtp entirely, because the QA team themselves need to be able to test the mailers and give feedback/signoff on what I’m sure is a really fancy HTML email you’ve cooked up for them.
The following is a simple solution to a silly problem. You can throw some constant like this into your environments. In environments/staging.rb:
1 2 3 |
MAILER_RECIPIENTS = 'qa-team@accountdetails.info' |
And then in environments/production.rb:
1 2 3 |
MAILER_RECIPIENTS = nil |
And now, back in app/models/notifier.rb…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Notifier < ActionMailer::Base def invitation(invitation) recipients mail_recipients(invitation.email) from 'invitations@accountdetails.info' subject "[Account Details] - #{invitation.user.name} has invited you to use account details!" body :invitation => invitation end protected def mail_recipients(recipients) MAILER_RECIPIENTS || recipients end end |
Note that the first line of #invitation changed to call the new #mail_recipients instead of just sending in the email directly.
This way your QA team sees all email being generated by the site, and doesn’t have to live in constant fear of meddling users accidentally getting in on your early pre launch action!
Anyone have a better way to do this? So far we’ve thought of…
- “Tell your QA team not to send anyone but themselves email”—but we all know how that will end
- “Mock out notifier.rb in text/mocks/staging”—I think this would work
Comments on this post
Nov 30
Tom said,
Does Mailtrap fit the bill?
Depends on whether or not you want to test that your sendmail stuff works, or that the content of the mail is correct.
Nov 30
Piers Cawley said,
So I’m curious; is this the most cryptic announcement of a cool new web 2.0 site yet?
Nov 30
matt jankowski said,
Tom, yeah Mailtrap is pretty cool – but we need to be able to have mail go to certain people and not be totally stopped. If Mailtrap had a “accept all mail and relay it to this address instead of where it wanted to go” or “accept all mail and only allow mail addressed to these certain addresses through” setting – that would be interesting.
Piers, cryptic or not, I can assure you that accountdetails.info is not something you’ll want to miss!
Nov 30
DevP said,
I’d make sure that imported live data has the email addresses obfuscated, except for accounts/email addresses used by staff. (So rewriting superguy@aol.com to superguy.at.aol.com@test.lametube.net, and you could set up a thing where *@test.lambetube.net goes somewhere reasonable.)
Ideally I’d still want something in the code (like this) to keep accidents from happening, though. Maybe a module for ActionMailer that forces the use of MAILER_RECIPIENTS?
Nov 30
Wes Maldonado said,
We have basically done the same thing as you except we use rail mail set to not deliver in dev/test and set it to relay in the staging environment using the technique you mention.
Sorry, comments are closed for this article.
© 2000 - 2009 by thoughtbot, inc.
written by a bushel of tiny robots
Widgetfinger: Simple content management for simple websites.
Come “ride the toad” on Hoptoad, the app error app.
Tee-Bot, funny shirts your friends won't understand!
Umbrella Today: “It’s like totally the simplest weather report ever, Julie.”
Thoughtbot
thoughtbot is a technology consulting firm that provides web application development and design services. We focus on building modern systems, embracing good ideas and delivering elegant solutions.
Interested in learning Rails?
Sign up for our training.
Archives