Description

Once a site has gone live, it's important to not send emails (and do other things like pushing to Twitter/Facebook) from development environments. These actions should only happen from the production server. This tutorial describes how to set up your code to ensure that you are only sending emails etc. from the production environment.

The setup: settings.php

The first thing to do is to set up a variable in settings.php that can be used in your code to determine whether you are in a development or production environment. This can be done by editing settings.php and adding the following code to the bottom of the file:

  • Development environment: $conf['development_environment'] = TRUE;
  • Production environment: $conf['development_environment'] = FALSE;

After the above code has been set, we can use variable_get('development_environment', TRUE); in any function to access the value of the variable set in settings.php (thanks Ayesh). Next we will examine how this can be used in your code.

Preventing email

Preventing emails from being sent from the system is quite simple. All that needs to be done is to implement hook_mail_alter(), and set send to FALSE. We will also log the values sent to the watchdog, so we can use them for debugging:

span class="co1">// We don't want to send emails if the variable has not been set, or if it has been set and is TRUE.
  // We can use variable_get() to get the $conf variable set in our settings.php file
  // Note that by setting the default to TRUE, the default setting for the system is to be
  // a development environment. Set this to FALSE to have the default be a live environment.
'development_environment'// First: Prevent the mail from being sent
    $message['send'// Next: Log the mail so it can be debugged if necessary
'Development Env', 'The following email was not sent: !message''!message' => '<pre>''</pre>'));
  }
}

Other uses

A version of the above method can be used anywhere on your site where code is pushed to 3rd parties, or anything else you don't want to happen on development environments. For example, on one of our systems, we are pushing items to a Twitter feed using the function twitter_set_status() provided by the Twitter module. We only want to push these tweets when on the production environment. So once again, we check the value from settings.php, and then determine what to do based on that value:

span class="st0">'development_environment''Development Env', 'The following item was not pushed to twitter as this is a development environment: !twitter_item''!twitter_item'