2016-06-20 35 views

回答

0

David,

請按照下面的一些代碼來閱讀電子郵件。我相信可以幫助你:

/** 
* Get last UID on Emails table 
* 
* return integer 
*/ 
private function getLastUID() 
{ 
    // I have a model Email and I try to get the higher uid on database 
    return Email::max('uid'); 
} 

/** 
* Open Imap instance 
* 
* @return resource 
*/ 
private function startEmail() 
{ 
    return imap_open(env('IMAP'), env('IMAP_EMAIL'), env('IMAP_PASSWORD'), OP_READONLY); 
} 

/** 
* Get Emails from Imap instance 
* 
* @return array 
*/ 
private function getTodayEmails() 
{ 
    $mailbox = $this->startEmail(); 

    $today = Carbon::now()->format('j-M-Y'); 

    //I only search for todays emails, since I have a cron job that runs this task every hour (For my purpose I don't need to check it every minute) 
    $inbox = imap_search($mailbox,'SINCE '.$today); 

    /* If there is no email */ 
    if ($inbox === false) return false; 

    //Sort to insert the new email first 
    rsort($inbox); 

    $emails = []; 

    foreach($inbox as $box) { 
     /* get information specific to this email */ 
     $overview = imap_fetch_overview($mailbox, $box, 0); 
     $header = imap_headerinfo($mailbox , $box); 

     $uid = imap_uid($mailbox , $box); 

     // Here I check if the email $uid is already on my database, if no, I save it. If yes I break the conditional. 
     // I highly believe that you have to work on this conditional and in your Model. The rest is working well (at least for me! :)) 

     if ($uid > $this->getLastUID()) { 
      $emails[$box]['uid'] = $uid; 
      $emails[$box]['date'] = (isset($header->udate)) ? date('Y-m-d H:i:s', $header->udate) : null; 
      $emails[$box]['subject'] = (isset($overview[0]->subject)) ? $overview[0]->subject : null; 
      $emails[$box]['from'] = (isset($header->from[0])) ? $this->extractEmail($header->from[0]) : null; 
      $emails[$box]['from_name'] = (isset($header->from[0]->personal)) ? $header->from[0]->personal : null; 
      $emails[$box]['to'] = (isset($header->to[0])) ? $this->extractEmail($header->to[0]) : null; 
      $emails[$box]['to_name'] = (isset($header->to[0]->personal)) ? $header->to[0]->personal : null; 
      $emails[$box]['reply_to'] = (isset($header->reply_to[0])) ? $this->extractEmail($header->reply_to[0]) : null; 
      $emails[$box]['reply_name'] = (isset($header->reply_to[0]->personal)) ? $header->reply_to[0]->personal : null; 

      /* output the email body */ 
      $emails[$box]['message'] = $this->getBody($uid, $mailbox); 
     } else { 
      break; 
     } 

    imap_close($mailbox); 

    return $emails; 
} 

/** 
* Extract email from Imap Instance 
* 
* @param object $email 
* 
* @return bool|string 
*/ 
private function extractEmail($email) 
{ 
    if (isset($email->mailbox) && isset($email->host)) 
     return $email->mailbox.'@'.$email->host; 

    return false; 
} 


/** 
* Get body message 
* 
* @param integer $uid 
* @param Imap Instance $imap 
* 
* @return bool 
*/ 
private function getBody($uid, $imap) 
{ 
    $body = $this->getPart($imap, $uid, "TEXT/HTML"); 
    // if HTML body is empty, try getting text body 
    if ($body == "") { 
     $body = $this->getPart($imap, $uid, "TEXT/PLAIN"); 
    } 
    return $body; 
} 

/** 
* Treat body message of email 
* 
* @param Imap Instance $imap 
* @param integer $uid 
* @param string $mimetype 
* @param bool $structure 
* @param bool $partNumber 
* 
* @return bool|string 
*/ 
private function getPart($imap, $uid, $mimetype, $structure = false, $partNumber = false) 
{ 
    if (!$structure) { 
     $structure = imap_fetchstructure($imap, $uid, FT_UID); 
    } 
    if ($structure) { 
     if ($mimetype == $this->getMimeType($structure)) { 
      if (!$partNumber) { 
       $partNumber = 1; 
      } 
      $text = imap_fetchbody($imap, $uid, $partNumber, FT_UID); 
      switch ($structure->encoding) { 
       case 3: return imap_base64($text); 
       case 4: return imap_qprint($text); 
       default: return $text; 
      } 
     } 

     // multipart 
     if ($structure->type == 1) { 
      foreach ($structure->parts as $index => $subStruct) { 
       $prefix = ""; 
       if ($partNumber) { 
        $prefix = $partNumber . "."; 
       } 
       $data = $this->getPart($imap, $uid, $mimetype, $subStruct, $prefix . ($index + 1)); 
       if ($data) { 
        return $data; 
       } 
      } 
     } 
    } 
    return false; 
} 

/** 
* Get Mimetype of part 
* 
* @param $structure 
* 
* @return string 
*/ 
private function getMimeType($structure) 
{ 
    $primaryMimetype = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER"); 

    if ($structure->subtype) { 
     return $primaryMimetype[(int)$structure->type] . "/" . $structure->subtype; 
    } 
    return "TEXT/PLAIN"; 
} 

在.ENV您必須插入:

IMAP={imap.gmail.com:993/ssl/novalidate-cert}INBOX 
(or you can use {imap.gmail.com:993/imap/ssl}INBOX) 
IMAP_EMAIL=<Your GMAIL> 
IMAP_PASSWORD=<PASSWORD> 

雖然你可以使用一些庫,例如https://github.com/barbushin/php-imap。我真的相信用raw php很容易(http://php.net/manual/en/book.imap.php)。

你可以得到更多的信息:https://davidwalsh.name/gmail-php-imap(其中大部分的功能都可以,順便說一句)