How can I configure PHP to send all outgoing mail to my own account so that I can test a business application without actually sending mails to unsuspecting businesses, such as "Congratulations, you have a new account. You will be billed for $xxx" ?
5 Answers
Rather than configuring PHP, a generalized solution would be to stand up a dummy SMTP server.
See this question.
-
Thanks. I already looked at that thread. All the solutions either required Windows, or were for Java servers. I'm on linux and my MTA is qmail. I did find a better solution and I'm testing it now. If it works, I'll post a response to my own question. – Lawrence I. Siden Jul 27 '10 at 18:27
-
@Fosco, can you justify why it's a terrible idea? @Isiden, the SMTP servers suggested run on Java, not your app. – Dolph Jul 27 '10 at 19:53
-
Well, instead of limiting the change to just the one application during testing, now the entire server is incapable of sending legitimate emails. See Isiden's post below re: his solution... – Fosco Jul 27 '10 at 19:59
-
Yes, the entire TEST *server* is isolated from the PRODUCTION environment. – Dolph Jul 27 '10 at 20:18
So you already wrote the application and it uses live email addresses, and now you want to test it? Did you use a centralized function for mail or are there tons of mail() calls all over the code? Sorry but you're going to have to change every mail() call. Do yourself a favor and replace them all with your own function, and then handle test/live functionality in that one location.

- 38,138
- 7
- 87
- 101
-
Sorry. I didn't write the application. I have a copy of the database and I tried to change all the email addresses to my own, but the database is not normalized and there are still many tables with live e-mail addresses that I haven't found yet. – Lawrence I. Siden Jul 27 '10 at 18:28
-
I was suggesting you change the function that sends the email, not the database. The email address is used as a parameter in the mail() function and you could replace it with your own. – Fosco Jul 27 '10 at 18:36
-
-
How about enabling the configuration to go into 'testing' mode and having a 'testing email address' to route all emails to. That's what I'm suggesting.. not hardcoding configuration data. – Fosco Jul 27 '10 at 20:00
You can redirect all port 25 traffic on the server running PHP to a mailserver/port which delivers all mail to you.
This is the only 100% foolproof method of which I know.

- 95,191
- 9
- 106
- 122
-
Are you referring to iptables? That's interesting, but I think I found a more elegant solution, at least for now. – Lawrence I. Siden Jul 27 '10 at 18:37
-
@Isiden Yes, using iptables you can do this :-). I'm glad you've got a solution, whatever it is. – Borealid Jul 27 '10 at 18:40
-
Well, not quite. I found that mail is still getting out to other people. I tried sudo /sbin/iptables -t nat -A OUTPUT -m tcp --dport 25 -j DNAT --to-destination 65.19.154.62 but got iptables: Unknown error 4294967295 The IP address is to smtp.dummysmtp.com – Lawrence I. Siden Jul 27 '10 at 22:07
-
@Isiden: why are you using the nat table? Shouldn't you be using mangle? – Borealid Jul 27 '10 at 22:28
You could create a Google Apps account (or use your dummy server), create a catch-all email account and have it sent to the domain. All you would have to do is look at the catch-all account.

- 31
- 4
-
Thank you. That sounds similar to what I'm describing below when I found a solution. How do you create a dummy server? – Lawrence I. Siden Jul 27 '10 at 18:43
I found this site: http://dummysmtp.com/.
My server is running qmail, so I edited the contents of /var/qmail/control/smtproutes like so:
:smtp.dummysmtp.com username password
It worked when I sent a simple mail with PHP mail(), but later I found that mail is still getting out to other people. I had to crawl into the bowels of the code and found this:
/* Choose the mailer */
switch($this->Mailer) {
case 'sendmail':
$result = $this->SendmailSend($header, $body);
break;
case 'smtp':
$result = $this->SmtpSend($header, $body);
break;
case 'mail':
$result = $this->MailSend($header, $body);
break;
default:
$result = $this->MailSend($header, $body);
break;
//$this->SetError($this->Mailer . $this->Lang('mailer_not_supported'));
//$result = false;
//break;
}
So I had to make sure that each option was configured to send its mail to dummysmtp.com. Once I got that figured out, it all worked.

- 9,191
- 10
- 43
- 56