コード例 #1
3
 function getAttachments($id, $path)
 {
     $parts = imap_fetchstructure($this->mailbox, $id);
     $attachments = array();
     //FIXME if we do an is_array() here it breaks howver if we don't
     //we get foreach errors
     foreach ($parts->parts as $key => $value) {
         $encoding = $parts->parts[$key]->encoding;
         if ($parts->parts[$key]->ifdparameters) {
             $filename = $parts->parts[$key]->dparameters[0]->value;
             $message = imap_fetchbody($this->mailbox, $id, $key + 1);
             switch ($encoding) {
                 case 0:
                     $message = imap_8bit($message);
                 case 1:
                     $message = imap_8bit($message);
                 case 2:
                     $message = imap_binary($message);
                 case 3:
                     $message = imap_base64($message);
                 case 4:
                     $message = quoted_printable_decode($message);
                 case 5:
                 default:
                     $message = $message;
             }
             $fp = fopen($path . $filename, "w");
             fwrite($fp, $message);
             fclose($fp);
             $attachments[] = $filename;
         }
     }
     return $attachments;
 }
コード例 #2
0
function getdecodevalue($message,$coding) {
		switch($coding) {
			case 0:
			case 1:
				$message = imap_8bit($message);
				break;
			case 2:
				$message = imap_binary($message);
				break;
			case 3:
			case 5:
				$message=imap_base64($message);
				break;
			case 4:
				$message = imap_qprint($message);
				break;
		}
		return $message;
	}
コード例 #3
0
ファイル: class.mailfetch.php プロジェクト: iHunt101/OsTicket
 function decode($encoding, $text)
 {
     switch ($encoding) {
         case 1:
             $text = imap_8bit($text);
             break;
         case 2:
             $text = imap_binary($text);
             break;
         case 3:
             $text = imap_base64($text);
             break;
         case 4:
             $text = imap_qprint($text);
             break;
         case 5:
         default:
             $text = $text;
     }
     return $text;
 }
コード例 #4
0
ファイル: Get_Email.class.php プロジェクト: ohjack/newErp
 /**
  * get attach of the message
  *
  * @param string $msgCount
  * @param string $path
  * @return array
  */
 public function getAttach($msgCount, $path)
 {
     $struckture = imap_fetchstructure($this->_connect, $msgCount, FT_UID);
     print_r($struckture->parts);
     $attach = array();
     if ($struckture->parts) {
         foreach ($struckture->parts as $key => $value) {
             $encoding = $struckture->parts[$key]->encoding;
             if ($struckture->parts[$key]->ifdparameters) {
                 $name = $this->subjectDecode($struckture->parts[$key]->dparameters[0]->value);
                 $message = imap_fetchbody($this->_connect, $msgCount, $key + 1, FT_UID);
                 if ($encoding == 0) {
                     $message = imap_8bit($message);
                 } else {
                     if ($encoding == 1) {
                         $message = imap_8bit($message);
                     } else {
                         if ($encoding == 2) {
                             $message = imap_binary($message);
                         } else {
                             if ($encoding == 3) {
                                 $message = imap_base64($message);
                             } else {
                                 if ($encoding == 4) {
                                     $message = quoted_printable_decode($message);
                                 }
                             }
                         }
                     }
                 }
                 try {
                     $name = $this->subjectDecode($name);
                     $this->downAttach($path, $name, $message);
                     echo $name . "1\n";
                     $attach[] = $name;
                 } catch (Exception $e) {
                     echo '附件下载失败\\n';
                 }
             }
             if ($struckture->parts[$key]->parts) {
                 foreach ($struckture->parts[$key]->parts as $keyb => $valueb) {
                     $encoding = $struckture->parts[$key]->parts[$keyb]->encoding;
                     if ($struckture->parts[$key]->parts[$keyb]->ifdparameters) {
                         $name = $struckture->parts[$key]->parts[$keyb]->dparameters[0]->value;
                         $partnro = $key + 1 . "." . ($keyb + 1);
                         $message = imap_fetchbody($this->_connect, $msgCount, $partnro, FT_UID);
                         if ($encoding == 0) {
                             $message = imap_8bit($message);
                         } else {
                             if ($encoding == 1) {
                                 $message = imap_8bit($message);
                             } else {
                                 if ($encoding == 2) {
                                     $message = imap_binary($message);
                                 } else {
                                     if ($encoding == 3) {
                                         $message = imap_base64($message);
                                     } else {
                                         if ($encoding == 4) {
                                             $message = quoted_printable_decode($message);
                                         }
                                     }
                                 }
                             }
                         }
                         try {
                             $name = $this->subjectDecode($name);
                             $this->downAttach($path, $name, $message);
                             echo $name . "2\n";
                             $attach[] = $name;
                         } catch (Exception $e) {
                             echo "下载附件失败\n";
                         }
                     }
                 }
             }
         }
     }
     return $attach;
 }
コード例 #5
0
ファイル: Imap.php プロジェクト: kamaroly/Mail
 /**
  * Splits out body parts
  * ie. plain, HTML, attachment
  *
  * @param string $content The content to parse
  * @param array  $parts   The existing parts
  *
  * @return array
  */
 private function getParts($content, array $parts = array())
 {
     //separate the head and the body
     list($head, $body) = preg_split("/\n\\s*\n/", $content, 2);
     //front()->output($head);
     //get the headers
     $head = $this->getHeaders($head);
     //if content type is not set
     if (!isset($head['content-type'])) {
         return $parts;
     }
     //split the content type
     if (is_array($head['content-type'])) {
         $type = array($head['content-type'][1]);
         if (strpos($type[0], ';') !== false) {
             $type = explode(';', $type[0], 2);
         }
     } else {
         $type = explode(';', $head['content-type'], 2);
     }
     //see if there are any extra stuff
     $extra = array();
     if (count($type) == 2) {
         $extra = explode('; ', str_replace(array('"', "'"), '', trim($type[1])));
     }
     //the content type is the first part of this
     $type = trim($type[0]);
     //foreach extra
     foreach ($extra as $i => $attr) {
         //transform the extra array to a key value pair
         $attr = explode('=', $attr, 2);
         if (count($attr) > 1) {
             list($key, $value) = $attr;
             $extra[$key] = $value;
         }
         unset($extra[$i]);
     }
     //if a boundary is set
     if (isset($extra['boundary'])) {
         //split the body into sections
         $sections = explode('--' . str_replace(array('"', "'"), '', $extra['boundary']), $body);
         //we only want what's in the middle of these sections
         array_pop($sections);
         array_shift($sections);
         //foreach section
         foreach ($sections as $section) {
             //get the parts of that
             $parts = $this->getParts($section, $parts);
         }
     } else {
         //if name is set, it's an attachment
         //if encoding is set
         if (isset($head['content-transfer-encoding'])) {
             //the goal here is to make everytihg utf-8 standard
             if (is_array($head['content-transfer-encoding'])) {
                 $head['content-transfer-encoding'] = array_pop($head['content-transfer-encoding']);
             }
             switch (strtolower($head['content-transfer-encoding'])) {
                 case 'binary':
                     $body = imap_binary($body);
                     break;
                 case 'base64':
                     $body = base64_decode($body);
                     break;
                 case 'quoted-printable':
                     $body = quoted_printable_decode($body);
                     break;
                 case '7bit':
                     $body = mb_convert_encoding($body, 'UTF-8', 'ISO-2022-JP');
                     break;
                 default:
                     break;
             }
         }
         if (isset($extra['name'])) {
             //add to parts
             $parts['attachment'][$extra['name']][$type] = $body;
         } else {
             //it's just a regular body
             //add to parts
             $parts[$type] = $body;
         }
     }
     return $parts;
 }
コード例 #6
0
 public function DecodificaMensagem($Mensagem, $Codificacao)
 {
     switch ($Codificacao) {
         case 0:
         case 1:
             $Mensagem = imap_8bit($Mensagem);
             break;
         case 2:
             $Mensagem = imap_binary($Mensagem);
             break;
         case 3:
         case 5:
         case 6:
         case 7:
             $Mensagem = imap_base64($Mensagem);
             break;
         case 4:
             $Mensagem = imap_qprint($Mensagem);
             break;
     }
     return $Mensagem;
 }
コード例 #7
0
function getEmailAttachments($inbox, $email_number, $structure)
{
    $attachments = array();
    // Tomar adjuntos de e-mail
    if (isset($structure->parts) && count($structure->parts)) {
        for ($i = 0; $i < count($structure->parts); $i++) {
            $is_attachment = false;
            $filename = "";
            $name = "";
            $attachment = "";
            if ($structure->parts[$i]->ifdparameters) {
                foreach ($structure->parts[$i]->dparameters as $object) {
                    if (strtolower($object->attribute) == 'name') {
                        $is_attachment = true;
                        $filename = $object->value;
                    }
                }
            }
            if ($structure->parts[$i]->ifparameters) {
                foreach ($structure->parts[$i]->parameters as $object) {
                    if (strtolower($object->attribute) == 'name') {
                        $is_attachment = true;
                        $filename = $object->value;
                    }
                }
            }
            if ($is_attachment) {
                $attachment = imap_fetchbody($inbox, $email_number, $i + 1);
                switch ($structure->parts[$i]->encoding) {
                    case 0:
                    case 1:
                        $attachment = imap_8bit($attachment);
                        break;
                    case 2:
                        $attachment = imap_binary($attachment);
                        break;
                    case 3:
                    case 5:
                        $attachment = imap_base64($attachment);
                        break;
                    case 4:
                        $attachment = imap_qprint($attachment);
                        break;
                }
                $attachments[$i] = array('is_attachment' => $is_attachment, 'filename' => $filename, 'name' => $name, 'attachment' => $attachment);
            }
        }
    }
    // Procesar adjuntos y formatear arreglo
    $tmpAttachments = array();
    foreach ($attachments as $attachment) {
        mb_internal_encoding('UTF-8');
        $attachment["filename"] = str_replace("_", " ", mb_decode_mimeheader($attachment["filename"]));
        $attachmentArray = array();
        $tmpFilenameArray = explode(".", $attachment["filename"]);
        $basename = $tmpFilenameArray[0];
        $extension = end($tmpFilenameArray);
        $attachmentArray["basename"] = $basename;
        switch (strtolower($extension)) {
            case 'xml':
                $attachmentArray["extension"] = "xml";
                $attachmentArray["attachment"] = $attachment["attachment"];
                $tmpAttachments[] = $attachmentArray;
                break;
            case 'zip':
                $zipArray = processZipFile($attachment["attachment"]);
                foreach ($zipArray as $zippedFile) {
                    $attachmentArray = array();
                    $attachmentArray["basename"] = $zippedFile["basename"];
                    $attachmentArray["extension"] = $zippedFile["extension"];
                    $attachmentArray["attachment"] = $zippedFile["attachment"];
                    $tmpAttachments[] = $attachmentArray;
                }
                break;
            default:
                $attachmentArray["extension"] = $extension;
                $attachmentArray["attachment"] = $attachment["attachment"];
                $tmpAttachments[] = $attachmentArray;
                break;
        }
    }
    // Armar arreglo final (juntar xml con su pdf respectivo)
    $returnArray = array();
    foreach ($tmpAttachments as $attachment) {
        $tmpArray = array();
        if (strtolower($attachment["extension"]) == "xml") {
            $tmpArray = array("xml_basename" => $attachment["basename"], "xml_extension" => $attachment["extension"], "xml_attachment" => $attachment["attachment"], "pdf_basename" => "", "pdf_extension" => "", "pdf_attachment" => "");
            foreach ($tmpAttachments as $pdfAttachment) {
                if ((strtolower($pdfAttachment["extension"]) == "pdf" || strtolower($pdfAttachment["extension"]) == "tiff") && $pdfAttachment["basename"] == $attachment["basename"]) {
                    $tmpArray["pdf_basename"] = $pdfAttachment["basename"];
                    $tmpArray["pdf_extension"] = $pdfAttachment["extension"];
                    $tmpArray["pdf_attachment"] = $pdfAttachment["attachment"];
                }
            }
            $returnArray[] = $tmpArray;
        }
    }
    return $returnArray;
}
コード例 #8
0
 /**
  * Private function : Recursivly get attached documents
  *
  * @param $mid          message id
  * @param $path         temporary path
  * @param $maxsize      of document to be retrieved
  * @param $structure    of the message or part
  * @param $part         part for recursive
  *
  * Result is stored in $this->files
  **/
 function getRecursiveAttached($mid, $path, $maxsize, $structure, $part = "")
 {
     if ($structure->type == 1) {
         // multipart
         reset($structure->parts);
         while (list($index, $sub) = each($structure->parts)) {
             $this->getRecursiveAttached($mid, $path, $maxsize, $sub, $part ? $part . "." . ($index + 1) : $index + 1);
         }
     } else {
         $filename = '';
         if ($structure->ifdparameters) {
             // get filename of attachment if present
             // if there are any dparameters present in this part
             if (count($structure->dparameters) > 0) {
                 foreach ($structure->dparameters as $dparam) {
                     if (Toolbox::strtoupper($dparam->attribute) == 'NAME' || Toolbox::strtoupper($dparam->attribute) == 'FILENAME') {
                         $filename = $dparam->value;
                     }
                 }
             }
         }
         //if no filename found
         if (empty($filename) && $structure->ifparameters) {
             // if there are any parameters present in this part
             if (count($structure->parameters) > 0) {
                 foreach ($structure->parameters as $param) {
                     if (Toolbox::strtoupper($param->attribute) == 'NAME' || Toolbox::strtoupper($param->attribute) == 'FILENAME') {
                         $filename = $param->value;
                     }
                 }
             }
         }
         if (empty($filename) && $structure->type == 5 && $structure->subtype) {
             // Embeded image come without filename - generate trivial one
             $filename = "image_{$part}." . $structure->subtype;
         }
         // if no filename found, ignore this part
         if (empty($filename)) {
             return false;
         }
         //try to avoid conflict between inline image and attachment
         $i = 2;
         while (in_array($filename, $this->files)) {
             //replace filename with name_(num).EXT by name_(num+1).EXT
             $new_filename = preg_replace("/(.*)_([0-9])*(\\.[a-zA-Z0-9]*)\$/", "\$1_" . $i . "\$3", $filename);
             if ($new_filename !== $filename) {
                 $filename = $new_filename;
             } else {
                 //the previous regex didn't found _num pattern, so add it with this one
                 $filename = preg_replace("/(.*)(\\.[a-zA-Z0-9]*)\$/", "\$1_" . $i . "\$2", $filename);
             }
             $i++;
         }
         $filename = $this->decodeMimeString($filename);
         if ($structure->bytes > $maxsize) {
             $this->addtobody .= "\n\n" . sprintf(__('%1$s: %2$s'), __('Too large attached file'), sprintf(__('%1$s (%2$s)'), $filename, Toolbox::getSize($structure->bytes)));
             return false;
         }
         if (!Document::isValidDoc($filename)) {
             //TRANS: %1$s is the filename and %2$s its mime type
             $this->addtobody .= "\n\n" . sprintf(__('%1$s: %2$s'), __('Invalid attached file'), sprintf(__('%1$s (%2$s)'), $filename, $this->get_mime_type($structure)));
             return false;
         }
         if ($message = imap_fetchbody($this->marubox, $mid, $part)) {
             switch ($structure->encoding) {
                 case 1:
                     $message = imap_8bit($message);
                     break;
                 case 2:
                     $message = imap_binary($message);
                     break;
                 case 3:
                     $message = imap_base64($message);
                     break;
                 case 4:
                     $message = quoted_printable_decode($message);
                     break;
             }
             if (file_put_contents($path . $filename, $message)) {
                 $this->files[$filename] = $filename;
                 // If embeded image, we add a tag
                 if ($structure->type == 5 && $structure->subtype) {
                     end($this->files);
                     $tag = Rule::getUuid();
                     $this->tags[$filename] = $tag;
                     // Link file based on id
                     if (isset($structure->id)) {
                         $clean = array('<' => '', '>' => '');
                         $this->altfiles[strtr($structure->id, $clean)] = $filename;
                     }
                 }
             }
         }
         // fetchbody
     }
     // Single part
 }
コード例 #9
0
 function decode($text, $encoding)
 {
     switch ($encoding) {
         case 1:
             $text = imap_8bit($text);
             break;
         case 2:
             $text = imap_binary($text);
             break;
         case 3:
             // imap_base64 implies strict mode. If it refuses to decode the
             // data, then fallback to base64_decode in non-strict mode
             $text = ($conv = imap_base64($text)) ? $conv : base64_decode($text);
             break;
         case 4:
             $text = imap_qprint($text);
             break;
     }
     return $text;
 }
コード例 #10
0
ファイル: receve.class.php プロジェクト: zqstudio2015/smeoa
 /**
  * get attach of the message
  *
  * @param string $msg_count
  * @param string $path
  * @return array
  */
 public function get_attach($msg_count, $path)
 {
     if (!$this->_connect) {
         return false;
     }
     $struckture = imap_fetchstructure($this->_connect, $msg_count);
     $ar = "";
     if ($struckture->parts) {
         foreach ($struckture->parts as $key => $value) {
             $enc = $struckture->parts[$key]->encoding;
             $subtype = $struckture->parts[$key]->subtype;
             $text_type = array("PLAIN", "HTML", "ALTERNATIVE");
             if (in_array($subtype, $text_type)) {
                 continue;
             }
             if ($struckture->parts[$key]->ifdparameters) {
                 $name = $this->mail_decode($struckture->parts[$key]->dparameters[0]->value);
                 $cid = $struckture->parts[$key]->id;
                 $cid = substr($cid, 1, strlen($cid) - 2);
                 $disposition = $struckture->parts[$key]->disposition;
                 if (empty($disposition)) {
                     $disposition = "INLINE";
                 }
                 $name = $cid . "_" . $disposition . "_" . $name;
                 $message = imap_fetchbody($this->_connect, $msg_count, $key + 1);
                 if ($enc == 0) {
                     $message = imap_8bit($message);
                 }
                 if ($enc == 1) {
                     $message = imap_8bit($message);
                 }
                 if ($enc == 2) {
                     $message = imap_binary($message);
                 }
                 if ($enc == 3) {
                     $message = imap_base64($message);
                 }
                 if ($enc == 4) {
                     $message = quoted_printable_decode($message);
                 }
                 if ($enc == 5) {
                     $message = $message;
                 }
                 $fp = fopen($path . urlencode($name), "w");
                 fwrite($fp, $message);
                 fclose($fp);
                 $ar = $ar . $name . ",";
             }
             if ($struckture->parts[$key]->ifparameters && $struckture->parts[$key]->ifdparameters == 0) {
                 if ($struckture->parts[$key]->parameters[0]->attribute == "NAME") {
                     $name = $this->mail_decode($struckture->parts[$key]->parameters[0]->value);
                 }
                 if ($struckture->parts[$key]->parameters[1]->attribute == "NAME") {
                     $name = $this->mail_decode($struckture->parts[$key]->parameters[1]->value);
                 }
                 $cid = $struckture->parts[$key]->id;
                 $cid = substr($cid, 1, strlen($cid) - 2);
                 $disposition = $struckture->parts[$key]->disposition;
                 if (empty($disposition)) {
                     $disposition = "INLINE";
                 }
                 $name = $cid . "_" . $disposition . "_" . $name;
                 $message = imap_fetchbody($this->_connect, $msg_count, $key + 1);
                 if ($enc == 0) {
                     $message = imap_8bit($message);
                 }
                 if ($enc == 1) {
                     $message = imap_8bit($message);
                 }
                 if ($enc == 2) {
                     $message = imap_binary($message);
                 }
                 if ($enc == 3) {
                     $message = imap_base64($message);
                 }
                 if ($enc == 4) {
                     $message = quoted_printable_decode($message);
                 }
                 if ($enc == 5) {
                     $message = $message;
                 }
                 $fp = fopen($path . urlencode($name), "w");
                 fwrite($fp, $message);
                 fclose($fp);
                 $ar = $ar . $name . ",";
             }
             if ($struckture->parts[$key]->parts) {
                 foreach ($struckture->parts[$key]->parts as $keyb => $valueb) {
                     $enc = $struckture->parts[$key]->parts[$keyb]->encoding;
                     if ($struckture->parts[$key]->parts[$keyb]->ifdparameters) {
                         $name = $this->mail_decode($struckture->parts[$key]->parts[$keyb]->dparameters[0]->value);
                         $id = $struckture->parts[$key]->parts[$keyb]->id;
                         $disposition = $struckture->parts[$key]->parts[$keyb]->disposition;
                         $name = $id . "_" . $disposition . "_" . $name;
                         $partnro = $key + 1 . "." . ($keyb + 1);
                         $message = imap_fetchbody($this->_connect, $msg_count, $partnro);
                         if ($enc == 0) {
                             $message = imap_8bit($message);
                         }
                         if ($enc == 1) {
                             $message = imap_8bit($message);
                         }
                         if ($enc == 2) {
                             $message = imap_binary($message);
                         }
                         if ($enc == 3) {
                             $message = imap_base64($message);
                         }
                         if ($enc == 4) {
                             $message = quoted_printable_decode($message);
                         }
                         if ($enc == 5) {
                             $message = $message;
                         }
                         $fp = fopen($path . urlencode($name), "w");
                         fwrite($fp, $message);
                         fclose($fp);
                         $ar = $ar . $name . ",";
                     }
                 }
             }
         }
     }
     $ar = substr($ar, 0, strlen($ar) - 1);
     return $ar;
 }
コード例 #11
0
ファイル: GetMail.php プロジェクト: paulbunyannet/mail
 /**
  * Get Attached File from Mail
  *
  * @param int $mid message id
  * @param string $path attachment storage path
  *
  * @param string $separator list separator for attachment names
  *
  * @return string
  */
 public function getAttachment($mid, $path, $separator = ',')
 {
     if (!$this->getMailBox()) {
         return false;
     }
     $structure = imap_fetchstructure($this->getMailBox(), $mid);
     $attachments = "";
     if (property_exists($structure, 'parts') && $structure->parts) {
         foreach (array_keys($structure->parts) as $key) {
             $enc = $structure->parts[$key]->encoding;
             if ($structure->parts[$key]->ifdparameters) {
                 $name = $structure->parts[$key]->dparameters[0]->value;
                 $message = imap_fetchbody($this->getMailBox(), $mid, $key + 1);
                 switch ($enc) {
                     case 0:
                         $message = imap_8bit($message);
                         break;
                     case 1:
                         $message = imap_8bit($message);
                         break;
                     case 2:
                         $message = imap_binary($message);
                         break;
                     case 3:
                         $message = imap_base64($message);
                         break;
                     case 4:
                         $message = quoted_printable_decode($message);
                         break;
                 }
                 $fp = fopen($path . $name, "w");
                 fwrite($fp, $message);
                 fclose($fp);
                 $attachments .= $name . $separator;
             }
             // Support for embedded attachments starts here
             if (property_exists($structure->parts[$key], 'parts')) {
                 foreach (array_keys($structure->parts[$key]->parts) as $keyB) {
                     $enc = $structure->parts[$key]->parts[$keyB]->encoding;
                     if ($structure->parts[$key]->parts[$keyB]->ifdparameters) {
                         $name = $structure->parts[$key]->parts[$keyB]->dparameters[0]->value;
                         $partNum = $key + 1 . "." . ($keyB + 1);
                         $message = imap_fetchbody($this->getMailBox(), $mid, $partNum);
                         switch ($enc) {
                             case 0:
                                 $message = imap_8bit($message);
                                 break;
                             case 1:
                                 $message = imap_8bit($message);
                                 break;
                             case 2:
                                 $message = imap_binary($message);
                                 break;
                             case 3:
                                 $message = imap_base64($message);
                                 break;
                             case 4:
                                 $message = quoted_printable_decode($message);
                                 break;
                         }
                         $fp = fopen($path . $name, "w");
                         fwrite($fp, $message);
                         fclose($fp);
                         $attachments .= $name . $separator;
                     }
                 }
             }
         }
     }
     /** Catch embedded images  */
     $embedded = $this->getEmbeddedImages(array('mid' => $mid, 'path' => $path, 'separator' => $separator));
     if ($embedded) {
         $attachments = $attachments . $embedded;
     }
     $attachments = substr($attachments, 0, strlen($attachments) - strlen($separator));
     return $attachments;
 }
コード例 #12
0
ファイル: receivemail.class.php プロジェクト: tmlsoft/main
 function GetAttach($mid, $path)
 {
     if (!$this->marubox) {
         return false;
     }
     $struckture = imap_fetchstructure($this->marubox, $mid);
     $ar = "";
     if ($struckture->parts) {
         foreach ($struckture->parts as $key => $value) {
             $enc = $struckture->parts[$key]->encoding;
             if ($struckture->parts[$key]->ifdparameters) {
                 $name = $struckture->parts[$key]->dparameters[0]->value;
                 $message = imap_fetchbody($this->marubox, $mid, $key + 1);
                 switch ($enc) {
                     case 0:
                         $message = imap_8bit($message);
                         break;
                     case 1:
                         $message = imap_8bit($message);
                         break;
                     case 2:
                         $message = imap_binary($message);
                         break;
                     case 3:
                         $message = imap_base64($message);
                         break;
                     case 4:
                         $message = quoted_printable_decode($message);
                         break;
                     case 5:
                         $message = $message;
                         break;
                 }
                 $fp = fopen($path . $name, "w");
                 fwrite($fp, $message);
                 fclose($fp);
                 $ar = $ar . $name . ",";
             }
             // Support for embedded attachments starts here
             if ($struckture->parts[$key]->parts) {
                 foreach ($struckture->parts[$key]->parts as $keyb => $valueb) {
                     $enc = $struckture->parts[$key]->parts[$keyb]->encoding;
                     if ($struckture->parts[$key]->parts[$keyb]->ifdparameters) {
                         $name = $struckture->parts[$key]->parts[$keyb]->dparameters[0]->value;
                         $partnro = $key + 1 . "." . ($keyb + 1);
                         $message = imap_fetchbody($this->marubox, $mid, $partnro);
                         switch ($enc) {
                             case 0:
                                 $message = imap_8bit($message);
                                 break;
                             case 1:
                                 $message = imap_8bit($message);
                                 break;
                             case 2:
                                 $message = imap_binary($message);
                                 break;
                             case 3:
                                 $message = imap_base64($message);
                                 break;
                             case 4:
                                 $message = quoted_printable_decode($message);
                                 break;
                             case 5:
                                 $message = $message;
                                 break;
                         }
                         $fp = fopen($path . $name, "w");
                         fwrite($fp, $message);
                         fclose($fp);
                         $ar = $ar . $name . ",";
                     }
                 }
             }
         }
     }
     $ar = substr($ar, 0, strlen($ar) - 1);
     return $ar;
 }
コード例 #13
0
ファイル: Mailbox.php プロジェクト: Yame-/mautic
 /**
  * @param Message      $mail
  * @param              $partStructure
  * @param              $partNum
  * @param bool|true    $markAsSeen
  * @param bool|false   $isDsn
  */
 protected function initMailPart(Message $mail, $partStructure, $partNum, $markAsSeen = true, $isDsn = false)
 {
     $options = FT_UID;
     if (!$markAsSeen) {
         $options |= FT_PEEK;
     }
     $data = $partNum ? imap_fetchbody($this->getImapStream(), $mail->id, $partNum, $options) : imap_body($this->getImapStream(), $mail->id, $options);
     if ($partStructure->encoding == 1) {
         $data = imap_utf8($data);
     } elseif ($partStructure->encoding == 2) {
         $data = imap_binary($data);
     } elseif ($partStructure->encoding == 3) {
         $data = imap_base64($data);
     } elseif ($partStructure->encoding == 4) {
         $data = quoted_printable_decode($data);
     }
     $params = $this->getParameters($partStructure);
     // attachments
     $attachmentId = $partStructure->ifid ? trim($partStructure->id, " <>") : (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null);
     if ($attachmentId) {
         if (empty($params['filename']) && empty($params['name'])) {
             $fileName = $attachmentId . '.' . strtolower($partStructure->subtype);
         } else {
             $fileName = !empty($params['filename']) ? $params['filename'] : $params['name'];
             $fileName = $this->decodeMimeStr($fileName, $this->serverEncoding);
             $fileName = $this->decodeRFC2231($fileName, $this->serverEncoding);
         }
         $attachment = new Attachment();
         $attachment->id = $attachmentId;
         $attachment->name = $fileName;
         if ($this->attachmentsDir) {
             $replace = ['/\\s/' => '_', '/[^0-9a-zа-яіїє_\\.]/iu' => '', '/_+/' => '_', '/(^_)|(_$)/' => ''];
             $fileSysName = preg_replace('~[\\\\/]~', '', $mail->id . '_' . $attachmentId . '_' . preg_replace(array_keys($replace), $replace, $fileName));
             $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . $fileSysName;
             file_put_contents($attachment->filePath, $data);
         }
         $mail->addAttachment($attachment);
     } else {
         if (!empty($params['charset'])) {
             $data = $this->convertStringEncoding($data, $params['charset'], $this->serverEncoding);
         }
         if ($partStructure->type == 0 && $data) {
             if (strtolower($partStructure->subtype) == 'plain') {
                 $mail->textPlain .= $data;
             } else {
                 $mail->textHtml .= $data;
             }
         } elseif ($partStructure->type == 1 && $partStructure->ifsubtype && (strtolower($partStructure->subtype) == 'report' && isset($params['REPORT-TYPE']) && strtolower($params['REPORT-TYPE']) == 'delivery-status')) {
             $mail->dsnMessage = trim($data);
             $isDsn = true;
         } elseif ($partStructure->type == 2 && $data) {
             if ($isDsn || strtolower($partStructure->subtype) == 'delivery-status') {
                 $mail->dsnReport = $data;
             } else {
                 $mail->textPlain .= trim($data);
             }
         }
     }
     if (!empty($partStructure->parts)) {
         foreach ($partStructure->parts as $subPartNum => $subPartStructure) {
             if ($partStructure->type == 2 && $partStructure->subtype == 'RFC822') {
                 $this->initMailPart($mail, $subPartStructure, $partNum, $markAsSeen, $isDsn);
             } else {
                 $this->initMailPart($mail, $subPartStructure, $partNum . '.' . ($subPartNum + 1), $markAsSeen, $isDsn);
             }
         }
     }
 }
コード例 #14
0
ファイル: Imap.php プロジェクト: radub/php-imap-client
 /**
  * return content of messages attachment
  *
  * @return binary attachment
  * @param $id of the message
  * @param $index of the attachment (default: first attachment)
  */
 public function getAttachment($id, $index = 0)
 {
     // find message
     $attachments = false;
     $messageIndex = imap_msgno($this->imap, $id);
     $header = imap_headerinfo($this->imap, $messageIndex);
     $mailStruct = imap_fetchstructure($this->imap, $messageIndex);
     $attachments = $this->getAttachments($this->imap, $messageIndex, $mailStruct, "");
     if ($attachments == false) {
         return false;
     }
     // find attachment
     if ($index > count($attachments)) {
         return false;
     }
     $attachment = $attachments[$index];
     // get attachment body
     $partStruct = imap_bodystruct($this->imap, imap_msgno($this->imap, $id), $attachment['partNum']);
     $filename = $partStruct->dparameters[0]->value;
     $message = imap_fetchbody($this->imap, $id, $attachment['partNum'], FT_UID);
     switch ($attachment['enc']) {
         case 0:
         case 1:
             $message = imap_8bit($message);
             break;
         case 2:
             $message = imap_binary($message);
             break;
         case 3:
             $message = imap_base64($message);
             break;
         case 4:
             $message = quoted_printable_decode($message);
             break;
     }
     return array("name" => $attachment['name'], "size" => $attachment['size'], "content" => $message);
 }
コード例 #15
0
ファイル: Imap.php プロジェクト: strong2much/yii2-imap
 /**
  * Function to decode a part of the message
  * @param string $msgPart the message part to decode
  * @param string $encoding the encoding used
  * @return bool|string
  */
 protected function decodeValue($msgPart, $encoding)
 {
     switch ($encoding) {
         case ENC7BIT:
         case ENC8BIT:
             $msgPart = imap_8bit($msgPart);
             break;
         case ENCBINARY:
             $msgPart = imap_binary($msgPart);
             break;
         case ENCBASE64:
         case ENCOTHER:
             $msgPart = imap_base64($msgPart);
             break;
         case ENCQUOTEDPRINTABLE:
             $msgPart = imap_qprint($msgPart);
             break;
         default:
             return false;
     }
     return $msgPart;
 }
コード例 #16
0
ファイル: mailcollector.class.php プロジェクト: stweil/glpi
 /**
  * Summary of getDecodedFetchbody
  * used to get decoded part from email
  *
  * @since version 0.90.2
  * @param $structure
  * @param $mid
  * @param $part
  *
  * @return bool|string
  **/
 private function getDecodedFetchbody($structure, $mid, $part)
 {
     if ($message = imap_fetchbody($this->marubox, $mid, $part)) {
         switch ($structure->encoding) {
             case 1:
                 $message = imap_8bit($message);
                 break;
             case 2:
                 $message = imap_binary($message);
                 break;
             case 3:
                 $message = imap_base64($message);
                 break;
             case 4:
                 $message = quoted_printable_decode($message);
                 break;
         }
         return $message;
     }
     return false;
 }
コード例 #17
0
 function getdecodevalue($message, $coding)
 {
     if ($coding == 0) {
         $message = imap_8bit($message);
     } elseif ($coding == 1) {
         $message = imap_8bit($message);
     } elseif ($coding == 2) {
         $message = imap_binary($message);
     } elseif ($coding == 3) {
         $message = imap_base64($message);
     } elseif ($coding == 4) {
         $message = imap_qprint($message);
     } elseif ($coding == 5) {
         $message = imap_base64($message);
     }
     return $message;
 }
コード例 #18
0
ファイル: bounce.php プロジェクト: selectSIFISO/.comsite
 function _decodeContent($content, $structure)
 {
     $encoding = $structure->encoding;
     //First we decode the content properly
     if ($encoding == 2) {
         $content = imap_binary($content);
     } elseif ($encoding == 3) {
         $content = imap_base64($content);
     } elseif ($encoding == 4) {
         $content = imap_qprint($content);
     }
     //Other cases??
     //added for a client who had issue when message was base64
     if (base64_decode($content, true) !== FALSE) {
         $content = base64_decode($content);
     }
     //Now we convert into utf-8!
     //$charset = $this->_getMailParam($structure,'charset');
     // removes attachment to prevent bounce handling timeout
     // 100 000 characters is plenty
     return substr($content, 0, 100000);
 }
コード例 #19
0
ファイル: Email.php プロジェクト: lovecheng/brs-demo2
 /**
  * 获取邮件附件
  * @param $mid 邮件id
  * @param $path 附件保存路径
  * @return string 返回附件名称
  */
 function GetAttach($mid, $path)
 {
     if (!$this->marubox) {
         return false;
     }
     $struckture = imap_fetchstructure($this->marubox, $mid);
     $ar = '';
     if ($struckture->parts) {
         foreach ($struckture->parts as $key => $value) {
             $enc = $struckture->parts[$key]->encoding;
             if ($struckture->parts[$key]->subtype == 'OCTET-STREAM') {
                 $imap_mime_header_decode = imap_mime_header_decode($struckture->parts[$key]->parameters[0]->value);
                 $name = $imap_mime_header_decode[0]->text;
                 $message = imap_fetchbody($this->marubox, $mid, $key + 1);
                 switch ($enc) {
                     case 0:
                         $message = imap_8bit($message);
                         break;
                     case 1:
                         $message = imap_8bit($message);
                         break;
                     case 2:
                         $message = imap_binary($message);
                         break;
                     case 3:
                         $message = imap_base64($message);
                         break;
                     case 4:
                         $message = quoted_printable_decode($message);
                         break;
                     case 5:
                         $message = $message;
                         break;
                 }
                 $file_name = time() . '_' . rand(100, 10000) . '_' . $name;
                 $fp = fopen($path . $file_name, "w");
                 fwrite($fp, $message);
                 fclose($fp);
                 $ar .= $file_name . ',';
             }
         }
     }
     // 返回附件名 [时间戳_随机数_原始名称]
     return trim($ar, ',');
 }
コード例 #20
0
ファイル: ImapSource.php プロジェクト: Jony01/LLD
 /**
  * @see http://www.nerdydork.com/download-pop3imap-email-attachments-with-php.html
  */
 protected function _decodeString($message, $coding)
 {
     switch ($coding) {
         case 0:
         case 1:
             return imap_8bit($message);
         case 2:
             return imap_binary($message);
         case 3:
             return imap_base64($message);
         case 4:
             return imap_qprint($message);
         default:
             // plain
             return $message;
     }
 }
コード例 #21
0
 protected function initMailPart(IncomingMail $mail, $partStructure, $partNum)
 {
     $data = $partNum ? imap_fetchbody($this->getImapStream(), $mail->id, $partNum, FT_UID) : imap_body($this->getImapStream(), $mail->id, FT_UID);
     if ($partStructure->encoding == 1) {
         $data = imap_utf8($data);
     } elseif ($partStructure->encoding == 2) {
         $data = imap_binary($data);
     } elseif ($partStructure->encoding == 3) {
         $data = imap_base64($data);
     } elseif ($partStructure->encoding == 4) {
         $data = imap_qprint($data);
     }
     $params = array();
     if (!empty($partStructure->parameters)) {
         foreach ($partStructure->parameters as $param) {
             $params[strtolower($param->attribute)] = $param->value;
         }
     }
     if (!empty($partStructure->dparameters)) {
         foreach ($partStructure->dparameters as $param) {
             $paramName = strtolower(preg_match('~^(.*?)\\*~', $param->attribute, $matches) ? $matches[1] : $param->attribute);
             if (isset($params[$paramName])) {
                 $params[$paramName] .= $param->value;
             } else {
                 $params[$paramName] = $param->value;
             }
         }
     }
     if (!empty($params['charset'])) {
         $data = iconv(strtoupper($params['charset']), $this->serverEncoding . '//IGNORE', $data);
     }
     // attachments
     $attachmentId = $partStructure->ifid ? trim($partStructure->id, " <>") : (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null);
     if ($attachmentId) {
         if (empty($params['filename']) && empty($params['name'])) {
             $fileName = $attachmentId . '.' . strtolower($partStructure->subtype);
         } else {
             $fileName = !empty($params['filename']) ? $params['filename'] : $params['name'];
             $fileName = $this->decodeMimeStr($fileName, $this->serverEncoding);
             $fileName = $this->decodeRFC2231($fileName, $this->serverEncoding);
         }
         $attachment = new IncomingMailAttachment();
         $attachment->id = $attachmentId;
         $attachment->name = $fileName;
         if ($this->attachmentsDir) {
             $replace = array('/\\s/' => '_', '/[^0-9a-zA-Z_\\.]/' => '', '/_+/' => '_', '/(^_)|(_$)/' => '');
             $fileSysName = preg_replace('~[\\\\/]~', '', $mail->id . '_' . $attachmentId . '_' . preg_replace(array_keys($replace), $replace, $fileName));
             $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . $fileSysName;
             file_put_contents($attachment->filePath, $data);
         }
         $mail->addAttachment($attachment);
     } elseif ($partStructure->type == 0 && $data) {
         if (strtolower($partStructure->subtype) == 'plain') {
             $mail->textPlain .= $data;
         } else {
             $mail->textHtml .= $data;
         }
     } elseif ($partStructure->type == 2 && $data) {
         $mail->textPlain .= trim($data);
     }
     if (!empty($partStructure->parts)) {
         foreach ($partStructure->parts as $subPartNum => $subPartStructure) {
             if ($partStructure->type == 2 && $partStructure->subtype == 'RFC822') {
                 $this->initMailPart($mail, $subPartStructure, $partNum);
             } else {
                 $this->initMailPart($mail, $subPartStructure, $partNum . '.' . ($subPartNum + 1));
             }
         }
     }
 }
コード例 #22
0
 /**
  * Private function : Recursivly get attached documents
  *
  * @param $mid : message id
  * @param $path : temporary path
  * @param $maxsize : of document to be retrieved
  * @param $structure : of the message or part
  * @param $part : part for recursive
  *
  * Result is stored in $this->files
  *
  */
 function getRecursiveAttached($mid, $path, $maxsize, $structure, $part = "")
 {
     global $LANG;
     if ($structure->type == 1) {
         // multipart
         reset($structure->parts);
         while (list($index, $sub) = each($structure->parts)) {
             $this->getRecursiveAttached($mid, $path, $maxsize, $sub, $part ? $part . "." . ($index + 1) : $index + 1);
         }
     } else {
         $filename = '';
         if ($structure->ifdparameters) {
             // get filename of attachment if present
             // if there are any dparameters present in this part
             if (count($structure->dparameters) > 0) {
                 foreach ($structure->dparameters as $dparam) {
                     if (utf8_strtoupper($dparam->attribute) == 'NAME' || utf8_strtoupper($dparam->attribute) == 'FILENAME') {
                         $filename = $dparam->value;
                     }
                 }
             }
         }
         //if no filename found
         if (empty($filename) && $structure->ifparameters) {
             // if there are any parameters present in this part
             if (count($structure->parameters) > 0) {
                 foreach ($structure->parameters as $param) {
                     if (utf8_strtoupper($param->attribute) == 'NAME' || utf8_strtoupper($param->attribute) == 'FILENAME') {
                         $filename = $param->value;
                     }
                 }
             }
         }
         if (empty($filename) && $structure->type == 5 && $structure->subtype) {
             // Embeded image come without filename - generate trivial one
             $filename = "image_{$part}." . $structure->subtype;
         }
         // if no filename found, ignore this part
         if (empty($filename)) {
             return false;
         }
         $filename = $this->decodeMimeString($filename);
         if ($structure->bytes > $maxsize) {
             $this->addtobody .= "<br>" . $LANG['mailgate'][6] . " (" . getSize($structure->bytes) . "): " . $filename;
             return false;
         }
         if (!Document::isValidDoc($filename)) {
             $this->addtobody .= "<br>" . $LANG['mailgate'][5] . " (" . $this->get_mime_type($structure) . ") : " . $filename;
             return false;
         }
         if ($message = imap_fetchbody($this->marubox, $mid, $part)) {
             switch ($structure->encoding) {
                 case 1:
                     $message = imap_8bit($message);
                     break;
                 case 2:
                     $message = imap_binary($message);
                     break;
                 case 3:
                     $message = imap_base64($message);
                     break;
                 case 4:
                     $message = quoted_printable_decode($message);
                     break;
             }
             if (file_put_contents($path . $filename, $message)) {
                 $this->files['multiple'] = true;
                 $j = count($this->files) - 1;
                 $this->files[$j]['filename']['size'] = $structure->bytes;
                 $this->files[$j]['filename']['name'] = $filename;
                 $this->files[$j]['filename']['tmp_name'] = $path . $filename;
                 $this->files[$j]['filename']['type'] = $this->get_mime_type($structure);
             }
         }
         // fetchbody
     }
     // Single part
 }
コード例 #23
0
ファイル: Message.php プロジェクト: zalazdi/laravel-imap
 private function decodeString($string, $encoding)
 {
     switch ($encoding) {
         case self::ENC_7BIT:
             return $string;
         case self::ENC_8BIT:
             return quoted_printable_decode(imap_8bit($string));
         case self::ENC_BINARY:
             return imap_binary($string);
         case self::ENC_BASE64:
             return imap_base64($string);
         case self::ENC_QUOTED_PRINTABLE:
             return quoted_printable_decode($string);
         case self::ENC_OTHER:
             return $string;
         default:
             return $string;
     }
 }
コード例 #24
0
ファイル: MY_Email.php プロジェクト: nguyenanhnhan/CI_Imap
 protected function _init_mail_part($mail, $part_structure, $part_num)
 {
     $data = $part_num ? imap_fetchbody($this->get_imap_stream(), $mail->id, $part_num, FT_UID) : imap_body($this->get_imap_stream(), $mail->id, FT_UID);
     if ($part_structure->encoding == 1) {
         $data = imap_utf8($data);
     } elseif ($part_structure->encoding == 2) {
         $data = imap_binary($data);
     } elseif ($part_structure->encoding == 3) {
         $data = imap_base64($data);
     } elseif ($part_structure->encoding == 4) {
         $data = imap_qprint($data);
     }
     $params = array();
     if (!empty($part_structure->parameters)) {
         foreach ($part_structure->parameters as $param) {
             $params[strtolower($param->attribute)] = $param->value;
         }
     }
     if (!empty($part_structure->dparameters)) {
         foreach ($part_structure->dparameters as $param) {
             $paramName = strtolower(preg_match('~^(.*?)\\*~', $param->attribute, $matches) ? $matches[1] : $param->attribute);
             if (isset($params[$paramName])) {
                 $params[$paramName] .= $param->value;
             } else {
                 $params[$paramName] = $param->value;
             }
         }
     }
     if (!empty($params['charset'])) {
         // php-imap fix #46
         //$data = iconv(strtoupper($params['charset']), $this->imap_server_encoding . '//IGNORE', $data);
         $data = mb_convert_encoding($data, $this->imap_server_encoding, $params['charset']);
     }
     // attachments
     $attachment_id = $part_structure->ifid ? trim($part_structure->id, " <>") : (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null);
     if ($attachment_id) {
         if (empty($params['filename']) && empty($params['name'])) {
             $file_name = $attachment_id . '.' . strtolower($part_structure->subtype);
         } else {
             $file_name = !empty($params['filename']) ? $params['filename'] : $params['name'];
             $file_name = $this->decode_mime_str($file_name, $this->imap_server_encoding);
             $file_name = $this->decode_rfc2231($file_name, $this->imap_server_encoding);
         }
         $CI =& get_instance();
         $CI->load->library('Incoming_Mail_Attachment');
         $mailattachment = new $CI->incoming_mail_attachment();
         $mailattachment->id = $attachment_id;
         $mailattachment->name = $file_name;
         if ($this->imap_attachemnt_dir) {
             $replace = array('/\\s/' => '_', '/[^0-9a-zA-Z_\\.]/' => '', '/_+/' => '_', '/(^_)|(_$)/' => '');
             $file_sys_name = preg_replace('~[\\\\/]~', '', $mail->id . '_' . $attachment_id . '_' . preg_replace(array_keys($replace), $replace, $file_name));
             $mailattachment->uploadfileName = $file_sys_name;
             $mailattachment->filePath = $this->imap_attachemnt_dir . DIRECTORY_SEPARATOR . $file_sys_name;
             file_put_contents($mailattachment->filePath, $data);
         }
         $mail->add_attachment($mailattachment);
         unset($mailattachment);
     } elseif ($part_structure->type == 0 && $data) {
         if (strtolower($part_structure->subtype) == 'plain') {
             $mail->text_plain .= $data;
         } else {
             $mail->text_html .= $data;
         }
     } elseif ($part_structure->type == 2 && $data) {
         $mail->text_plain .= trim($data);
     }
     if (!empty($part_structure->parts)) {
         foreach ($part_structure->parts as $sub_part_num => $sub_part_structure) {
             if ($part_structure->type == 2 && $part_structure->subtype == 'RFC822') {
                 $this->_init_mail_part($mail, $sub_part_structure, $part_num);
             } else {
                 $this->_init_mail_part($mail, $sub_part_structure, $part_num . '.' . ($sub_part_num + 1));
             }
         }
     }
 }
コード例 #25
0
 protected function initMailPart(IncomingMail $mail, $partStructure, $partNum, $markAsSeen = true)
 {
     $options = FT_UID;
     if (!$markAsSeen) {
         $options |= FT_PEEK;
     }
     $data = $partNum ? imap_fetchbody($this->getImapStream(), $mail->id, $partNum, $options) : imap_body($this->getImapStream(), $mail->id, $options);
     if ($partStructure->encoding == 1) {
         $data = imap_utf8($data);
     } elseif ($partStructure->encoding == 2) {
         $data = imap_binary($data);
     } elseif ($partStructure->encoding == 3) {
         $data = preg_replace('~[^a-zA-Z0-9+=/]+~s', '', $data);
         // https://github.com/barbushin/php-imap/issues/88
         $data = imap_base64($data);
     } elseif ($partStructure->encoding == 4) {
         $data = quoted_printable_decode($data);
     }
     $params = array();
     if (!empty($partStructure->parameters)) {
         foreach ($partStructure->parameters as $param) {
             $params[strtolower($param->attribute)] = $param->value;
         }
     }
     if (!empty($partStructure->dparameters)) {
         foreach ($partStructure->dparameters as $param) {
             $paramName = strtolower(preg_match('~^(.*?)\\*~', $param->attribute, $matches) ? $matches[1] : $param->attribute);
             if (isset($params[$paramName])) {
                 $params[$paramName] .= $param->value;
             } else {
                 $params[$paramName] = $param->value;
             }
         }
     }
     // attachments
     $attachmentId = $partStructure->ifid ? trim($partStructure->id, " <>") : (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null);
     if ($attachmentId) {
         if (empty($params['filename']) && empty($params['name'])) {
             $fileName = $attachmentId . '.' . strtolower($partStructure->subtype);
         } else {
             $fileName = !empty($params['filename']) ? $params['filename'] : $params['name'];
             $fileName = $this->decodeMimeStr($fileName, $this->serverEncoding);
             $fileName = $this->decodeRFC2231($fileName, $this->serverEncoding);
         }
         $attachment = new IncomingMailAttachment();
         $attachment->id = $attachmentId;
         $attachment->name = $fileName;
         $attachment->disposition = isset($partStructure->disposition) ? $partStructure->disposition : null;
         if ($this->attachmentsDir) {
             $replace = array('/\\s/' => '_', '/[^0-9a-zа-яіїє_\\.]/iu' => '', '/_+/' => '_', '/(^_)|(_$)/' => '');
             $fileSysName = preg_replace('~[\\\\/]~', '', $mail->id . '_' . $attachmentId . '_' . preg_replace(array_keys($replace), $replace, $fileName));
             $attachment->filePath = $this->attachmentsDir . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . $fileSysName;
             file_put_contents($attachment->filePath, $data);
         }
         $mail->addAttachment($attachment);
     } else {
         if (!empty($params['charset'])) {
             $data = $this->convertStringEncoding($data, $params['charset'], $this->serverEncoding);
         }
         if ($partStructure->type == 0 && $data) {
             if (strtolower($partStructure->subtype) == 'plain') {
                 $mail->textPlain .= $data;
             } else {
                 $mail->textHtml .= $data;
             }
         } elseif ($partStructure->type == 2 && $data) {
             $mail->textPlain .= trim($data);
         }
     }
     if (!empty($partStructure->parts)) {
         foreach ($partStructure->parts as $subPartNum => $subPartStructure) {
             if ($partStructure->type == 2 && $partStructure->subtype == 'RFC822') {
                 $this->initMailPart($mail, $subPartStructure, $partNum, $markAsSeen);
             } else {
                 $this->initMailPart($mail, $subPartStructure, $partNum . '.' . ($subPartNum + 1), $markAsSeen);
             }
         }
     }
 }
コード例 #26
0
ファイル: imap.php プロジェクト: kam1katze/ocDashboard
 function saveAttachment($uid, $partNum, $encoding)
 {
     $partStruct = imap_bodystruct($this->stream, imap_msgno($this->stream, $uid), $partNum);
     $message = imap_fetchbody($this->stream, $uid, $partNum, FT_UID);
     switch ($encoding) {
         case 0:
         case 1:
             $message = imap_8bit($message);
             break;
         case 2:
             $message = imap_binary($message);
             break;
         case 3:
             $message = imap_base64($message);
             break;
         case 4:
             $message = quoted_printable_decode($message);
             break;
     }
     return $message;
 }
コード例 #27
0
ファイル: EmbeddedPart.php プロジェクト: artemevsin/imap
 /**
  * Decode given string with encoding in current string and convert it to UTF-8 from part charset
  *
  * @param $string
  * @return string
  */
 private function decode($string)
 {
     if (!$string) {
         return $string;
     }
     //transfer encoding
     switch (strtolower($this->contentTransferEncoding)) {
         case "7bit":
             $decodedString = mb_convert_encoding($string, "UTF-8", "auto");
             break;
         case "8bit":
             $decodedString = imap_8bit($string);
             break;
         case "binary":
             $decodedString = imap_base64(imap_binary($string));
             break;
         case "base64":
             $decodedString = imap_base64($string);
             break;
         case "quoted-printable":
             $decodedString = imap_qprint($string);
             break;
         default:
             throw new \UnexpectedValueException('Cannot decode ' . $this->contentTransferEncoding);
     }
     //do not convert if string is attachment content
     if ($this->disposition == "attachment") {
         return $decodedString;
     }
     //charset encoding
     //TODO add different charsets
     $decodedString = quoted_printable_decode($decodedString);
     if (in_array($this->charset, ['windows-1250', 'koi8-r'])) {
         return iconv(strtoupper($this->charset), "UTF-8", $decodedString);
     } else {
         return mb_convert_encoding($decodedString, "UTF-8", strtoupper($this->charset));
     }
 }
コード例 #28
0
 function GetAttach($mid, $path)
 {
     // Get Atteced File from Mail
     if (!$this->marubox) {
         return false;
     }
     $struckture = imap_fetchstructure($this->marubox, $mid);
     $ar = "";
     //        var_dump($struckture);
     if (isset($struckture->parts)) {
         if ($struckture->parts) {
             foreach ($struckture->parts as $key => $value) {
                 $enc = $struckture->parts[$key]->encoding;
                 if ($struckture->parts[$key]->subtype == 'OCTET-STREAM') {
                     //                    $name = base64_decode($struckture->parts[$key]->parameters[1]->value);
                     $name = base64_decode($struckture->parts[$key]->parameters[1]->value);
                     $name = iconv($struckture->parts[$key]->parameters[0]->value, 'utf-8', $name);
                     $name = substr($name, 7);
                     // 未知原因 base64解密以后字符串前边多了三个 乱码字符    截取7以后的字符串
                     $message = imap_fetchbody($this->marubox, $mid, $key + 1);
                     switch ($enc) {
                         case 0:
                             $message = imap_8bit($message);
                             break;
                         case 1:
                             $message = imap_8bit($message);
                             break;
                         case 2:
                             $message = imap_binary($message);
                             break;
                         case 3:
                             $message = imap_base64($message);
                             break;
                         case 4:
                             $message = quoted_printable_decode($message);
                             break;
                         case 5:
                             $message = $message;
                             break;
                     }
                     // 文件名转换
                     $name = explode('.', $name);
                     $firs_name = urlencode($name[0]);
                     $filename = $firs_name . '.' . $name[1];
                     $fp = fopen($path . $filename, "w");
                     //fopen  中文文件名
                     fwrite($fp, $message);
                     fclose($fp);
                     $ar = $ar . $name[0] . "." . $name[1] . ",";
                 }
             }
         }
     }
     $ar = substr($ar, 0, strlen($ar) - 1);
     return $ar;
 }
コード例 #29
0
 protected function initMailPart($mbox, $mail, $partStructure, $partNum)
 {
     $data = $partNum ? imap_fetchbody($mbox, $mail['id'], $partNum, FT_UID | FT_PEEK) : imap_body($mbox, $mail['id'], FT_UID | FT_PEEK);
     if ($partStructure->encoding == 1) {
         $data = imap_utf8($data);
     } elseif ($partStructure->encoding == 2) {
         $data = imap_binary($data);
     } elseif ($partStructure->encoding == 3) {
         $data = imap_base64($data);
     } elseif ($partStructure->encoding == 4) {
         $data = imap_qprint($data);
     }
     $params = array();
     if (!empty($partStructure->parameters)) {
         foreach ($partStructure->parameters as $param) {
             $params[strtolower($param->attribute)] = $param->value;
         }
     }
     if (!empty($partStructure->dparameters)) {
         foreach ($partStructure->dparameters as $param) {
             $paramName = strtolower(preg_match('~^(.*?)\\*~', $param->attribute, $matches) ? $matches[1] : $param->attribute);
             if (isset($params[$paramName])) {
                 $params[$paramName] .= $param->value;
             } else {
                 $params[$paramName] = $param->value;
             }
         }
     }
     if (!empty($params['charset'])) {
         $data = iconv(strtoupper($params['charset']), 'utf-8', $data);
     }
     $attachmentId = $partStructure->ifid ? trim($partStructure->id, " <>") : (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null);
     if ($attachmentId) {
         if (empty($params['filename']) && empty($params['name'])) {
             $fileName = $attachmentId . '.' . strtolower($partStructure->subtype);
         } else {
             $fileName = !empty($params['filename']) ? $params['filename'] : $params['name'];
             $fileName = self::decodeMimeStr($fileName);
             $fileName = self::decodeRFC2231($fileName);
         }
         $mail['attachments'][$attachmentId]['filename'] = $fileName;
         $mail['attachments'][$attachmentId]['attachment'] = $data;
     } elseif ($partStructure->type == 0 && $data) {
         if (base64_decode($data, true)) {
             $data = base64_decode($data);
         }
         if (strtolower($partStructure->subtype) == 'plain') {
             $mail['textPlain'] .= $data;
         } else {
             $mail['textHtml'] .= $data;
         }
     } elseif ($partStructure->type == 2 && $data) {
         $mail['textPlain'] .= trim($data);
     }
     if (!empty($partStructure->parts)) {
         foreach ($partStructure->parts as $subPartNum => $subPartStructure) {
             if ($partStructure->type == 2 && $partStructure->subtype == 'RFC822') {
                 $mail = self::initMailPart($mbox, $mail, $subPartStructure, $partNum);
             } else {
                 $mail = self::initMailPart($mbox, $mail, $subPartStructure, $partNum . '.' . ($subPartNum + 1));
             }
         }
     }
     return $mail;
 }
コード例 #30
0
 function GetAttech($mid, $path)
 {
     $struckture = imap_fetchstructure($this->marubox, $mid);
     $ar = "";
     foreach ($struckture->parts as $key => $value) {
         $enc = $struckture->parts[$key]->encoding;
         if ($struckture->parts[$key]->ifdparameters) {
             $name = $struckture->parts[$key]->dparameters[0]->value;
             $message = imap_fetchbody($this->marubox, $mid, $key + 1);
             if ($enc == 0) {
                 $message = imap_8bit($message);
             }
             if ($enc == 1) {
                 $message = imap_8bit($message);
             }
             if ($enc == 2) {
                 $message = imap_binary($message);
             }
             if ($enc == 3) {
                 $message = imap_base64($message);
             }
             if ($enc == 4) {
                 $message = quoted_printable_decode($message);
             }
             if ($enc == 5) {
                 $message = $message;
             }
             $fp = fopen($path . $name, "w");
             fwrite($fp, $message);
             fclose($fp);
             $ar = $ar . $name . ",";
         }
     }
     $ar = substr($ar, 0, strlen($ar) - 1);
     return $ar;
 }