-
Notifications
You must be signed in to change notification settings - Fork 72
/
testdriver1.php
304 lines (265 loc) · 12.4 KB
/
testdriver1.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<?php
//error_reporting(E_ALL);
require_once("bounce_driver.class.php");
$bouncehandler = new Bouncehandler();
if(!empty($_GET['testall'])){
$files = get_sorted_file_list('eml');
if (is_array($files)) {
echo "<P>File Tests:</P>\n";
foreach($files as $file) {
echo "<a href=\"".$_SERVER['PHP_SELF']."?eml=".urlencode($file)."\">$file</a> ";
$bounce = file_get_contents("eml/".$file);
$multiArray = $bouncehandler->get_the_facts($bounce);
if( !empty($multiArray[0]['action'])
&& !empty($multiArray[0]['status'])
&& !empty($multiArray[0]['recipient']) ){
print " - Passed<br>\n";
}
else{
print "<font color=red> - WRONG</font><br>\n";
print "<pre>\n";
print_r($multiArray[0]);
print "</pre>\n";
}
}
}
}
?>
<h1>bounce_driver.class.php -- Version 7.4</h1>
<P>
Chris Fortune ~ <a href="http://cfortune.kics.bc.ca">http://cfortune.kics.bc.ca</a>
</P>
<P>June 19, 2014</P>
<UL>
<LI>make auto-responder identification table driven</LI>
<LI>make bounce_statuscodes.php (prev rfc1893_status_codes.php) generated from IANA list
php Make_statuscodes.php >bounce_statuscodes.php</LI>
<LI>allow for rfc status codes with 2 digits in the 3rd paramater</LI>
<LI>more supression for php notifications on undefined data</LI>
<LI>better detection and field definition for FBL handling</LI>
<LI>remove spaces in joined header lines</LI>
<LI>remove invalid/redundant implode operations</LI>
<LI>add two new sample emails 61,62</LI>
<LI>add command line test tool (cmdline_test.php)</LI>
<P>
July 4, 2013
</P>
<P>
Replaced deprecated split() function.
Added auto-responder identification filter.
Suppressed php Notice errors.
<P>
<HR>
<!-- onclick="alert('not ready just yet'); return false;" -->
<a href="php.bouncehandler.v7.4.zip">Download source code</a>
<HR>
<P>
Feb 3, 2011
</P>
<P>
Hey! Class is no longer static, it is rewritten in dynamic <code>$this-></code> notation. It's much easier to customize now. If you are upgrading from a previous version, you will need to rewrite your method invocation code.
<P>
<HR>
<!-- onclick="alert('not ready just yet'); return false;" -->
<a href="php.bouncehandler.v7.4.zip">Download source code</a>
<HR>
<P>
This bounce handler Attempts to parse Multipart reports for hard bounces, according to <a href='http://www.faqs.org/rfcs/rfc1892.html'>RFC1892</a> (RFC 1892 - The Multipart/Report Content Type for the Reporting of Mail System Administrative Messages) and <a href='http://www.faqs.org/rfcs/rfc1894.html'>RFC1894</a> (RFC 1894 - An Extensible Message Format for Delivery Status Notifications). We can reuse this for any well-formed bounces. </P>
<P>
It handles FBL (Feedback Loop) emails, if they are in Abuse Feedback Reporting Format, <a href="http://en.wikipedia.org/wiki/Abuse_Reporting_Format#Abuse_Feedback_Reporting_Format_.28ARF.29">ARF</a> (It even handles Hotmail's ridiculous attempts at FBL). DKIM parsing is not yet implemented.
</P>
<P>
You can configure custom regular expressions to find any web beacons you may have put in your outgoing mails, in either the mail body or an x-header field. (see source code for examples). You can use it to track data (eg, recipient, list, mail run, etc...) by sending out unique ids, then parsing them from the bounces. This is especially useful when parsing FBL's, because usually all recipient fields have been removed (redacted).
</P>
<P>
If the bounce is not well formed, it tries to extract some useful information anyway. Currently Postfix and Exim are supported, partially. You can edit the function <code>get_the_facts()</code> if you want to add a parser for your own busted MTA. Please forward any useful & reuseable code to the keeper of this class. <a href="http://cfortune.kics.bc.ca/">Chris Fortune</a></P>
<?
// a perl regular expression to find a web beacon in the email body
$bouncehandler->web_beacon_preg_1 = "/u=([0-9a-fA-F]{32})/";
$bouncehandler->web_beacon_preg_2 = "/m=(\d*)/";
// find a web beacon in an X-header field (in the head section)
$bouncehandler->x_header_search_1 = "X-ctnlist-suid";
//$bouncehandler->x_header_search_2 = "X-sumthin-sumpin";
if($_GET['eml']){
echo "<HR><P><B>".$_GET['eml']."</B> -- ";
echo "<a href=\"testdriver1.php\">View a different bounce</a></P>";
$bounce = file_get_contents("eml/".$_GET['eml']);
echo "<P>Quick and dirty bounce handler:<BR>
useage:
<blockquote><code>
require_once(\"bounce_driver.class.php\");<br>
\$bouncehandler = new Bouncehandler();<br>
\$multiArray = \$bouncehandler->get_the_facts(\$strEmail); , or<br>
\$multiArray = \$bouncehandler->parse_email(\$strEmail);</code> (same thing)
</blockquote>
returns a multi-dimensional associative array of bounced recipient addresses and their SMTP status codes (if available)<br><br>
<code>print_r(\$multiArray);</code>
<P>";
$multiArray = $bouncehandler->get_the_facts($bounce);
echo "<TEXTAREA COLS=100 ROWS=".(count($multiArray)*8).">";
//print_r($bouncehandler); exit;
print_r($multiArray);
echo "</TEXTAREA>";
$bounce = $bouncehandler->init_bouncehandler($bounce, 'string');
list($head, $body) = preg_split("/\r\n\r\n/", $bounce, 2);
}
else{
print "<OL><LI><a href=\"".$_SERVER['PHP_SELF']."?testall=true\">Test All Sample Bounce E-mails</a>\n\n";
print "<LI>Or, select a bounce email to view the parsed results:</OL>\n";
$files = get_sorted_file_list('eml');
if (is_array($files)) {
reset($files);
echo "<P>Files:</P>\n";
foreach($files as $file) {
echo "<a href=\"".$_SERVER['PHP_SELF']."?eml=".urlencode($file)."\">$file</a><br>\n";
}
}
exit;
}
echo "<P>Will return recipient's email address, the RFC1893 error code, and the action. Action can be one of the following:
<UL>
<LI>'transient'(temporary problem),
<LI>'failed' (permanent problem),
<LI>'autoreply' (a vacation auto-response), or
<LI>'' (nothing -- not classified).</UL>";
echo "<P>You could dereference the \$multiArray in a 'for loop', for example...</P>
<PRE>
foreach(\$multiArray as \$the){
switch(\$the['action']){
case 'failed':
//do something
kill_him(\$the['recipient']);
break;
case 'transient':
//do something else
\$num_attempts = delivery_attempts(\$the['recipient']);
if(\$num_attempts > 10){
kill_him(\$the['recipient']);
}
else{
insert_into_queue(\$the['recipient'], (\$num_attempts+1));
}
break;
case 'autoreply':
//do something different
postpone(\$the['recipient'], '7 days');
break;
default:
//don't do anything
break;
}
}
Or, if it is an FBL, you could use the class variables to access the
data (Unlike Multipart-reports, FBL's report only one bounce)
You could also use them if the output array has only one index:
if(\$bouncehandler->type == 'fbl'
or count(\$bouncehandler->output) == 1)
{
echo \$bouncehandler->action : .... <i>$bouncehandler->action</i>
echo \$bouncehandler->status : .... <i>$bouncehandler->status</i>
echo \$bouncehandler->recipient : .... <i>$bouncehandler->recipient</i>
echo \$bouncehandler->feedback_type : .... <i>$bouncehandler->feedback_type</i>
}
These class variables are safe for all bounce types:
echo \$bouncehandler->type : .... <i>$bouncehandler->type</i>
echo \$bouncehandler->subject : .... <i>$bouncehandler->subject</i>
echo \$bouncehandler->web_beacon_1 : .... <i>$bouncehandler->web_beacon_1</i>
echo \$bouncehandler->web_beacon_2 : .... <i>$bouncehandler->web_beacon_2</i>
echo \$bouncehandler->x_header_beacon_1 : .... <i>$bouncehandler->x_header_beacon_1</i>
echo \$bouncehandler->x_header_beacon_2 : .... <i>$bouncehandler->x_header_beacon_2</i>
</PRE>
";
echo "<P>That's all you need to know, but if you want to get more complicated you can. All methods are public. See source code.</P><BR>";
echo "<hr><h2>Here is the parsed head</h2>\n";
$head_hash = $bouncehandler->parse_head($head);
echo "<TEXTAREA COLS=100 ROWS=".(count($head_hash)*2.7).">";
print_r($head_hash);
echo "</TEXTAREA>";
if($bouncehandler->is_hotmail_fbl) echo "RRRRRR".$bouncehandler->recipient ;
exit;
if ($bouncehandler->is_RFC1892_multipart_report($head_hash)){
print "<h2><font color=red>Looks like an RFC1892 multipart report</font></H2>";
}
else if($bouncehandler->looks_like_an_FBL){
print "<h2><font color=red>It's a Feedback Loop, ";
if($bouncehandler->is_hotmail_fbl){
print " in Hotmail Doofus Format (HDF?)</font></H2>";
}else{
print " in Abuse Feedback Reporting format (ARF)</font></H2>";
echo "<TEXTAREA COLS=100 ROWS=12>";
print_r($bouncehandler->fbl_hash);
echo "</TEXTAREA>";
}
}
else {
print "<h2><font color=red>Not an RFC1892 multipart report</font></H2>";
echo "<TEXTAREA COLS=100 ROWS=100>";
print_r($body);
echo "</TEXTAREA>";
exit;
}
echo "<h2>Here is the parsed report</h2>\n";
echo "<P>Postfix adds an appropriate X- header (X-Postfix-Sender:), so you do not need to create one via phpmailer. RFC's call for an optional Original-recipient field, but mandatory Final-recipient field is a fair substitute.</P>";
$boundary = $head_hash['Content-type']['boundary'];
$mime_sections = $bouncehandler->parse_body_into_mime_sections($body, $boundary);
$rpt_hash = $bouncehandler->parse_machine_parsable_body_part($mime_sections['machine_parsable_body_part']);
echo "<TEXTAREA COLS=100 ROWS=".(count($rpt_hash)*16).">";
print_r($rpt_hash);
echo "</TEXTAREA>";
echo "<h2>Here is the error status code</h2>\n";
echo "<P>It's all in the status code, if you can find one.</P>";
for($i=0; $i<count($rpt_hash['per_recipient']); $i++){
echo "<P>Report #".($i+1)."<BR>\n";
echo $bouncehandler->find_recipient($rpt_hash['per_recipient'][$i]);
$scode = $rpt_hash['per_recipient'][$i]['Status'];
echo "<PRE>$scode</PRE>";
echo $bouncehandler->fetch_status_messages($scode);
echo "</P>\n";
}
echo "<h2>The Diagnostic-code</h2> <P>is not the same as the reported status code, but it seems to be more descriptive, so it should be extracted (if possible).";
for($i=0; $i<count($rpt_hash['per_recipient']); $i++){
echo "<P>Report #".($i+1)." <BR>\n";
echo $bouncehandler->find_recipient($rpt_hash['per_recipient'][$i]);
$dcode = $rpt_hash['per_recipient'][$i]['Diagnostic-code']['text'];
if($dcode){
echo "<PRE>$dcode</PRE>";
echo $bouncehandler->fetch_status_messages($dcode);
}
else{
echo "<PRE>couldn't decode</PRE>";
}
echo "</P>\n";
}
echo "<H2>Grab original To: and From:</H2>\n";
echo "<P>Just in case we don't have an Original-recipient: field, or a X-Postfix-Sender: field, we can retrieve information from the (optional) returned message body part</P>\n";
$head = $bouncehandler->get_head_from_returned_message_body_part($mime_sections);
echo "<P>From: ".$head['From']."<br>To: ".$head['To']."<br>Subject: ".$head['Subject']."</P>";
echo "<h2>Here is the body in RFC1892 parts</h2>\n";
echo "<P>Three parts: [first_body_part], [machine_parsable_body_part], and [returned_message_body_part]</P>";
echo "<TEXTAREA cols=100 rows=100>";
print_r($mime_sections);
echo "</TEXTAREA>";
/*
$status_code = $bouncehandler->format_status_code($rpt_hash['per_recipient'][$i]['Status']);
$status_code_msg = $bouncehandler->fetch_status_messages($status_code['code']);
$status_code_remote_msg = $status_code['text'];
$diag_code = $bouncehandler->format_status_code($rpt_hash['per_recipient'][$i]['Diagnostic-code']['text']);
$diag_code_msg = $bouncehandler->fetch_status_messages($diag_code['code']);
$diag_code_remote_msg = $diag_code['text'];
*/
function get_sorted_file_list($d){
$fs = array();
if ($h = opendir($d)) {
while (false !== ($f = readdir($h))) {
if($f=='.' || $f=='..') continue;
$fs[] = $f;
}
closedir($h);
sort($fs, SORT_STRING);//
}
return $fs;
}
?>