Simple PHP email testing with regular expressions

When working with system generated emails in PHP it can be difficult as email is often considered unique data in the system. To create and check various email accounts can become tedious and when testing on a live system the results of sending a test email to a client can be rather embarrassing.

The solution is a simple PHP function that you can call with any email address and it will convert it into a unique email address for an existing mailbox. The function will take an email address like to Please note that the testing destination email address needs to support the + operator (ie Gmail).

// Create test email for use in testing mode
function createTestEmail($email) {
$email = preg_replace('/(.*?)@(.*)/', "myaccount+\$", $email);
return $email;

To use the above function simply replace myaccount with your email address's name and with your email's domain then put through it any email address your system sends to when you are testing. I would recommend automatically replace any destination address with the results of the function if the URL from $_SERVER['HTTP_HOST'] matches your development domain; this way the process is automatic and you don't need to worry about changing configuration or debugging flags.


Unknown said...

Development is always an great thing to work with.
Web Designing Company In Chennai

Unknown said...

Interesting to know about it! I have never thought Email testing is so easy as you say.
school websites design

Post a Comment