tags:

views:

40

answers:

5

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" ?

+1  A: 

Rather than configuring PHP, a generalized solution would be to stand up a dummy SMTP server.

See this question.

Dolph
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.
lsiden
-1 In my opinion, this is a terrible idea.
Fosco
@Fosco, can you justify why it's a terrible idea?@Isiden, the SMTP servers suggested run on Java, not your app.
Dolph
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
Yes, the entire TEST *server* is isolated from the PRODUCTION environment.
Dolph
A: 

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.

Fosco
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.
lsiden
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
--; Suggesting that the OP hardcode configuration data? For shame.
Dolph
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
A: 

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.

Borealid
Are you referring to iptables? That's interesting, but I think I found a more elegant solution, at least for now.
lsiden
@Isiden Yes, using iptables you can do this :-). I'm glad you've got a solution, whatever it is.
Borealid
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.62but gotiptables: Unknown error 4294967295The IP address is to smtp.dummysmtp.com
lsiden
@Isiden: why are you using the nat table? Shouldn't you be using mangle?
Borealid
A: 

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.

theproxy
Thank you. That sounds similar to what I'm describing below when I found a solution. How do you create a dummy server?
lsiden
A: 

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.

lsiden