cPanel管道电子邮件到PHP脚本

时间:2011-08-05 03:09:41

标签: php email pipe cpanel forward

花了我几个小时,但我终于想出了如何使用cPanel X3将邮件传递给我的PHP脚本。

实际的解析脚本只是我设置的一个测试脚本,它会在执行时通过电子邮件发送给我。

#!/usr/local/bin/php -q
<?php
$headers = "From: test@email.com";
$to = "myemail@gmail.com";
$subject = "Recieved";
$body = "Message recieved.";
$mail = mail($to, $subject, $body, $headers);

上面的脚本正确执行并接收数据,即使它正在接收邮件,它也会发回以下内容:

This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

 pipe to |/home/PATH/TO/SCRIPT/parse.php
   generated by test@email.com

The following text was generated during the delivery attempt:

------ pipe to |/home/PATH/TO/SCRIPT/parse.ph
  generated by test@email.com ------

PHP Warning:  Module 'PDO' already loaded in Unknown on line 0
PHP Warning:  Module 'pdo_sqlite' already loaded in Unknown on line 0
PHP Warning:  Module 'SQLite' already loaded in Unknown on line 0
PHP Warning:  Module 'pdo_mysql' already loaded in Unknown on line 0

------ This is a copy of the message, including all the headers. ------

Return-path: <myemail@gmail.com>
Received: from mail-fx0-f44.google.com ([209.85.161.44])
       by my.server.com with esmtps (TLSv1:RC4-SHA:128)
       (Exim 4.69)
       (envelope-from <myemail@gmail.com>)
       id 1QpAdr-0008UY-MO
       for test@email.com; Thu, 04 Aug 2011 22:00:07 -0500
Received: by fxe6 with SMTP id 6so2641925fxe.3
       for <test@email.com>; Thu, 04 Aug 2011 20:00:04 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
       d=gmail.com; s=gamma;
       h=mime-version:from:date:message-id:subject:to:content-type;
       bh=gqxzpu6OEZTUs6uTT1G+NLaRvZh0HIOfcrOh1KtUuqw=;
       b=ICOQ1YpNQZKXxAB5DCguFui6aCSqg9wMDaj8S+1iuNkJQhGL8otqT8zRdRU8i+dngU
        +KjDbSPNLdt52PGLqbz4v48MKWUCeaTo/xwa4Pftix6d63x6yqwU4/Hy9ZG9dhNiVHYM
        goSQb+InqzTgw3msyWMsw75Mddwh/HK4I8fv0=
Received: by 10.204.151.216 with SMTP id d24mr532167bkw.304.1312513204131;
 Thu, 04 Aug 2011 20:00:04 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.204.23.196 with HTTP; Thu, 4 Aug 2011 19:59:44 -0700 (PDT)
From: myemail@gmail.com
Date: Thu, 4 Aug 2011 22:59:44 -0400
Message-ID: <CAHxz2PpNoNWRakeP2JoN8cdmPfz=HrYd5N2vZ4aqb9E_vLiUjw@mail.gmail.com>
Subject: Hahahahha
To: test@email.com
Content-Type: multipart/alternative; boundary=0015175dd9cc4b987d04a9b94b71

--0015175dd9cc4b987d04a9b94b71
Content-Type: text/plain; charset=ISO-8859-1

awda asd asdqwd a xzccz

--0015175dd9cc4b987d04a9b94b71
Content-Type: text/html; charset=ISO-8859-1

awda asd asdqwd a xzccz

--0015175dd9cc4b987d04a9b94b71--    

这是我在cPanel中使用的路径:

|php -q -n /PATH/TO/SCRIPT/parse.php

如果正在执行脚本,为什么我仍然从服务器收到错误?

编辑:我弄清楚问题是什么。我的php.ini文件设置不正确。以下是重复的:

extension=pdo.so
extension=pdo_sqlite.so
extension=sqlite.so
extension=pdo_mysql.so

3 个答案:

答案 0 :(得分:2)

“错误邮件”的原因是,如果执行的程序在stdoutstderr上写了一些内容(我不是shure),MTA认为存在错误 - 这就是php在发出警告时会这样做。

您可以尝试通过设置error_reporting来压制这些警告:

|php -n -d error_reporting=E_ERROR /PATH/TO/SCRIPT/parse.php

如果不起作用,请尝试消除所有输出:

|php -d display_errors=off -d display_startup_errors=off -d error_log=/dev/null -n /PATH/TO/SCRIPT/parse.php

但是这样你就不会看到任何可能不需要的语法错误等错误。

答案 1 :(得分:0)

我看到的唯一问题是$headers = "From: test@email.com";应为$headers = "From: test@email.com\r\n";

答案 2 :(得分:0)

  

上面的脚本正确执行并接收数据,即使它正在接收邮件

?您显示的脚本不会收到仅发送的电子邮件。

  

为什么我仍然会收到该错误

哪个错误?由于错误配置导致PHP错误 - 但这些并不妨碍脚本运行。这些可能会产生反向散射,具体取决于MDA。还有无法传递的消息 - 这是您的Exim配置的问题。

您的接收脚本也会在读取之前退出并关闭STDIN管道。真的,您应该阅读EOF并使用显式返回码退出(0表示成功)