doujie7886 2013-04-02 07:11
浏览 89
已采纳

在阅读带有POP3的电子邮件后,如何从PHP中提交Kayako Resolve API的票证?

UPDATED: Please read the answer(s) to this question before posting a new one! Thanks.

This is for work. (I work at Acenet, Inc - a webhost - so my code will be sanitized of vital logins & other API data). I need some help with a script I am writing:

<?php
/* lothbrok.php - abuse notification email-reader & ticket-injector script for Acenet Inc */

/*
 * 
 * "One script to rule them all,
 * One script to find them.
 * One script to bring them home,
 * And in the darkness, bind them."
 * 
 */

if ($_GET['worms'] != "food") {
    echo "Sorry, but you are not authorized to run LOTHBROK today! Goodbye!";
    exit;
} else {
require("email_functions.php");


global $timeout, $error, $buffer;

// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");

ob_start();

//-----------------------------------------------------------------------------
//                 Declarations
//-----------------------------------------------------------------------------

$error              = "";                       //    Error string.

$timeout            = 90;                       //    Default timeout before giving up on a network operation.

$Count              = -1;                       //    Mailbox msg count

$buffer             = 512;                      //    Socket buffer for socket fgets() calls, max per RFC 1939 the returned line a POP3 server can send is 512 bytes.

$server             = "SANITIZED";      //    Set this to hard code the server name

$RFC1939            = false;                    //    Set by noop(). See rfc1939.txt

$msg_list_array     = array();                  //    List of messages from server

$login              = 'SANITIZED';
$pass               = 'SANITIZED';

 //-----------------------------------------------------------------------------
 //                 The Code
 //-----------------------------------------------------------------------------
    echo "<h1>LOTHBROK</h1><h2>Starting Program</h2><br>";
         set_time_limit($timeout);

         $fp = connect ($server, $port = 110);

    $Count = login($login,$pass, $fp);

    if( (!$Count) or ($Count == -1) )
    {
         exit;
  }// end if
  if ($Count < 1)
  {
      die();
  } else {
      echo "Login OK: Inbox contains [$Count] messages<BR>
";
      $msg_list_array = uidl("", $fp);
      set_time_limit($timeout);
   }// end if

    // loop thru the array to get each message
   for ($i=1; $i <= $Count; $i++){
        set_time_limit($timeout);
        $MsgOne = top($i, empty($numLines), $fp);

        if( (!$MsgOne) or (gettype($MsgOne) != "array") )
        {
            echo "oops, Message not returned by the server.<BR>
";
            exit;
        }// end if
        /*
            call the function to read the message
            returns true if access, breakdown and insertion
            in to db are completed sucessfully
       */
       /*
        Function to read the message and extract :
            a. subject
            b. date
            c. split the body line by line
    */
       $body            = '';       // get the body of the message into 1 variable
       $subjects        = '';       // get the subject of the email
       $dates           = '';       // get the date of the email
       $body_start_key  = false;    // body starts at blank line, blank line is separator for from headers to body
       $TIRSFlag        = false;    // flag for seeing if the email is really to be processed by the app
       $base64Flag      = false;    // flag to handle base 64 encoding by email systems.



        foreach ($MsgOne as $key => $value)
    {
    if (trim($value) == "Content-Transfer-Encoding: base64"){
      $base64Flag = true;
    }//end if

    //get the subject line of the email
    preg_match("/^(Subject\:) (.*)?
$/i",$subjects,$subject);

      //look for "FBL" in the subject to see if we need to attempt to process the email
      if (strstr($subject,"FBL")) {
          $TIRSFlag = true;
        }// end if
    }// end if

    //get the date of the email
    preg_match("/(Date\:) ^([A-Za-z]{3}), ([0-9]{2}) ([A-Za-z]{3}) ([0-9]{4}) ([0-9]{2})\:([0-9]{2})\:([0-9]{2}) \-?([0-9]{4})$?
/i",$subjects,$dates);

    // Grab the message body
        $MsgOne = get($i, $fp);

    //the body
    if (strlen(trim($value))==0){
      if ($body_start_key == false){ $body_start_key = true; } //set the start key for the body
    }//end if

    if ($body_start_key == true){
      $body .= trim($value);
      if ($base64Flag == false){ $body .="<br />"; }
    }// end if

  }// end foreach

  // only create support ticket(s) if the subject line contains the acronym "FBL"
  if ($TIRSFlag == false){
        echo "<br>Nothing to read - finished.</b>";
        exit;
  }else{

      //decode the message if its base64 encoded
      if ($base64Flag == true) {
        $body = base64_decode($body);
      } //end if

  } 
         //convert the body crlf to <br> tags if not done when the message was built
         $body = nl2br($body);
         $body = preg_replace("/^(Please investigate these FBL reports accordingly and create a ticket for the client if need be.)$/i"," ",$body);
         $body = preg_replace("/^(Acenet) (AOL|Comcast|Yahoo!?|Hotmail) (FBL Bot)$/i"," ",$body);
 /* Cordoned off in case something doesn't work and this is then neeeded.....
            $dayn = substr($date,0,2);
            $day = substr($date,6,7);
            $monn = substr($date,9,11);
            $monn = substr($date,9,11);
            $year = substr($date,13,16);
            $fhrd = substr($date,18,19);
            $mind = substr($date,20,21);
            $secd = substr($date,22,23);
            $msed = substr($date,26,30);
 End of Quarantine! :D .... */
         while (preg_match("/(THIS IS AN AUTOMATED ABUSE ALERT - DO NOT REPLY)/i",$body) && $dates > strtotime($dates - 8)) {

        // Here goes the part to actually make things work!

 /* Yet another Quarantine section....
        preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/", $subject, $ipaddy);   // Client's first name in records (check WHMCS)
        preg_match("/^(AOL|Hotmail|Yahoo|Comcast|Facebook|Google|Gmail)$/", $subject, $mailnet);    // FBL Reporting Mailsys Network (Hotmail, Comcast, Yahoo!, Google, or AOL)
 Hello, back to the news! */

        $getArray["e"] = "/Tickets/Ticket";
        $apiUrl = "SANITIZED" . $getArray["e"];
        $apiKey = "SANITIZED";
        $salt =  mt_rand(1,99);
        $secretKey = "SANITIZED";
        $signature = base64_encode(hash_hmac('sha256',$salt,$secretKey,true));

        $subject = "Abuse Email Ticket (TESTING - DO NOT RESPOND!)";
        $fullname = "Ragnar Lothbrok";
        $email = "burningjunk@mailinator.com";
        $contents = "Hello anonymous Acenet user,

We have recently received an FBL (Feedback Loop) Report from an email provider indicating users have identified one or more messages originating from your account as spam. 

Acenet is setup to receive a notification any time email sent from our network arrives in the mailbox of another user at this provider and that email provider's user clicks \"This is Spam\" for that email. This is known as a Feedback Loop, we receive Feedback from external mail providers categorizing email which is sent from our network.

There are many reasons other users may be categorizing mail from your account as spam. The purpose of this notification is not to imply you are a spammer, but to factually inform you that a user has clicked \"This is Spam\" for email sent from your account. 

Along with the FBL, we receive details about the email itself including the Sender, Subject, and Header information. Please note that the exact recipient of the email is often removed by the mail provider as part of their privacy policy. You will often see <redacted> in place of the true recipient's email address.

The relevant information related to this FBL Report is included below. 

It's important to understand that we are not implying you are a spammer. But, it is our goal to assist you in determining why this report was generated for your emails. If these reports continue to generate, the aforementioned email provider may end up blacklisting your IP and prevent you from emailing those users. By working with our Technicians to address these FBL reports, we can help you to avoid such a situation.

If the email details provided below are unfamiliar to you, please update this ticket and our Technicians will be happy to review the contents and help interpret how this email may have originated from your account.


If you recognize the email details provided, you should review your mailing practices and attempt to identify why recipients are clicking \"This is Spam\" for these emails.
FBL Reporting Information follows:
-------------------------------------------------------------------------------------
$body


";
        $departmentid = "9";
        $ticketstatusid = "4";
        $ticketpriorityid = "9";
        $tickettypeid = "1";
        $staffid = "27";

        $post_data = array('subject' => $subject,
            'fullname' => $fullname,
            'email' => $email,
            'contents' => $contents,
            'departmentid' => $departmentid,
            'ticketstatusid' => $ticketstatusid,
            'ticketpriorityid' => $ticketpriorityid,
            'tickettypeid' => $tickettypeid,
            'staffid' => $staffid, 
            'apikey' => $apiKey, 
            'salt' => $salt, 
            'signature' => $signature);

        $post_data = http_build_query($post_data, '', '&');

        $curl = curl_init($apiUrl); 
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_URL, $apiUrl);
        curl_setopt($curl, CURLOPT_HEADER, true); 
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

        $response = curl_exec($curl);  
        curl_close($curl);


       }// end for loop



    //close the email box and delete all messages marked for deletion
    quit($fp);

    //close the application
    echo "<br>Finished</b>";
    exit;

    }
  /* Have a nice day! */

  ?>

Anyway, my issue is that I need to read only the previous 7 days' worth (in other words: a week's worth) of abuse email messages and submit them as support tickets within our firm's Kayako installation. The Kayako API I have down pat; the problem for me is how do I ONLY read the last week's worth of abuse notices and NOT all the notices in the entire email account (last 2 times I ran this script, it kept on going and going and going like the Energizer Bunny and I had to run a kill -9 [process ID] command on the server where the script & its process were located).

By the way, as for the "email-functions.php" script this script mentions: ignore it. It was written by other folks, not me, so I can't vouch for its erroneousness.

I can only admit to the faults in my own coding...

If anyone knows how to fix my issue, please leave a reply or comment, thank you!

  • 写回答

1条回答 默认 最新

  • doukenqiong0588 2013-04-02 17:37
    关注

    Easy answer:

    What I am doing is pointless. The way my firm handles the tickets that come in on abuse (actually, submits abuse tickets based on email notices) needs fixing before any of these PHP/Kayako REST API scripts become handy.

    The Long Answer:

    Our firm currently processes all abuse notices and submits them to the Kayako ticketing support system using human beings as the transfer method. However, our human-powered ticket submission (and client look-up using WHMCS) is flawed in that only humans could use it; were we to try to integrate (or automate) a robotic (automated) system into our current methods, it would, as Bill Gates (or someone else) once famously said, "magnify the inefficiencies" in our own current system methodology.

    Therefore, I have decided the script is a farce. It's not worth it; I plan to inform my boss of this by midnight tomorrow morning, and also present him with a plan to fix the current system. In the meantime I will be writing that business plan.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)