起因
--
技術解析
請參考 Postfix 使用虛擬使用者 – 4. 信件解析,讓網頁看得懂
--
PHP 實作
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
<?php ini_set('display_errors',true); ini_set('error_reporting',E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED); mb_internal_encoding('UTF-8'); const path = '/var/spool/mail'; $userArray = scandir(path); $users =array(); foreach( $userArray as $k=>$user ){ if ( $user !='.' && $user !='..' ){ $users[] = $user; } } // 選取了使用者信箱 if ( $_GET['a'] =='user' ) { $mail = array(); $m = ''; $f = path .'/'. $_POST['user']; $handle = @fopen($f, "r"); $mail = $body = array(); $i = -1; if ($handle) { $header = true; while (($line = fgets($handle, 4096)) != false) { if ( preg_match('/^From /', $line) ){ $header = true; ++$i; $mail[$i] .= $line; } else{ if ( $header == true ) { $mail[$i] .= $line; } else{ $body[$i] .= $line; } } if ( preg_match('/^\n$/', $line) ) { $header = false; } } fclose($handle); } // 讀取單一信件 $m = array(); foreach( $mail as $k=>$v ){ // 將信件內容丟進 mailparse 進行解析 $mime = mailparse_msg_create(); mailparse_msg_parse($mime, $v); // 讀取信件結構 -> header & body $structure = mailparse_msg_get_structure($mime); $parts = array(); foreach($structure as $part_id) { $part = mailparse_msg_get_part($mime, $part_id); $parts[$part_id] = mailparse_msg_get_part_data($part); // 將信件結構存入陣列 $m[$k] = $parts[$part_id]; } //print_r($m); } $mail = array(); foreach( $m as $k=>$v ){ $mail[$k]['subject'] = mb_decode_mimeheader($v['headers']['subject']); } if ( count($m) >=1 ){ $Return = array( 'Result'=>true, 'Data'=>$mail ); } else { $Return = array( 'Result'=>false ); } echo json_encode($Return); exit; } // if ( $_GET['a'] =='mail' ) { $mail = array(); $m = ''; $f = path .'/'. $_POST['user']; $handle = @fopen($f, "r"); $mail = $body = array(); $i = -1; if ($handle) { $header = true; while (($line = fgets($handle, 4096)) != false) { if ( preg_match('/^From /', $line) ){ $header = true; ++$i; $mail[$i] .= $line; } else{ if ( $header == true ) { $mail[$i] .= $line; } else{ $body[$i] .= $line; } } if ( preg_match('/^\n$/', $line) ) { $header = false; } } fclose($handle); } if ( count($m) >=1 ){ $Return = array( 'Result'=>true, 'Data'=>$body[$_POST['mail']] ); } else { $Return = array( 'Result'=>false ); } echo json_encode($Return); exit; } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"/> <title></title> <style> *{ font-size: 16px; } pre{ background-color: #eee; border: 1px solid #aaa; border-radius: 8px; padding: 16px; margin: 16px; } .box{ margin: 16px; padding: 16px; border-radius: 8px; border: 1px solid #ddd; } </style> </head> <body> <div class="box"> <label>選擇使用者帳號: <select name="user" onchange="selectUser(this)"> <option value="">- 選擇信箱 -</option> <?php foreach($users as $k=>$v){ ?> <option value="<?php echo $v;?>"><?php echo $v;?></option> <?php } ?> </select> </label> </div> <?php echo $_POST['user'];?> <div class="box"> <label>選取信件: <select name="mail" onchange="readMail(this)"></select> </label> </div> <div>信件內容:</div> <pre id="MailBody"></pre> <script src="//code.jquery.com/jquery-1.11.3.min.js"></script> <script> var user = ''; function selectUser(obj){ var $this = $(obj); user = $this.val(); $.ajax({ url: '?a=user', type: 'post', data: { user: $this.val() }, dataType: 'json', success: function(json){ var h = '<option value="">- 選擇信件 -</option>'; if ( json['Result'] ==true ){ $.each( json['Data'], function(k,v){ h += '<option value="'+ k +'">'+ v['subject'] +'</option>'; } ); } $('[name="mail"]').html(h); } }); } function readMail(obj){ var $this = $(obj); $.ajax({ url: '?a=mail', type: 'post', data: { user: user, mail: $('[name="mail"]').val() }, dataType: 'json', success: function(json){ var h = ''; if ( json['Result'] ==true ){ h += json['Data']; } $('#MailBody').html(h); } }); } </script> </body> </html> |
--
運作實例
--
330 total views, 1 views today