Coder Social home page Coder Social logo

cfortune / php-bounce-handler Goto Github PK

View Code? Open in Web Editor NEW
116.0 116.0 73.0 2.74 MB

This class can be used to parse bounced message reports. It parses e-mail messages with multipart report content type formatted according to RFC 1892 and 1894 documents. If the bounce is not well formed, it also tries to extract some useful information. Exim is supported, partially. Please send bounce messages from your busted MTA, I'll extend the class to support them. It can extract the the address of the recipient that bounced as well the mail server response status code.

Home Page: http://anti-spam-man.com/php_bouncehandler/

PHP 100.00%

php-bounce-handler's People

Contributors

cfortune avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-bounce-handler's Issues

Bounce-Handler not working for me

Hello cfortune,

I am using your repo to filter bounce mail bun it doesn't work for me. Sample email is below

Delivered-To: [email protected]

Received: by 10.27.14.129 with SMTP id 1csp1169733wlo; Sun, 8 Feb 201500:46:13 -0800
(PST)

X-Received: by 10.140.92.161 with SMTP id b30mr26431246qge.59.1423385171989;Sun, 08 Feb
2015 00:46:11 -0800 (PST)

Return-Path: <>

Received: from mail-qa0-x248.google.com
(mail-qa0-x248.google.com.[2607:f8b0:400d:c00::248]) by mx.google.com with
ESMTPS idn94si9685903qgn.48.2015.02.08.00.46.11 for
<[email protected]>(version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256
bits=128/128); Sun, 08Feb 2015 00:46:11 -0800 (PST)

Received-SPF: none (google.com: mail-qa0-x248.google.com does not designatepermitted sender
hosts) client-ip=2607:f8b0:400d:c00::248;

Authentication-Results: mx.google.com; spf=none (google.com:mail-qa0-x248.google.com does not
designate permitted sender hosts)smtp.mail=; dkim=pass
[email protected]

Received: by mail-qa0-f72.google.com with SMTP id j7so59032827qaq.3
for<[email protected]>; Sun, 08 Feb 2015 00:46:11 -0800 (PST)

DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com;s=20120113;
h=mime-version:from:to:subject:message-id:date:content-type:content-transfer-encoding;
bh=o43EsPqHa1KK8tIxxcjBwa3oFvhDNXnoG9Ibl8wu5Lo=;b=m9Ol8HWy8xLWN7TvhlJqaC3UouzELkeKlYNPyanC++aYG5/O+VwNKpOqzcb6R7wNPTpROTh/6cWY4I5J2O26Cy3tMLc+U6EzlagMYKH+8/AqHEzu16avm6/BxIaLHQOIUKNZ28vCAzENjShr8dANswIuw9bBcQamtlKjlkOk1yMWFGdDhlfbtAyPKL+AHt9urgQ3lCFT1t4yag55jsQgaD3x1YH7DNnxgbCw75IZE1hHIi8cDdqw17exqnPAY37iOrx+MmguMCqiSznh09VUizT9v3R0AoBsx5aXt7ZmzhQSwBEEq3fOH+HUDFnGZ6lyT+z/6oOHCT95os85so
XNxA==

X-Received: by 10.236.23.229 with SMTP id v65mr3994160yhv.174.1423385171050;Sun, 08 Feb
2015 00:46:11 -0800 (PST)

MIME-Version: 1.0

Return-Path: <>

Received: by 10.236.23.229 with SMTP id v65mr3462457yhv.174; Sun, 08 Feb 201500:46:11
-0800 (PST)

From: Mail Delivery Subsystem <[email protected]>

To: [email protected]

Subject: Delivery Status Notification (Delay)

Message-ID: <[email protected]>

Date: Sun, 08 Feb 2015 08:46:11 +0000

Content-Type: text/plain;
charset="UTF-8"

Content-Transfer-Encoding: quoted-printable

Body: This is an automatically generated Delivery Status Notification

THIS IS A WARNING MESSAGE ONLY.

YOU DO NOT NEED TO RESEND YOUR MESSAGE.

Delivery to the following recipient has been delayed:

[email protected]

Message will be retried for 1 more day(s)

Technical details of temporary failure:=20
The recipient server did not accept our requests to connect. Learn more at =
http://support.google.com/mail/bin/answer.py?answer=3D7720=20
[(0) mail.kochar.com. [182.50.146.128]:25: socket error]

----- Original message -----

DKIM-Signature: v=3D1; a=3Drsa-sha256; c=3Drelaxed/relaxed;
d=3Drtcamp.com; s=3Dgoogle;
h=3Dmessage-id:date:from:subject:to:mime-version:content-type
:content-transfer-encoding:sender;
bh=3D/0M3tqpeBCRwnJNk0tnFEMZVdwVTABrINVKi8tnYCB4=3D;
b=3DW1Fnme6JrJC8t12RSroptxgjQfnqYhFbN11myzpjFG2rIHA9VE0FK9V4Uls5O4F=
rKH
zRYEfAECd787qcxEN1n0M1MCUGB2hqg8cltYEWDvr6csEGTzd7XHFyZW/0Xxvc7eqL=
tc
ws++f4wOdNVf+Vss2uzwk1zBZkD4PjtaDqZ7o=3D
X-Google-DKIM-Signature: v=3D1; a=3Drsa-sha256; c=3Drelaxed/relaxed;
d=3D1e100.net; s=3D20130820;
h=3Dx-gm-message-state:message-id:date:from:subject:to:mime-version
:content-type:content-transfer-encoding:sender;
bh=3D/0M3tqpeBCRwnJNk0tnFEMZVdwVTABrINVKi8tnYCB4=3D;
b=3DGDGFQ1Zkk3XPN8m0Unr2F3034v1/QEqBbYUf68Tg9fT0q75pqD/mWn4BZSB59Zq=
70E
bh7EGzImF/48fu3zMkGUxyFxumW5qob1rKcAdKDKFJ/XJOgKV0yaiytspwhHeyZrR1=
ZD
/cj8mjNia4xpwBOCyNg6Wv0xhmuPng9foN0/rFR7wQWxxR13fh87IFq2w78fWUltJ5=
Ll
nzUogOlkIwBJl0jHnnZjnXZL6sPQW2csRUVgzMnpCLVKgHHctoZB/dyRs9TIGYFLek=
0H
ol0LW+FErig+LR19Bl4GueKG00/zwOgnsYi22eNsu32d5LlXr50TfOHmWo/KGoIBNW=
8/
Xh/A=3D=3D
X-Gm-Message-State: ALoCoQmvG5GmMaiPW9pjgBco9/YCpca5mIpGzMNGsDoJktJiqDogZiW=
4s5IPsAB9EuBN2+mWTz21
X-Received: by 10.236.23.229 with SMTP id v65mr563084yhv.174.1423210102366;
Fri, 06 Feb 2015 00:08:22 -0800 (PST)
Return-Path:
Received: from smtp.gmail.com (li.rtcamp.com. [23.92.30.145])
by mx.google.com with ESMTPSA id o47sm1111741yhd.48.2015.02.06.00.0=
8.21
(version=3DTLSv1 cipher=3DECDHE-RSA-RC4-SHA bits=3D128/128);
Fri, 06 Feb 2015 00:08:21 -0800 (PST)
Message-ID:
Date: Fri, 06 Feb 2015 08:08:20 +0000
From: rtCamp
X-Helpdesk: notification
Subject: [Helpdesk #80061] =E2=80=9C0=E2=80=9D COST CAMPUS RECRUITMENT DRIV=
E for B.Tech/MCA 2015
passing out students
To: [email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected]
MIME-Version: 1.0
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable
Sender:=20

----- End of message -----

Fatal error line 595

Using latest version 7.3

For a certain message I'm getting error:

PHP Fatal error: Cannot use string offset as an array in ... bounce_driver.class.php on line 595

line 595: $stat = $ret['code'][0];

I think I can fix it temporarily for me using
if (!is_array($ret)) {
return $ret;
}

One thing which might be problematic could be the received 'for' address bounced emailed had. That certain Bounced email had address as

for [email protected]; Mon, 4 Nov 2013 14:00:24 -0500 (EST)
Content-Transfer-Encoding: quoted-printable

undefinded v7.3

in testdriver.php add: ini_set('display_errors', 'On');

Iam using php framework Laravel 3.2.x with error display on.
I get:
Message:

Undefined variable: autorespondlist
Location:

bounce_driver.class.php on line 85

because of Problematic last line which is utf encoded. ...#163.com

After removing that line there is next error:

Undefined index: Subject
Location: bounce_driver.class.php on line 455

Notice: Undefined index: per_recipient in bounce_driver.class.php on line 477

should be ['boundary'] in:
function is_RFC1892_multipart_report(){
return @$this->head_hash['Content-type']['type']=='multipart/report'
&& @$this->head_hash['Content-type']['report-type']=='delivery-status'
&& @$this->head_hash['Content-type'][boundary]!=='';

How to use with php_imap?

Hello,

As far as I can see, this class uses the raw source of an email to work out whether it's a bounce or not.

Is it possible to use this class with php_imap which doesn't return the raw source of a message?

Something like the following, but obviously imap_body only returns the body and not the whole message:

    $body = imap_body($mbox, $i);

$multi_array = $bouncehandler->get_the_facts($body);

var_export($multi_array);

Any tips or pointers would be appreciated.

Thanks,

Alex

Silent dying in init

PHP 8.1, forced march to use this PHP version

Was using BounceHandler 5.4 (still did the job). But neither that version or 7.4 work onder PHP 8.1.

In both cases, silently aborts during BounceHandler::init_bouncehandler. Changed error_reporting to all, but still bombs quietly.

Poor coding!

Did you test this with debug mode active?

There is a ton of Notice undefined index... is it too hard to add isset, validate all assigned var. its a parser script. should be more solid than that.

deprecated, undefinded v7

Function split() is deprecated,

Undefined index: Received
Location:bounce_driver.class.php on line 412

Undefined index: X-loop

Use of undefined constant boundary - assumed 'boundary' - line405
Undefined index: Feedback-type line269
Undefined index: Received line453

How to get the email text

Hi, we are trying to implement your class in our code to check bounces but we are getting the following error:
#0 /vagrant/leads/cron/bounce/bounce_driver.class.php(500):
#1 /vagrant/leads/cron/bounce/bounce_driver.class.php(431): BounceHandler->standard_parser
#2 /vagrant/leads/cron/bounce/bounce_driver.class.php(107): BounceHandler->parse_head
#3 /vagrant/leads/cron/bouncemailparser.php(36): BounceHandler->get_the_facts
#4 /vagrant/leads/cron/bouncemailparser.php(74): BounceMailParser->run

To get the emails we are using imap_open in the following way:

$conn = imap_open($host, $user, $pass, OP_SILENT) or die(imap_last_error());

$num_msgs = imap_num_msg($conn);
$email_addresses = array();
$delete_addresses = array();
for($n = 1; $n <= $num_msgs; $n++)
{
$bounce = imap_fetchheader($conn, $n) . imap_body($conn, $n); //entire message
$multiArray = $bouncehandler->get_the_facts($bounce);
}

Boundary parsing

In bounce_driver.class.php on line 417 in function parse_body_into_mime_sections there occurs a problem with boundaries ending with == because =\r\n gets stripped away at line 309.
An easy fix (maybe not the best) is to rtrim the = from $boundary.

    function parse_body_into_mime_sections($body, $boundary){
        if(!$boundary) return array();
        if(is_array($body)) $body = implode("\r\n", $body);
        $body = explode(rtrim($boundary, '='), $body);
        $mime_sections['first_body_part']            = @$body[1];
        $mime_sections['machine_parsable_body_part'] = @$body[2];
        $mime_sections['returned_message_body_part'] = @$body[3];
        return $mime_sections;
    }

Sample bounce message:

Return-path: <>
Envelope-to: [email protected]
Delivery-date: Thu, 20 Mar 2014 12:03:05 +0100
Received: from mail2.bemta5.messagelabs.com ([195.245.231.131])
    by mailserver.domain.domain with esmtps (TLSv1:AES256-SHA:256)
    (Exim 4.73 (FreeBSD))
    id 1WQakY-0006Ke-3E
    for [email protected]; Thu, 20 Mar 2014 12:03:05 +0100
Received: from [85.158.136.35:12059] by server-10.bemta-5.messagelabs.com id 57/A5-27081-4EACA235; Thu, 20 Mar 2014 11:03:00 +0000
X-Msg-Ref: server-16.tower-125.messagelabs.com!1395313183!33967189!1
X-Originating-IP: [195.245.231.134]
X-StarScan-Received:
X-StarScan-Version: 6.11.1; banners=-,-,-
X-VirusChecked: Checked
Received: (qmail 11713 invoked from network); 20 Mar 2014 10:59:43 -0000
Received: from mail5.bemta5.messagelabs.com (HELO mail5.bemta5.messagelabs.com) (195.245.231.134)
  by server-16.tower-125.messagelabs.com with DHE-RSA-AES256-SHA encrypted SMTP; 20 Mar 2014 10:59:43 -0000
Received: by server-14.bemta-5.messagelabs.com id DD/21-15696-F1ACA235; Thu, 20 Mar 2014 10:59:43 +0000
From: Mail Delivery System <[email protected]>
To: <[email protected]>
Subject: Mail Delivery Failure
Date: Thu, 20 Mar 2014 10:59:43 +0000
Content-Type: multipart/report; report-type=delivery-status;
  boundary="ONHrN715MUZyqWppCw12MuNjL3IsvuuVQXcdmQ=="
X-Exim-Version: 4.73 (build at 17-Jan-2011 14:55:22)
X-Date: 2014-03-20 12:03:05
X-Connected-IP: 195.245.231.131:39383
X-Spam-Threshold: 5
X-Spam-Score: -4.1
X-Spam-Score-Int: -40
X-Spam-Bar: ----
X-Spam-Flag: NO
X-Delivered-To: [email protected]
X-Message-Age: 3

--ONHrN715MUZyqWppCw12MuNjL3IsvuuVQXcdmQ==
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

This is the mail delivery agent at messagelabs.com.

I was unable to deliver your message to the following addresses:

[email protected]

Reason: 550 5.1.1 <[email protected]>... User unknown

The message subject was: Diabetes Nieuwsbrief maart 2014
The message date was: Thu, 20 Mar 2014 11:59:43 +0100
The message identifier was: 4B/21-15696-F1ACA235
The message reference was: server-9.tower-125.messagelabs.com!1395313182!24=
402582!1

Please do not reply to this email as it is sent from an unattended mailbox.
Please visit www.messagelabs.com/support for more details
about this error message and instructions to resolve this issue.


--ONHrN715MUZyqWppCw12MuNjL3IsvuuVQXcdmQ==
Content-Type: message/delivery-status

Arrival-Date: Thu, 20 Mar 2014 10:59:43 +0000
Reporting-MTA: dns; server-14.bemta-5.messagelabs.com

Remote-MTA: dns; 194.151.80.130
Diagnostic-Code: smtp; 550 5.1.1 <[email protected]>... User unknown
Last-Attempt-Date: Thu, 20 Mar 2014 10:59:43 +0000
Final-Recipient: rfc822; [email protected]
Status: 5.1.1
Action: failed

--ONHrN715MUZyqWppCw12MuNjL3IsvuuVQXcdmQ==
Content-Type: text/rfc822-headers

Return-Path: <[email protected]>
Received: from [85.158.136.35:41051] by server-14.bemta-5.messagelabs.com id 4B/21-15696-F1ACA235; Thu, 20 Mar 2014 10:59:43 +0000
X-Env-Sender: [email protected]
X-Msg-Ref: server-9.tower-125.messagelabs.com!1395313182!24402582!1
X-Originating-IP: [33.33.33.33]
X-SpamReason: No, hits=0.8 required=7.0 tests=HTML_MESSAGE,
  MIME_HTML_MOSTLY,MPART_ALT_DIFF
X-StarScan-Received:
X-StarScan-Version: 6.11.1; banners=-,-,viecuri.nl
X-VirusChecked: Checked
Received: (qmail 4698 invoked from network); 20 Mar 2014 10:59:42 -0000
Received: from mailserver.domain.domain (HELO mailserver.domain.domain) (33.33.33.33)
  by server-9.tower-125.messagelabs.com with DHE-RSA-AES256-SHA encrypted SMTP; 20 Mar 2014 10:59:42 -0000
Received: from localhost ([127.0.0.1] helo=www.domain.domain)
    by mailserver.domain.domain with esmtpa (Exim 4.73 (FreeBSD))
    (envelope-from <[email protected]>)
    id 1WQahL-0004m2-FT
    for [email protected]; Thu, 20 Mar 2014 11:59:43 +0100
MIME-Version: 1.0
Message-ID: <[email protected]>
From: "Domain domain" <[email protected]>
Subject: Something 2014
Content-Type: multipart/alternative;
    boundary="=_216b93cd925b7803b4c52bca98a3ee3c"
To: [email protected]
Date: Thu, 20 Mar 2014 11:59:43 +0100
X-Date: 2014-03-20 11:59:43
X-Connected-IP: 127.0.0.1:50595


--ONHrN715MUZyqWppCw12MuNjL3IsvuuVQXcdmQ==--

Fatal Error

Some emails are successfully processed and the array is populated as expected, but most are failing. With error reporting turned on I get the following output (original file path has been removed):

PHP Fatal error: Cannot use string offset as an array in ....../bounce_driver.class.php on line 595

I'm not knowledgeable enough to debug/fix this. Any ideas?

Thanks,
Colin

line 458

PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

Think it should be something like:

preg_replace_callback("/=([0-9A-F][0-9A-F])/", function($matches){ chr(hexdec($matches[0])); } , $line);

but not tested yet.

I want specific non-fatal messages to precede others and interpret long sentences.

The newest version I found still can't cope with lines that have been split up by different rules of the max. linesize, like
The mail system

<xxxxxxxxxxxxxxxxxxxxxx>: maildir delivery failed: "Sorry, the user's
maildir
has overdrawn his diskspace quota"

so I'll come up with a new version. I'm trying to get this library to read IMAP-mails as well.
Probably the well maintained IMAP-implementation will lower the needed complexity for header and body interpretation.

At the moment I have an IMAP-user with some really bounced e-mails, for which I want all possible retries and mailbox full messages to appear before a 550 as a last resort appears. I even think about being more clear about not knowing, just answer 9.9.9 or something, as 550 already could be anything.

The resulting status can then be updated to the mail-relation records, to be able to decide to keep them. As I'm not confident I'll ever succeed at matching 100% I could imagine it would be convenient just to be able to click on some link with the mail-relation-record just to open the original e-mails without having to search for them.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.