PHP notification when i receive new e-mail [closed]
Want to improve this开发者_如何学运维 question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this questionhow to be notified when i receive any email to a specific email address on my web server for example if i have an email on my server "replay@xx.com" when i have any email i want to fire a script is that possible with PHP ?
Thanks
Using cronjobes you will read periodically your mail with IMAP/POP3 whatever they call it. I wrote a similar app. I'll write part of it here within minutes. Edit: Here it is. http://php.net/manual/en/function.imap-headerinfo.php
$m_mail = imap_open ("{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX", $m_username . "@gmail.com", $m_password)
// or throw a freakin error............you pig
or die("ERROR: " . imap_last_error());
// unix time gone by or is it bye.....its certanly not bi.......or is it? ......I dunno fooker
$m_gunixtp = array(2592000, 1209600, 604800, 259200, 86400, 21600, 3600, 5500);
// Date to start search
$m_gdmy = date("F j, Y, g:i a", "200901022");
echo $m_gdmy;
//search mailbox for unread messages since $m_t date
$m_search=imap_search ($m_mail, 'UNSEEN');
Typically on a linux box you can pipe an email to an application. you'd have to write your php script to be a commandline app.
either way you really can't do it with php alone.
I'm not 100% sure how to make whatever email server you use fire off a script when it recieves mail, but i hope i got you a little closer to your answer.
provided the emails go to your webserver, yes. heck, even if they go elsewhere, and that elsewhere can open http connections to your webserver, then yes.
The easiest way may be to just use a library to access the mail server and check the mailbox for new mail, and then just have this in a script that runs on a regular basis. Something like this will work for checking the mailbox in php.
As other have hinted there are two approaches:
1) configure your mail delivery agent (or mail transport agent) to route the incoming message via a filter
2) Have a scheduled job poll the mailbox to see if new messages have arrived
The first method totally depends on how your MTA / MDA is configured - with luck your server may be using procmail which is a very flexible tool (configured via the file .procmailrc in the home dir of the user whom owns the mailbox). e.g.
:0 * ^To:.*replay@xx.com * !FROM_DAEMON | /usr/bin/php -q /path/to/yourscript.php
Then read the email from stdin in yourscript.php
Add a space f to the first line (:0 f) and write a copy to stdout if you want it to go to the mailbox as well. Note that there is a whole lot of things you can configure using procmail. The ! FROM_DAEMON should eliminate most feedback loops.
If not, most MDA's will support routing through a filter using a .forward file in the users home dir.
Put this in the file:
| /usr/bin/php -q /path/to/yourscript.php
Note that ALL mail which would have been delivered to the users mailbox will now to the program INSTEAD of the mailbox. If you want to recover the emails you'll need to piggy back your own delivery agent within your script or forward the message to another address. Also, you'll need to deal with bounces withing your script to avoid feedback loops.
C.
精彩评论