How do I use Procmail with PHP?

只愿长相守 提交于 2020-01-07 06:52:17

问题


I'm trying to use procmail to send emails to a PHP script so the script will check a MySQL database and edit the subject line based on the sender email. I believe I've got a working procmail to do this:

:0:
* ^To:.*@barrett.com
! '/usr/local/bin/php-5.2 -f $HOME/ticket/emailcustcheck.php'

However, I'm not sure exactly how procmail executes the command. How does the email get passed to the PHP script, and therefore, how do I refer to it inside the script?


回答1:


The correct syntax for piping to a script is

:0   # no lock file
* ^To:.*@barrett\.com
| /usr/local/bin/php-5.2 -f $HOME/ticket/emailcustcheck.php  # no quotes, use pipe

The ! action would attempt to forward to an email address, but of course, the long quoted string with the path to your PHP interpreter is not a valid email address.

If you need locking (i.e. no two instances of this PHP script are allowed to run at the same time), you need to name a lock file; Procmail cannot infer a lock file name here, so the lock action you had would only produce an error message anyway. If you are uncertain, adding a named lock file is the safer bet, but if you don't have concurrency issues (such as, the script needs to write to a database while no other process is using the database) it should not be necessary, and could potentially slow down processing.

The condition regex also looks somewhat imprecise, but I can only speculate that you might want to trigger on Cc mail as well as direct To:. Look up the ^TO_ macro in the documentation if so.

The script gets the message as its standard input; it should probably read all input lines to an array, or split into two arrays so that everything before the first empty line goes into the "headers" array and the rest goes into the "body" array. Or perhaps PHP has some class which can read an email message into an object from standard input.




回答2:


:0 wf
* ^To:.*@barrett\.com
| /usr/local/bin/php-5.2 -f $HOME/ticket/emailcustcheck.php

The f tells procmail that you are going to filter the message ie change it. The w Wait for the filter or program to finish and check its exitcode.

If you want to work only on the body of the message you must add the flag b If you want to work only on the header of the message you must add the flag h



来源:https://stackoverflow.com/questions/13752809/how-do-i-use-procmail-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!