Heim > php教程 > php手册 > PHP处理postfix的邮件内容

PHP处理postfix的邮件内容

WBOY
Freigeben: 2016-06-21 08:54:55
Original
778 Leute haben es durchsucht

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

<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">"\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 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 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></li>

</ol>

Nach dem Login kopieren




Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage