I'm building a daemon in php that checks for received emails which it then stores in the database leading them through a whole process. The thing is that I want to build some unit tests for this, for which I don't want to setup a whole mail server. So for tests I want to somehow send emails to localhost, which should then be picked up by the daemon and processed further. So I tried the following:
$headers = 'From: me@mydomain.com
Reply-To: me@mydomain.com
X-Mailer: PHP/' . phpversion();
mail('www-data@localhost', 'THE SUBJECT', 'THE BODY IS HERE', $headers);
When I then run mail
from the command line, I just get a message saying No mail for kramer65
.
So my question; does anybody know how I can send emails to localhost in php, and how I can then read these emails from within php again? All tips are welcome!
[EDIT] So I figured that it is sending an email to the www-data account, and not to my personal kramer65 account. I changed the to
email address into kramer65@localhost, and when I now run mail
I get
kramer65@php0:~$ mail
Mail version 8.1.2 01/15/2001. Type ? for help.
"/var/mail/kramer65": 1 message 1 new
>N 1 kramer65e@php0 Fri Apr 25 10:48 16/495 THE SUBJECT
&
My following question is now; how do I read or somehow get this email from within php?
This depends on how you have configured the php internal mail settings. If you configured it to use a local mail forward agent (sendmail
or similar) then you should be able to send messages to a local account (not a local email address) by just specifying the account name. At least this is what such agents offer. Unless php explicitly prevents such usage it might be worth a try.
You cannot send to a local email address, since that requires an email server, specifically an smtp server (exim
or the like). Without it there is no component that could accept an incoming message.