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 realclient@client.com to myaccount+realclient@gmail.com 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+\$1@gmail.com", $email);
return $email;
}


To use the above function simply replace myaccount with your email address's name and gmail.com 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.

2 comments:

shri hari said...

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

Lucy Grace said...

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

Post a Comment