--
這是一封由 Google Gmail 寄出的信件原始碼,主旨是 sdf 內容 sdf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
From pc@hoyo.idv.tw Sun May 20 14:01:45 2018 Received: from mail-pl0-x236.google.com (unknown [IPv6:2607:f8b0:400e:c01::236]) by sm.hoyo.idv.tw (Postfix) with ESMTP id C94B9C2B30A2C for <oenru2@sm.hoyo.idv.tw>; Sun, 20 May 2018 06:01:44 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 sm.hoyo.idv.tw C94B9C2B30A2C Authentication-Results: sm.hoyo.idv.tw; dkim=pass (2048-bit key) header.d=hoyo-idv-tw.20150623.gappssmtp.com header.i=@hoyo-idv-tw.20150623.gappssmtp.com header.b="0n6iLjVc" Received: by mail-pl0-x236.google.com with SMTP id ay10-v6so6907782plb.1 for <oenru2@sm.hoyo.idv.tw>; Sat, 19 May 2018 23:01:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hoyo-idv-tw.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=zmipbNoRU4Htj2PidOeINnUwMC3J2OqqDT0EdnBpO08=; b=0n6iLjVcN/rZCA1gQDpF3kxCdnzz2JQZ/mhpx6nV0G4MIlBdeCg4Mx+RI3uGwCH1/Z mfrPSYZA9ncuTyNlry0sMH69sk5zlVx8LI0PJFsSruyBSDtShq6UmYG2mCIJvZYrLlRL 3nvM9ngH68JpgD757YaiVhOm79cAf57qAvEWo7e6+riwi5mbF2CowHc+p4EmBm9t763b lM7SLR3ColpTCDywstq6eZBeEAkB/EFlkCAoE8y2NOKgwx0VT97GwLpFxWIDMRP2assr GSeSauui1zpHmpGRZEnwt7wUTT3HwiDq9PsOtvl+vPmHUxeT79Xkd00/x/hU5X8X+AJy K4yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=zmipbNoRU4Htj2PidOeINnUwMC3J2OqqDT0EdnBpO08=; b=rviNB4QgamZtGALFTsAHCgBQ/Wp4cMphhOkMYweAIdvOyk3MysPfWd4LbykdrQF64P w+BDnzLZhuRWYFQKJvclkWUZn8CLEnzlfFos69CjbXKXEBy+z363SUQFZ/6CArYC7+A2 sQAA118UStijV1XWnrcRT/ceGd/+n1mB3bGCqHE1gjb3LurByzDYeX9rOqqR7ZgLA3l7 o89VBl/5TXDWE6xu22qWH3tyIRf/32NwMQAVkO7QReFyqbA9wrbUMVqr/EFPDDerALJu E2rRggwoxlApJR0+ha/RmMWfAxjNXNFxcQtUm0WyQO5DJA90dTAHnWUVPjVSC1eMN7bx LEbw== X-Gm-Message-State: ALKqPwfwoP/JRc6Y0ecepyjG0h7QrVq0v2mjHOlXNuz3uxuFUgUWnmh4 g7Tx/pY2EuoZUD7ThKmdGgjm0ZRwc4M+mRls7dcrOfgD X-Google-Smtp-Source: AB8JxZrm9IAfiocxS9p7XvPFJMCBP3tD8bpUuR57yCkbKJ4/dHRVHA9B3eIqV1svUQCqR7BRj6JIt26G8J3Yjz8DEck= X-Received: by 2002:a17:902:3103:: with SMTP id w3-v6mr15405047plb.37.1526796103596; Sat, 19 May 2018 23:01:43 -0700 (PDT) MIME-Version: 1.0 Received: by 10.100.181.171 with HTTP; Sat, 19 May 2018 23:01:43 -0700 (PDT) From: =?UTF-8?B?6buD5b+X6LOi?= <pc@hoyo.idv.tw> Date: Sun, 20 May 2018 14:01:43 +0800 Message-ID: <CAJ060EoTYq_UGkp-SVkvh6KUdk2JOBHFUrWPwkokijNg0sB9XA@mail.gmail.com> Subject: sdf To: oenru2@sm.hoyo.idv.tw Content-Type: multipart/alternative; boundary="000000000000664f74056c9ceba4" --000000000000664f74056c9ceba4 Content-Type: text/plain; charset="UTF-8" sdf --000000000000664f74056c9ceba4 Content-Type: text/html; charset="UTF-8" <div dir="ltr">sdf<br></div> --000000000000664f74056c9ceba4-- |
SMTP 本來在收到這封信時,會解析 To: 等內容確定這封信是要給誰,然後將信件歸到該使用者的檔案或資料夾內,使用者使用讀信軟體時就會進行內容解讀,將寄信人、收件人、主旨、內容等一般資訊列出
可是當我們要把信件呈現在網頁上時,就必須先把信件內容「翻譯」成網頁可以顯示的結果,例如 ?UTF-8?B?6buD5b+X6LOi?= 這種 RFC2047 的編碼方式,還有將不同的字元編碼統一轉成 UTF-8 等等等之類的工作
--
PHP 的信件解析函式庫
幸運的是 PHP 不愧是成熟的語言,什麼風浪都見過,針對信件有了專門的解析函式庫,不幸的是這是 PECL 所以需要另外安裝載入
--
安裝、載入 Mailparse
- Installing the PECL/PEAR mailparse module for PHP on CentOS
- How to Install php-mailparse in Ubuntu 18.04
Ubuntu
1 2 |
# apt install php-dev # apt install php-mailparse |
CentOS 7 + PHP 7.2
1 2 3 4 |
# yum install php-pear php-devel # pear channel-update pecl.php.net # yum install http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/r/re2c-0.14.3-2.el7.x86_64.rpm # pear install pecl/mailparse |
CentOS 6 + php5.3
安裝 mailparse
1 2 |
# yum install re2c # pear install pecl/mailparse-2.1.6 |
--
載入 extension ,檔案必須自己產生
1 2 |
vi /etc/php.d/40-mailparse.ini extension=mailparse.so |
重新啟動 httpd
1 |
# systemctl restart httpd |
CentOS 6
1 |
/etc/rd.d/init.d/httpd restart |
--
Hoyo 自己寫的解析程式
本來是用 MIME-mail-parser 這個別人寫的直接套,後來因為 X 和 XX 以及 XXX 種種的原因,在消音了無數次之後果斷自己寫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
<?php mb_internal_encoding('UTF-8'); class HoyoMailParser { function __construct($a) { define( 'FilePath', '/tmp' ); $info = array(); $section = array(); $header = array(); $j = 0; $mail = mailparse_msg_create(); mailparse_msg_parse($mail, $a); $struct = mailparse_msg_get_structure($mail); //print_r($struct); foreach($struct as $st) { $section[] = mailparse_msg_get_part($mail, $st); } foreach( $section as $k=>$s ){ $info[] = mailparse_msg_get_part_data($s); } //print_r($info); // 信件 header $h = $info[0]['headers']; foreach( $h as $k=>$v ){ //echo $v ."---\r\n"; if ( is_string($v) ) $header[$k] = mb_decode_mimeheader($v); } $this->Header = $header; //print_r( $header ); for( $i=0; $i<count($struct) ;$i++ ){ // 接收 part data $GLOBALS["catchData"] = ''; mailparse_msg_extract_part($section[$i], $a, "catchData"); $tempData = $GLOBALS["catchData"]; // [transfer-encoding] => 8bit if ( $info[$i]['headers']['transfer-encoding'] =='8bit' ){ $tempData = mb_decode_mimeheader($tempData); } // 文字編碼轉換 if ( strtolower($info[$i]['charset']) !='utf-8' ){ $tempData = mb_convert_encoding( $tempData, "UTF-8", $info[$i]['charset'] ); } // HTML if ( preg_match('/text\/html/i', $info[$i]['headers']['content-type'] ) ){ $this->Body['HTML'] = $tempData; } // 純文字 if ( preg_match('/text\/plain/i', $info[$i]['headers']['content-type'] ) ){ $this->Body['Plain'] = $tempData; } // 檔案附件 if ( $info[$i]['headers']['x-attachment-id'] !='' ){ // 儲存檔案 $ta = explode('.', mb_decode_mimeheader($info[$i]['headers']['content-type']) ); $afs = trim( array_pop($ta), '"' ); file_put_contents( FilePath .'/'. $info[$i]['headers']['x-attachment-id'] .'.'. $afs , $tempData); // 是否是內嵌圖片 if ( preg_match('/inline/i', $info[$i]['headers']['content-disposition']) ){ $this->Body['HTML'] = str_replace('cid:'.$info[$i]['headers']['x-attachment-id'], 'data:'.$info[$i]['content-type'].';base64,'. base64_encode($GLOBALS["catchData"]), $this->Body['HTML']); $this->Body['File'][$j] = array( 'Filename'=> mb_decode_mimeheader($info[$i]['disposition-filename']), 'FileContent' => base64_encode($GLOBALS["catchData"]) ); $j++; } // 一般附件 if ( $info[$i]['content-disposition'] =='attachment' ) { $this->Body['File'][$j] = array( 'Filename'=> mb_decode_mimeheader($info[$i]['disposition-filename']), 'FileContent' => base64_encode($GLOBALS["catchData"]) ); $j++; } } } return $this; } } function catchData($part) { // 檔案大於 ?? 或多次執行直到結束 因此在此只要將資料拼接即可 $GLOBALS["catchData"] .= $part; } |
--
391 total views, 1 views today