Example #1
0
function pop_subject()
{
    $username = '******';
    $password = '******';
    $imap = imap_open("{pop3.163.com:995/pop3/ssl/novalidate-cert}", $username, $password);
    //    $imap = imap_open("{pop.21cn.com:110/pop/notls}", $username, $password);
    $message_count = imap_num_msg($imap);
    echo "------->>" . is_int($message_count);
    echo '$message_count' . $message_count . "\n";
    for ($i = $message_count; $i >= 1; $i--) {
        $header = imap_header($imap, $i);
        $date = $header->date;
        echo "************msg no: {$i} \n {$date} \n";
        $subjectArr = @imap_mime_header_decode($header->subject);
        if (is_array($subjectArr)) {
            $subject = '';
            foreach ($subjectArr as $subjectObj) {
                //遍历标题数组,获取所有标题text
                $subject .= trim($subjectObj->text);
            }
            $mailCharset = mb_detect_encoding($subject, array('UTF-8', 'GBK', 'LATIN1', 'BIG5'));
            echo $mailCharset . "\n";
            //查看邮件标题的编码
            $subject = mb_convert_encoding($subject, 'utf-8', $mailCharset);
            //对标题进行解码操作
            echo $subject . "\n";
        } else {
            continue;
        }
    }
    imap_close($imap);
}
 public function actionIndex()
 {
     $model = new Receive();
     error_reporting(E_ALL ^ E_WARNING);
     $obj = new receiveMail('tclrg', 'Luanruitest', '*****@*****.**', 'imap.126.com', 'imap', '993', 'ture');
     $obj->connect();
     $tot = $obj->getTotalMails();
     //查看邮箱是否有新邮件
     $old_email_count = Email::find()->count();
     for ($i = $tot; $i > $old_email_count; $i--) {
         $model = new Receive();
         $head = $obj->getHeaders($i);
         $head['subject'] = imap_mime_header_decode($head['subject'])[0]->text;
         $head['from'] = imap_mime_header_decode($head['from'])[0]->text;
         $head['fromName'] = imap_mime_header_decode($head['fromName'])[0]->text;
         $model->subject = $head['subject'];
         $model->sender = $head['from'];
         //            echo $i.$head['date']."</br>";
         //            echo $i.strtotime($head['date'])."</br>";
         //            echo $i.date("Y-m-d H:i:s",strtotime($head['date']))."</br>";
         $model->sendtime = date("Y-m-d H:i:s", strtotime($head['date']));
         $model->text = $obj->getBody($i);
         //            $model->sendtime=$head['date'];
         $model->save();
         //            echo $i.$head['date'];
     }
     $obj->close_mailbox();
 }
Example #3
0
 protected function decode($value)
 {
     if (empty($value) || ($value = '')) {
         return '';
     }
     $decoded = '';
     $parts = imap_mime_header_decode($value);
     foreach ($parts as $part) {
         $charset = 'default' == $part->charset ? 'auto' : $part->charset;
         // imap_utf8 doesn't seem to work properly, so use Transcoder instead
         try {
             $decoded .= Transcoder::create()->transcode($part->text, $charset);
         } catch (\Ddeboer\Transcoder\Exception\UndetectableEncodingException $ex) {
             $decoded .= $part->text;
         } catch (\Ddeboer\Transcoder\Exception\UnsupportedEncodingException $ex) {
             $decoded .= $part->text;
         } catch (\Ddeboer\Transcoder\Exception\IllegalCharacterException $ex) {
             // force transcoding using iconv and ignore.
             $decoded .= @iconv($charset, 'UTF-8//IGNORE', $part->text);
         } catch (\Exception $ex) {
             // maybe any kind of exception.
             $decoded .= $part->text;
         }
     }
     return $decoded;
 }
Example #4
0
 function getHeaders($mid)
 {
     if (!$this->marubox) {
         return false;
     }
     $mail_header = imap_header($this->marubox, $mid);
     $sender = $mail_header->from[0];
     //        var_dump(strtolower($sender->mailbox));
     //        return $this->marubox;
     $sender_replyto = $mail_header->reply_to[0];
     $mail_details = array();
     if (strtolower($sender->mailbox) != 'mailer-daemon' && strtolower($sender->mailbox) != 'postmaster') {
         date_default_timezone_set('PRC');
         $subject = imap_mime_header_decode($mail_header->subject);
         $toaddress = imap_mime_header_decode($mail_header->toaddress);
         $mail_details = array('from' => strtolower($sender->mailbox) . '@' . $sender->host, 'fromName' => "", 'fromName_charset' => "", 'toOth' => strtolower($sender_replyto->mailbox) . '@' . $sender_replyto->host, 'toNameOth' => "", 'subject' => $subject[0]->text, 'subject_charset' => $subject[0]->charset, 'mailDate' => date("Y-m-d H:i:s", $mail_header->udate), 'udate' => $mail_header->udate, 'to' => $toaddress[0]->text);
         if (isset($sender->personal)) {
             $sPersonal = imap_mime_header_decode($sender->personal);
             $mail_details['fromName'] = $sPersonal[0]->text;
             $mail_details['fromName_charset'] = $sPersonal[0]->charset;
         }
         if (isset($sender_replyto->personal)) {
             $rePersonal = imap_mime_header_decode($sender_replyto->personal);
             $mail_details['toNameOth'] = $rePersonal[0]->text;
         }
     }
     return $mail_details;
 }
Example #5
0
 protected function decode($value)
 {
     $decoded = '';
     $parts = imap_mime_header_decode($value);
     try {
         foreach ($parts as $part) {
             $charset = 'default' == $part->charset ? 'auto' : $part->charset;
             //Ajout COGIVEA fix encoding :
             $charset = $this->charset != '' && $charset == 'auto' ? $this->charset : $charset;
             //print $part->text."#".$this->charset."/".$charset."#"."\n";
             // imap_utf8 doesn't seem to work properly, so use Transcoder instead
             // Got from: https://github.com/Sawered/imap/commit/e739b7221c6e57521b38f7b56f78ba399acda888 and changed to UndetectableEncodingException
             try {
                 $decoded .= Transcoder::create()->transcode($part->text, $charset);
             } catch (IllegalCharacterException $e) {
                 //no warn, itis reality
                 //FIX COGIVEA
             } catch (UndetectableEncodingException $e) {
                 //no warn, it is reality, handle it somehow
                 $decoded = imap_utf8($part->text);
             }
         }
     } catch (Exception $e) {
     }
     return $decoded;
 }
Example #6
0
File: Mime.php Project: ephp/imap
 public static function decode($string)
 {
     $parts = imap_mime_header_decode($string);
     $s = '';
     foreach ($parts as $part) {
         $s .= $part->text;
     }
     return $s;
 }
 function flat_mime_decode($string)
 {
     $array = imap_mime_header_decode($string);
     $str = "";
     foreach ($array as $key => $part) {
         $str .= str_replace(['{', '}'], ['[', ']'], $part->text);
     }
     return $str;
 }
Example #8
0
function decode_imap_text($str)
{
    $result = '';
    $decode_header = imap_mime_header_decode($str);
    foreach ($decode_header as $obj) {
        $result .= htmlspecialchars(rtrim($obj->text, "\t"));
    }
    return $result;
}
Example #9
0
 /**
  * Decodes a header value
  * @param string $value The value to decode
  * @return string Decoded value
  */
 public function decodeHeaderValue($value)
 {
     $result = '';
     $tokens = imap_mime_header_decode($value);
     foreach ($tokens as $token) {
         $result .= $token->text;
     }
     return $result;
 }
function get_plain_text_subject($mbox, $msgNum, $headers = null)
{
    $headers or $headers = imap_headerinfo($mbox, $msgNum);
    $subjectParts = imap_mime_header_decode($headers->Subject);
    $subject = '';
    foreach ($subjectParts as $p) {
        $subject .= $p->text;
    }
    return trim($subject);
}
Example #11
0
function unmime($s)
{
    // decodage mime (=?iso-8859-1?Q?l=E9a?= et autres =?iso-8859-1?B?bOlh?=)
    $elements = imap_mime_header_decode($s);
    $s = "";
    for ($i = 0; $i < count($elements); $i++) {
        $s .= $elements[$i]->text;
    }
    return $s;
}
Example #12
0
 protected function decode($value)
 {
     $decoded = '';
     $parts = imap_mime_header_decode($value);
     foreach ($parts as $part) {
         $charset = 'default' == $part->charset ? 'auto' : $part->charset;
         // imap_utf8 doesn't seem to work properly, so use Transcoder instead
         $decoded .= Transcoder::create()->transcode($part->text, $charset);
     }
     return $decoded;
 }
Example #13
0
 /**
  * @param string $text
  * @param string $targetCharset
  *
  * @return string
  */
 public static function decode($text, $targetCharset = 'utf-8')
 {
     if (null === $text) {
         return null;
     }
     $result = '';
     foreach (imap_mime_header_decode($text) as $word) {
         $ch = 'default' === $word->charset ? 'ascii' : $word->charset;
         $result .= Message::charsetConvert($word->text, $ch, $targetCharset) ?: $word->text;
     }
     return $result;
 }
Example #14
0
 /**
  * Set email owner name.
  *
  * @param string $name
  */
 public function setName($name)
 {
     if (is_string($name)) {
         $this->raw_name = $name;
         $name = imap_mime_header_decode($name);
         if (empty($name[0])) {
             $this->name = '';
         } else {
             $this->name = $name[0]->text;
         }
     }
 }
Example #15
0
function decodeMimeStr($string, $charset = "UTF-8")
{
    $newString = '';
    $elements = imap_mime_header_decode($string);
    for ($i = 0; $i < count($elements); $i++) {
        if ($elements[$i]->charset == 'default' || $elements[$i]->charset == 'x-unknown') {
            $elements[$i]->charset = 'iso-8859-1';
        }
        $newString .= iconv($elements[$i]->charset, $charset, $elements[$i]->text);
    }
    return $newString;
}
function decodeSubject($string)
{
    $elements = imap_mime_header_decode($string);
    $out = '';
    for ($i = 0; $i < count($elements); $i++) {
        if ($elements[$i]->charset == 'default' || $elements[$i]->charset == 'UNKNOWN') {
            $out .= $elements[$i]->text;
        } else {
            $out .= iconv($elements[$i]->charset, 'UTF-8//TRANSLIT', $elements[$i]->text);
        }
    }
    return $out;
}
    static public function header($mimeString, $targetCharset = self::UTF8) {
        $decodedStr = '';
        $mimeParts = \imap_mime_header_decode($mimeString);
        foreach ($mimeParts as $mimePart) {
            $charset = $mimePart->charset;
            if ($charset == 'default') {
                $charset = self::ASCII;
            }

            $decodedStr .= self::text($mimePart->text, $targetCharset, $charset);
        }

        return $decodedStr;
    }
Example #18
0
 public function actionIndex()
 {
     $model = new Receive();
     $mailaccount = setting::find()->one();
     $user = $mailaccount->user;
     $password = $mailaccount->password;
     $username = $mailaccount->username;
     $receivehost = $mailaccount->receivehost;
     $receiveapply = "imap";
     $receiveport = $mailaccount->receiveport;
     $obj = new receiveMail($user, $password, $username, $receivehost, $receiveapply, $receiveport, 'ture');
     $obj->connect();
     $tot = $obj->getTotalMails();
     for ($i = $tot; $i > $tot - 5; $i--) {
         $model = new Receive();
         $head = $obj->getHeaders($i);
         //            var_dump($head);
         $head['subject'] = imap_mime_header_decode($head['subject'])[0]->text;
         $head['from'] = imap_mime_header_decode($head['from'])[0]->text;
         $head['fromName'] = imap_mime_header_decode($head['fromName'])[0]->text;
         $attachments = $obj->GetAttach($i, "c:/receiveattachment");
         $attach = "";
         if ($attachments != "") {
             $attach = $attachments;
             var_dump($attach);
             $encode = mb_detect_encoding($attachments, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5", "EUC-CN"));
             if ($encode == "EUC-CN") {
                 $encode = "GB2312";
             }
             if ($encode != "UTF-8") {
                 $attach = iconv("{$encode}", "UTF-8//IGNORE", $attachments);
             }
         }
         var_dump($attach);
         $model->subject = $head['subject'];
         $model->sender = $head['from'];
         //            echo $i.$head['date']."</br>";
         //            echo $i.strtotime($head['date'])."</br>";
         //            echo $i.date("Y-m-d H:i:s",strtotime($head['date']))."</br>";
         $model->sendtime = date("Y-m-d H:i:s", strtotime($head['date']));
         $model->text = $obj->getBody($i);
         $model->attachment = $attach;
         var_dump($model);
         //            $model->sendtime=$head['date'];
         $model->save();
         //            echo $i.$head['date'];
     }
     $obj->close_mailbox();
 }
Example #19
0
 function mimedecode($text, $encoding = 'UTF-8')
 {
     if (function_exists('imap_mime_header_decode') && ($parts = imap_mime_header_decode($text))) {
         $str = '';
         foreach ($parts as $part) {
             $str .= Charset::transcode($part->text, $part->charset, $encoding);
         }
         $text = $str;
     } elseif ($text[0] == '=' && function_exists('iconv_mime_decode')) {
         $text = iconv_mime_decode($text, 0, $encoding);
     } elseif (!strcasecmp($encoding, 'utf-8') && function_exists('imap_utf8')) {
         $text = imap_utf8($text);
     }
     return $text;
 }
Example #20
0
 /**
  * @param $enc
  * @return string
  */
 private static function decode($enc)
 {
     $parts = imap_mime_header_decode($enc);
     $str = '';
     for ($p = 0; $p < count($parts); $p++) {
         $ch = $parts[$p]->charset;
         $part = $parts[$p]->text;
         if ($ch !== 'default') {
             $str .= mb_convert_encoding($part, 'UTF-8', $ch);
         } else {
             $str .= $part;
         }
     }
     return $str;
 }
 public function format_charset($subject)
 {
     $mime = 0;
     if (substr_count(strtolower($subject), 'iso-88') > 0) {
         $mime = 1;
     }
     if (substr_count(strtolower($subject), 'utf-') > 0 && $mime == 0) {
         $mime = 1;
     }
     if ($mime == 1) {
         $subject = strip_tags($subject);
         $subject = imap_mime_header_decode($subject);
         $subject = $subject[0]->text;
     }
     return $subject;
 }
Example #22
0
 /**
  * @param string $text
  * @param string $targetCharset
  *
  * @return string
  */
 public static function decode($text, $targetCharset = 'utf-8')
 {
     if (null === $text) {
         return null;
     }
     $result = '';
     foreach (imap_mime_header_decode($text) as $word) {
         $ch = 'default' === $word->charset ? 'ascii' : $word->charset;
         if (strtoupper($ch) != strtoupper($targetCharset)) {
             $result .= iconv($ch, $targetCharset, $word->text);
         } else {
             $result .= $word->text;
         }
     }
     return $result;
 }
Example #23
0
 function decode($what, $errors = false)
 {
     if (function_exists('imap_mime_header_decode') && ($parts = imap_mime_header_decode($text))) {
         $str = '';
         foreach ($parts as $part) {
             $str .= Format::encode($part->text, $part->charset, $encoding);
         }
         return $str;
     } elseif ($text[0] == '=' && function_exists('iconv_mime_decode')) {
         return iconv_mime_decode($text, 0, $encoding);
         // TODO: Use a pure-PHP version to perform the decoding
     } elseif (!strcasecmp($encoding, 'utf-8') && function_exists('imap_utf8')) {
         return imap_utf8($text);
     }
     return $text;
 }
Example #24
0
 public static function Decod_Header($variable, $charset_conv = "utf-8")
 {
     $decod = imap_mime_header_decode($variable);
     if (array_key_exists(0, $decod)) {
         if ($decod[0]->charset == "default") {
             $subject = $variable;
         } else {
             if ($decod[0]->text != "") {
                 $subject = Charset::CambiaCharset($decod[0]->text, $decod[0]->charset, $charset_conv);
             }
         }
     } else {
         $subject = $variable;
     }
     return $subject;
 }
Example #25
0
 /**
  * @return bool
  */
 public function getNewMessages()
 {
     $query = imap_search(self::__connect(), 'UNSEEN');
     $data = false;
     $messages = [];
     if (is_array($query)) {
         foreach ($query as $number) {
             $messages[] = imap_fetch_overview(self::__connect(), $number)[0];
         }
         for ($i = 0; $i < count($messages); $i++) {
             $data[$i]['msgno'] = $messages[$i]->msgno;
             $data[$i]['subject'] = imap_mime_header_decode(imap_utf8(mb_convert_encoding($messages[$i]->subject, 'UTF8', 'KOI8-R')))[0]->text;
         }
     }
     return $data;
 }
Example #26
0
 /**
  *
  * @return string (in UTF-8 format)
  * @throws Exception if a subject header is not found
  */
 public function getSubject()
 {
     if (!isset($this->rawFields['subject'])) {
         throw new Exception("Couldn't find the subject of the email");
     }
     $ret = '';
     if ($this->isImapExtensionAvailable) {
         foreach (imap_mime_header_decode($this->rawFields['subject']) as $h) {
             // subject can span into several lines
             $charset = $h->charset == 'default' ? 'US-ASCII' : $h->charset;
             $ret .= iconv($charset, "UTF-8//TRANSLIT", $h->text);
         }
     } else {
         $ret = utf8_encode(iconv_mime_decode($this->rawFields['subject']));
     }
     return $ret;
 }
Example #27
0
 /**
  * Decodes encoded word elements from a header
  * 
  * @param mixed $value
  * @return string
  */
 private function decodeHeader($value)
 {
     if (substr($value, 0, 2) != '=?') {
         return $value;
     }
     $value_objects = imap_mime_header_decode($value);
     $new_value = '';
     $charset = 'WINDOWS-1252';
     foreach ($value_objects as $value_object) {
         if (preg_match('#^\\s+$#D', $value_object->text)) {
             continue;
         }
         $charset = $value_object->charset != 'default' ? $value_object->charset : $charset;
         $new_value .= $value_object->text;
     }
     return iconv($charset, 'UTF-8', $new_value);
 }
 public function __construct(stdClass $headers)
 {
     // Store all headers as lowercase
     $this->array = array_change_key_case((array) $headers);
     // Decode subject, as it may be UTF-8 encoded
     if (isset($headers->subject)) {
         $subject = '';
         foreach (imap_mime_header_decode($headers->subject) as $part) {
             // $part->charset can also be 'default', i.e. plain US-ASCII
             if (function_exists('iconv')) {
                 $charset = $part->charset == 'default' ? 'UTF-8' : $part->charset;
                 $subject .= iconv($charset, 'UTF-8', $part->text);
             } else {
                 $charset = $part->charset == 'default' ? 'auto' : $part->charset;
                 $subject .= mb_convert_encoding($part->text, 'UTF-8', $charset);
             }
         }
         $this->array['subject'] = $subject;
     }
     $this->array['msgno'] = (int) $this->array['msgno'];
     foreach (array('answered', 'deleted', 'draft') as $flag) {
         $this->array[$flag] = (bool) trim($this->array[$flag]);
     }
     if (isset($this->array['date'])) {
         $this->array['date'] = preg_replace('/(.*)\\(.*\\)/', '$1', $this->array['date']);
         // $this->array['date'] = new DateTime($this->array['date']);
     }
     if (isset($this->array['from'])) {
         $from = current($this->array['from']);
         $this->array['from'] = new Mirasvit_Ddeboer_Imap_Message_EmailAddress($from->mailbox, $from->host, isset($from->personal) ? imap_utf8($from->personal) : null);
     }
     if (isset($this->array['to'])) {
         $recipients = array();
         foreach ($this->array['to'] as $to) {
             $recipients[] = new Mirasvit_Ddeboer_Imap_Message_EmailAddress(str_replace('\'', '', $to->mailbox), str_replace('\'', '', $to->host), isset($to->personal) ? imap_utf8($to->personal) : null);
         }
         $this->array['to'] = $recipients;
     } else {
         $this->array['to'] = array();
     }
     if (isset($this->array['reply_to'])) {
         $from = current($this->array['reply_to']);
         $this->array['reply_to'] = new Mirasvit_Ddeboer_Imap_Message_EmailAddress($from->mailbox, $from->host, isset($from->personal) ? imap_utf8($from->personal) : null);
     }
 }
 /**
  * Decodes the given email header
  * @param string $header
  * @param string $encoding The encoding to output the header in (Default: UTF-8)
  * @return string
  */
 public function decodeHeader($header, $encoding = "UTF-8")
 {
     $header = imap_mime_header_decode($header);
     $encodings = mb_list_encodings();
     // Add simplified Chinese (GB2312), supported but not listed and widely used.
     $encodings[] = 'GB2312';
     // Make all strings upper case for comparison to charset.
     $encodings = array_map('strtoupper', $encodings);
     $encoded = [];
     if (count($header)) {
         foreach ($header as $h) {
             if (in_array(strtoupper($h->charset), $encodings)) {
                 $encoded[] = mb_convert_encoding($h->text, $encoding, $h->charset);
             } else {
                 // We don't know what the charset is, so let's attempt to detect and convert the encoding.
                 $encoded[] = mb_convert_encoding($h->text, $encoding);
             }
         }
     }
     return implode('', $encoded);
 }
 /**
  * takes the output from imap_mime_hader_decode() and handles multiple types of encoding
  * @param string subject Raw subject string from email
  * @return string ret properly formatted UTF-8 string
  */
 function handleMimeHeaderDecode($subject)
 {
     $subjectDecoded = imap_mime_header_decode($subject);
     $ret = '';
     foreach ($subjectDecoded as $object) {
         if ($object->charset != 'default') {
             $ret .= $this->handleCharsetTranslation($object->text, $object->charset);
         } else {
             $ret .= $object->text;
         }
     }
     return $ret;
 }