Bounce Email handling with PHP?

后端 未结 14 1447
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 04:30

Here is my scenario:

I have 2 email accounts: admin@domain.com and bounce@domain.com.

I want to send email to all my users with admin@domain.com but then \"r

相关标签:
14条回答
  • 2020-11-28 05:21

    I had the same problem, exact situation. By default my mail server, is sending all my returned mails to the same account that it was originally sent from, with automatic msg "Mail delivery failed: returning message to sender".

    I dont really want to know why it was returned, had so many mails transactions that I just want to remove the bad ones. Dont have time to check specific rule such as Doestn Exist, Unavailable, etc ,,, Just want to flag for deletion and go on.

    Bounce mails are so trivial as you need to deal with a lot of different servers and responses types. Each anti spam software or operating system scenario can send a different error code with the bounced email.

    I recomend you to read and download this fixed debugged version of Handling Bounced Email - USING PHPMAILER-BMH AND AUTHSMTP from KIDMOSES here http://www.kidmoses.com/blog-article.php?bid=40 if you want to setup IMAP and and send your own custom headers, send them to your bounce@domain.com and then cross your fingers to see if the script catches the headers you sent written in the bounced mail. I tried it, works.

    But if you want to follow my quick and easy fix that resolved my problem, here is the secret.

    1 - Download the better version from KIDMOSES site or from my site, just in case KIDMOSES want to move somewhere else http://chasqui.market/downloads/KIDMOSES-phpmailer-bmh.zip

    2 - The variable that contains the text of your returned mail is $body and itself contains the bad returned email (SO ITS AN MULTIDIMENSIONAL ARRAY ). (Also contains your servers mail and other DNS mails stuff, but we are looking for the BAD MAIL BOUNCED.

    3 - Since your OWN SERVICE is sending you back the bounced email, then its not likely to change its format and own headers, sending back bounced emails, so you are safe to pick the order of bounced email array returned. In my case was always the same format template. (Unless you change systems or providers)

    4 - We look into that $body and search for all email string variables and extract them positioning them into a two dimensional array called $matches

    5 - We select the array position, by printing the array using print_r( array_values( $matches ));

    6 - This is the code that you need to modify. Its around line 500 from class.phpmailer-bmh.php file

      // process bounces by rules
      $result = bmhDSNRules($dsn_msg,$dsn_report,$this->debug_dsn_rule);
    } elseif ($type == 'BODY') {
      $structure = imap_fetchstructure($this->_mailbox_link,$pos);
      switch ($structure->type) {
        case 0: // Content-type = text
          $body = imap_fetchbody($this->_mailbox_link,$pos,"1");
          $result = bmhBodyRules($body,$structure,$this->debug_body_rule);
    
          //MY RULE IT WORKS at least on my return mail system..
          $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';
          preg_match_all($pattern, $body, $matches);
            //print_r( array_values( $matches ));     //To select array number of bad returned mail desired, usually is 1st array $matches[0][0]          
            echo "<font color = red>".$matches[0][0]."</font><br>";
          break;
    

    So we forget about returned headers and concentrate on the bad emails. You can excel them, you can MySQL them, or process to whatever you want to do.

    IMPORTANT Comment the echos in callback_echo.php in the samples directory otherwise you get all the junk before printed.

    function callbackAction ($msgnum, $bounce_type, $email, $subject,      $xheader, $cheader, $remove, $rule_no=false, $rule_cat=false, $rule_msg='', $totalFetched=0) {
      $displayData = prepData($email, $bounce_type, $remove);
      $bounce_type = $displayData['bounce_type'];
      $emailName   = $displayData['emailName'];
      $emailAddy   = $displayData['emailAddy'];
      $remove      = $displayData['remove'];
      //echo "<br>".$msgnum . ': '  . $rule_no . ' | '  . $rule_cat . ' | '  . $bounce_type . ' | '  . $remove . ' | ' . $email . ' | '  . $subject . ' | ';
      //echo 'Custom Header: '  . $cheader . " | ";
      //echo 'Bounce Message: '  . $rule_msg . " | ";
      return true;
    }
    

    MY OUTPUT

    Connected to: mail.chasqui.market (bounce@chasqui.market)
    Total: 271 messages 
    Running in disable_delete mode, not deleting messages from mailbox
    
    kty2001us@starmedia.com
    
    ...
    
    entv@nuevoface.com
    
    Closing mailbox, and purging messages
    Read: 271 messages
    0 action taken
    271 no action taken
    0 messages deleted
    0 messages moved
    
    0 讨论(0)
  • 2020-11-28 05:24

    In the php mail command http://php.net/mail

    you use the fifth parameter and add "-f" to it.

    So, you use "-f mybouncebox@mydomain.com" as the parameter

    the phpList newsletter manager uses this to manage bounces.

    Once the bounces fill up in the mailbox, you can POP them, and process them. That's the easiest way to deal with them, as opposed to handling them when they arrive.

    0 讨论(0)
  • 2020-11-28 05:25

    i have had pretty bad luck looking for a PHP solution for this, but i ran across this product that does just what i needed.

    it runs as a native app mac/win but it does the job.

    http://www.maxprog.com/site/software/internet-marketing/email-bounce-handler_sheet_us.php

    0 讨论(0)
  • 2020-11-28 05:25

    You can use imap_open to access your mails from PHP.

    This functions also works for POP3 but not every function may work here. However I guess in 2018 most email-clients should support IMAP.

    This function can also be used to open streams to POP3 and NNTP servers, but some functions and features are only available on IMAP servers.

    Here is a little example, how to iterate through your emails:

      /* connect to server */
      $hostname = "{$your-server:$your-port}INBOX";
      $username = 'my-username';
      $password = '123';
    
      /* try to connect */
      $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to mailbox: ' . imap_last_error());
    
      /* grab emails */
      $emails = imap_search($inbox,'ALL');
    
      /* if emails are returned, cycle through each... */
      if($emails) {
        /* for every email... */
        foreach($emails as $email_number) {
    
            $message = imap_body($inbox,$email_number,2);
            $head    = imap_headerinfo($inbox, $email_number,2);
            // Here you can handle your emails
            // ...
            //  ...
          }
      }
    

    In my case, I know that I always get my mail delivery failed from Mailer-Daemon@myserver.com. So I could identify bounces like that:

    if($head->from[0]->mailbox == 'Mailer-Daemon')
    {
      // We have a bounce mail here!
    }
    

    You said:

    When, the email can't be sent, it's sent to bounce@domain.com, the error message could be 553 (non existent email ...) etc.

    So if your bounce emails have the subject "Mail delivery failed: Error 553" then you could identify them by the subject like this:

    if($head->subject == 'Mail delivery failed: Error 553')
    {
      // We have a bounce mail here!
    }
    

    The failed email address is not in the header, so you need to parse it from the $message variable with some smart code.

    0 讨论(0)
  • 2020-11-28 05:30

    You could always use something like http://cloudmailin.com to forward the bounced emails on to your php server via http however you may be better with a service dedicated to sending emails and using their api to retrieve the bounce details.

    0 讨论(0)
  • 2020-11-28 05:32

    Let the emails bounce to an address that is really an emailadress (with login details etc.).

    Make a php script which runs ever x minutes (for example with a cron job). This php script must do the following. - Retrieve all email from the box (use for example Zend Mail) - Check for the error in the message (e.g. by searching it with regular expressions) - Do what ever is necessary.

    If you want to know specifically who has bounced back you can use user specific bounce addresses. (See for example this site)

    0 讨论(0)
提交回复
热议问题