How to not accidentally send thousands of 'beta invites'

Matt Jankowski

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…

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:

MAILER_RECIPIENTS = 'qa-team@accountdetails.info'

And then in environments/production.rb:

MAILER_RECIPIENTS = nil

And now, back in app/models/notifier.rb…

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