Heim >Backend-Entwicklung >PHP-Tutorial >PHP处理postfix的邮件内容_PHP教程
<ol class="dp-c"> <li class="alt"><span><span>01 <?php </span></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">" "</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 class="func">count</span><span>(</span><span class="vars">$lines</span><span>); </span><span class="vars">$i</span><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 class="vars">$lines</span><span>[</span><span class="vars">$i</span><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 clas> <p align="left"></p> <div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/478830.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/478830.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">01?php 02 03#从输入读取到所有的邮件内容 04 $email = ; 05 $fd = fopen ( php://stdin , r ); 06 while (! feof ( $fd )){ 07 $email .= fread ( $fd ,1024); 08} 09fclose( $fd...</span> </div> <div class="art_confoot"></div> </li> </ol>