Search code examples
phpruby-on-railsemailproduction-environmenttest-environments

`plus` in email to have unique versions of same email how does it work? Managing production and test environment


I am having a web application with a production with hundreds of real users..

I needed to have one test environment with the state of production, as the application is data-driven, states of application change from data, things can-not be tested, if no real user data exists...

But as it has real emails its risk in testing, as testing emails cana go to real users..

So I needed a mechanism to change the emails to something not real, but as email field is unique, how do I change all of them with one query..

My colleague suggested that a email with +any_number ex. [email protected] and [email protected] will send the emails to same email..

So I wrote a some query

update users set email = CONCAT(CONCAT('rajat+',id),'@gmail.com')

and it solved the problem..Now I can operate in testing enviornment with production data..

My Questions:

1) How does it work??

2) Is it a right approach?? How do people manage test and production environments??


Solution

  • I prefer to change the code low level instead of changing the database. For example:

    function sendEmail($to, ...) {
        if (APPLICATION_ENV != 'production') {
            $to = "dev@.....";
        }
        // continue ...
    }
    

    This way you always ensure the recipient address is changed to a test address, even if the address came from a different source like a form.