<ol class="dp-c"><li class="alt"><span><span>01 <?php </span></span></li><li><span> </span></li><li class="alt"><span>02 </span></li><li><span> </span></li><li class="alt"><span>03 #从输入读取到所有的邮件内容 </span></li><li><span> </span></li><li class="alt"><span>04 </span><span class="vars">$email</span><span> = </span><span class="string">""</span><span>; </span></li><li><span> </span></li><li class="alt"><span>05 </span><span class="vars">$fd</span><span> = </span><span class="func">fopen</span><span>(</span><span class="string">"php://stdin"</span><span>, </span><span class="string">"r"</span><span>); </span></li><li><span> </span></li><li class="alt"><span>06 </span><span class="keyword">while</span><span> (!</span><span class="func">feof</span><span>(</span><span class="vars">$fd</span><span>)) { </span></li><li><span> </span></li><li class="alt"><span>07 </span><span class="vars">$email</span><span> .= </span><span class="func">fread</span><span>(</span><span class="vars">$fd</span><span>, 1024); </span></li><li><span> </span></li><li class="alt"><span>08 } </span></li><li><span> </span></li><li class="alt"><span>09 fclose(</span><span class="vars">$fd</span><span>); </span></li><li><span> </span></li><li class="alt"><span>10 </span></li><li><span> </span></li><li class="alt"><span>11 #记录所有的内容,测试 </span></li><li><span> </span></li><li class="alt"><span>12 </span><span class="func">file_put_contents</span><span>(</span><span class="string">"/tmp/mail/"</span><span>.time(), </span><span class="vars">$email</span><span>); </span></li><li><span> </span></li><li class="alt"><span>13 </span></li><li><span> </span></li><li class="alt"><span>14 #处理邮件 </span></li><li><span> </span></li><li class="alt"><span>15 </span><span class="vars">$lines</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">"\n"</span><span>, </span><span class="vars">$email</span><span>); </span></li><li><span> </span></li><li class="alt"><span>16 </span></li><li><span> </span></li><li class="alt"><span>17 </span><span class="comment">// empty vars </span><span> </span></li><li><span> </span></li><li class="alt"><span>18 </span><span class="vars">$from</span><span> = </span><span class="string">""</span><span>; </span></li><li><span> </span></li><li class="alt"><span>19 </span><span class="vars">$date</span><span> = </span><span class="string">""</span><span>; </span></li><li><span> </span></li><li class="alt"><span>20 </span><span class="vars">$subject</span><span> = </span><span class="string">""</span><span>; </span></li><li><span> </span></li><li class="alt"><span>21 </span><span class="vars">$message</span><span> = </span><span class="string">""</span><span>; </span></li><li><span> </span></li><li class="alt"><span>22 </span><span class="vars">$splittingheaders</span><span> = true; </span></li><li><span> </span></li><li class="alt"><span>23 </span></li><li><span> </span></li><li class="alt"><span>24 </span><span class="keyword">for</span><span> (</span><span class="vars">$i</span><span>=0; </span><span class="vars">$i</span><span><</span><span class="func">count</span><span>(</span><span class="vars">$lines</span><span>); </span><span class="vars">$i</span><span>++) { </span></li><li><span> </span></li><li class="alt"><span>25 </span><span class="keyword">if</span><span> (</span><span class="vars">$splittingheaders</span><span>) { </span></li><li><span> </span></li><li class="alt"><span>26 </span></li><li><span> </span></li><li class="alt"><span>27 </span><span class="comment">// look out for special headers </span><span> </span></li><li><span> </span></li><li class="alt"><span>28 </span><span class="keyword">if</span><span> (preg_match(</span><span class="string">"/^Subject: (.*)/"</span><span>, </span><span class="vars">$lines</span><span>[</span><span class="vars">$i</span><span>], </span><span class="vars">$matches</span><span>)) { </span></li><li><span> </span></li><li class="alt"><span>29 </span><span class="vars">$subject</span><span> = </span><span class="vars">$matches</span><span>[1]; </span></li><li><span> </span></li><li class="alt"><span>30 } </span></li><li><span> </span></li><li class="alt"><span>31 </span><span class="keyword">if</span><span> (preg_match(</span><span class="string">"/^From: (.*)/"</span><span>, </span><span class="vars">$lines</span><span>[</span><span class="vars">$i</span><span>], </span><span class="vars">$matches</span><span>)) { </span></li><li><span> </span></li><li class="alt"><span>32 </span><span class="keyword">if</span><span>(</span><span class="func">strpos</span><span>(</span><span class="vars">$lines</span><span>[</span><span class="vars">$i</span><span>],</span><span class="string">"<"</span><span>)){ </span></li><li><span> </span></li><li class="alt"><span>33 </span><span class="comment">//the name exist too in from header </span><span> </span></li><li><span> </span></li><li class="alt"><span>34 </span><span class="vars">$data</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">'<'</span><span>,</span><span class="vars">$lines</span><span>[</span><span class="vars">$i</span><span>]); </span></li><li><span> </span></li><li class="alt"><span>35 </span><span class="vars">$from</span><span> = </span><span class="func">substr</span><span>(trim(</span><span class="vars">$data</span><span>[1]),0,-1); </span></li><li><span> </span></li><li class="alt"><span>36 }</span><span class="keyword">else</span><span>{ </span></li><li><span> </span></li><li class="alt"><span>37 </span><span class="comment">//only the mail </span><span> </span></li><li><span> </span></li><li class="alt"><span>38 </span><span class="vars">$from</span><span> = </span><span class="vars">$matches</span><span>[1]; </span></li><li><span> </span></li><li class="alt"><span>39 } </span></li><li><span> </span></li><li class="alt"><span>40 } </span></li><li><span> </span></li><li class="alt"><span>41 </span><span class="keyword">if</span><span> (preg_match(</span><span class="string">"/^Date: (.*)/"</span><span>, </span><span class="vars">$lines</span><span>[</span><span class="vars">$i</span><span>], </span><span class="vars">$matches</span><span>)) { </span></li><li><span> </span></li><li class="alt"><span>42 </span><span class="vars">$date</span><span> = </span><span class="vars">$matches</span><span>[1]; </span></li><li><span> </span></li><li class="alt"><span>43 } </span></li><li><span> </span></li><li class="alt"><span>44 } </span><span class="keyword">else</span><span> { </span></li><li><span> </span></li><li class="alt"><span>45 </span><span class="comment">// not a header, but message </span><span> </span></li><li><span> </span></li><li class="alt"><span>46 </span><span class="vars">$message</span><span> .= </span><span class="vars">$lines</span><span>[</span><span class="vars">$i</span><span>].</span><span class="string">"\n"</span><span>; </span></li><li><span> </span></li><li class="alt"><span>47 } </span></li><li><span> </span></li><li class="alt"><span>48 </span></li><li><span> </span></li><li class="alt"><span>49 </span><span class="keyword">if</span><span> (trim(</span><span class="vars">$lines</span><span>[</span><span class="vars">$i</span><span>])==</span><span class="string">""</span><span>) { </span></li><li><span> </span></li><li class="alt"><span>50 </span><span class="comment">// empty line, header section has ended </span><span> </span></li><li><span> </span></li><li class="alt"><span>51 </span><span class="vars">$splittingheaders</span><span> = false; </span></li><li><span> </span></li><li class="alt"><span>52 } </span></li><li><span> </span></li><li class="alt"><span>53 } </span></li><li><span> </span></li><li class="alt"><span>54 </span></li><li><span> </span></li><li class="alt"><span>55 </span><span class="vars">$when</span><span> = </span><span class="func">date</span><span>(</span><span class="string">"Y-m-d G:i:s"</span><span>); </span></li><li><span> </span></li><li class="alt"><span>56 </span><span class="vars">$data</span><span> = </span><span class="func">explode</span><span>(</span><span class="string">'@'</span><span>,</span><span class="vars">$from</span><span>); </span></li><li><span> </span></li><li class="alt"><span>57 </span><span class="vars">$username</span><span> = </span><span class="vars">$data</span><span>[0]; </span></li><li><span> </span></li><li class="alt"><span>58 </span></li><li><span> </span></li><li class="alt"><span>59 #记录到数据库 </span></li><li><span> </span></li><li class="alt"><span>60 </span><span class="vars">$sql</span><span> = </span><span class="string">"insert into mails ( `username`, `from`, `subject`, `date`, `message`) values ( '$username', '$from', '$subject', '$when', '$message')"</span><span>; </span></li><li><span> </span></li><li class="alt"><span>61 </span></li><li><span> </span></li><li class="alt"><span>62 #测试 </span></li><li><span> </span></li><li class="alt"><span>63 </span><span class="func">file_put_contents</span><span>(</span><span class="string">"/tmp/mail2.log"</span><span>, </span><span class="vars">$sql</span><span>); </span></li><li><span> </span></li><li class="alt"><span>64 ?> </span></span></li></ol>