我正在建立一个照片共享网站(只是测试 PHP 的东西),我希望用户能够使用电子邮件提交照片。
例如,当用户发送带有图像附件和正文中的文本的电子邮件时,网站需要运行脚本。图像将被上传到服务器,并且将创建一个新的“照片帖子”,其中电子邮件正文中的文本是描述。
我的问题是,如何告诉我的服务器在收到电子邮件时自动运行脚本?
非常感谢任何/所有帮助。如果您想了解更多信息,请发表评论!
谢谢!-贾尔斯
如果您使用 cpanel,您可以通过管道将电子邮件发送到脚本,然后相应地处理电子邮件。您可以在电子邮件转发器下找到该选项。
If you are running the website on a UNIX server you have access to you can do this with procmail, sieve or similar mailtransport helpers. You would have to create a useraccount for the alias recepient as procmail is only invoked to process "real" users mail. Your .procmailrc would look something like this:
:0
*
| /usr/bin/php /path/to/your/script.php
And remember that procmail will pass it's info as arguments (and in the env variables).
The above scenario might not at all be possible for you, but if it is then I recommend taking a closer look at http://www.procmail.org/
你想使用cron。这是 Unix 定期运行脚本的标准方式,无需任何用户干预。创建您的脚本,确保它可以由服务器用户运行,然后安排它(完整的命令,例如,cron 中的 php myscript.php)。它不会在收到电子邮件时运行,但您可以经常运行该脚本,以使差异不会很明显。
Alternatively, you may be able to "pipe" the email directly to your PHP script. The process to do so will slightly differ depending on your email suite and/or server control panel software. You may be able to get "inspiration" from the Kayako manual at http://www.kayako.com/manuals/Kayako_SupportSuite_User_Manual_PDF.pdf (page 61 onwards) which shows how to setup email piping to the Kayako support helpdesk. You'll have to write the PHP file which reads in the file from STDIN yourself though.