Prevent unwanted email delivery

If you have multiple instances of a Magento shop – for example a staging and a production environment – sooner or later you’ll come to the point, where you need to  synchronize the database from live- to stage-system to get realistic production-like application behavior. In this case it is a good idea to prevent the system from sending mails to real customers – to disarm email the addresses -. But you probably want to send emails in cases where you test some email related functionality (f.e. email template development). The best way to achieve this, is to replace all customer email addresses by dummy addresses. This can be done with the following queries against the Magento database:

$> magerun db:query "UPDATE customer_entity SET email = CONCAT(SUBSTRING_INDEX(email, '@', 1), '-disarm@ask-sheldon_', entity_id, '.com') WHERE email NOT LIKE '%-disarm@ask-sheldon_%'"
$> magerun db:query "UPDATE sales_flat_order SET customer_email = CONCAT(SUBSTRING_INDEX(customer_email, '@', 1), '-disarm@ask-sheldon_', entity_id, '.com') WHERE customer_email NOT LIKE '%-disarm@ask-sheldon_%'"
$> magerun db:query "UPDATE sales_flat_order_address SET email = CONCAT(SUBSTRING_INDEX(email, '@', 1), '-disarm@ask-sheldon_', entity_id, '.com') WHERE email NOT LIKE '%-disarm@ask-sheldon_%'"

These queries replace all customer related emails with their local part followed by ‘-disarm@ask-sheldon.com’ (suffix + @ + domain part). Therefore all emails will be send to your own email sever.

As you can see I use Magerun, that is really awesome, to send the queries to the database. This can easily be put into a bash script or another automation or deployment tool.

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.