Bounce Email Handling with PHP

How to catch bounced emails in PHP?

First you need to send email with mail() function with the use of additional headers. You point the return email address to a mailbox where you will gather the bounced emails. The below code is an example how to achieve that.

$recipient = "jack@someotherexample.com";
$subject = "test subject";
$message = "test message";

$body = "<html>\r\n";
$body .= "<body style=\"font-family:Verdana, Verdana, Geneva, sans-serif; font-size:12px; color:#666666;\">\r\n";
$body .= $message;
$body .= "</body>\r\n";
$body .= "</html>\r\n";

$headers = "From: My site<noreply@example.com>\r\n";
$headers .= "Reply-To: bounce@example.com\r\n";
$headers .= "Return-Path: bounce@example.com\r\n";
$headers .= "X-Mailer: PHP\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$result = mail($recipient, $subject, $body, $headers);

if($result)
{
echo "email sent";
}
else
{
echo "email send error";
}

SOURCE

Than you just check the inbox for bounced error emails. The below example code will connect to an imap inbox, fetch and print out all the emails it finds there. You can easily customise it to your needs.

/* connect to server */
$hostname = '{example.com:143}INBOX';
$username = 'bounce@example.com';
$password = 'password';

/* 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) {

/* begin output var */
$output = '';

/* put the newest emails on top */
rsort($emails);

/* for every email... */
foreach($emails as $email_number) {

/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,1.2);

/* output the email header information */
$output.= '<div '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span>'.$overview[0]->subject.'</span> ';
$output.= '<span>'.$overview[0]->from.'</span>';
$output.= '<span>'.$overview[0]->date.'</span>';
$output.= '</div>';

/* output the email body */
$output.= '<div class="body">'.$message.'</div>';
}

echo $output;
}

/* close the connection */
imap_close($inbox);

SOURCE

handling bounce email w/phpmailer

PHPmailer does not handle receiving emails. It's purely a library for allowing PHP to talk to an SMTP server for sending emails. It has absolutely no support whatsoever to act as a mail client (e.g. receiving).

PHPmailer has no way of knowing the email bounced, as the bounce occurs LONG after PHPmailer's handed the email off to the outgoing SMTP server. IN real world terms, PHPmailer takes your letter and walks down the block to drop it into a mailbox. The bounce occurs later, when the letter carrier brings the letter back with 'return to sender' stamped on it - PHPmailer is not involved in this at all.

Your options are:

1) Use PHP's imap functions to connect to an existing pop/imap server and retrieve emails that way

2) Use a .forward or similar redirect on the SMTP side to "send" incoming email to a PHP script.

PHPMailer how to set bounce email address

The bounce email address is the SMTP MAIL FROM address, also known as the envelope sender. This is often the same as the From address (and PHPMailer uses the from address as the sender by default), but it is entirely normal for it to be different (subject to DMARC config). In PHPMailer you set it by setting the Sender property, as you are doing in this line:

$mail->Sender = 'noreply@custom.com';

When a server receives a message, it takes the envelope sender and adds it to the message in a Return-Path header; this is not something that a sending server should ever do.

So to change the bounce address, change that setting; it is mostly independent of from and reply-to addresses. Note however that gmail may ignore this if you have not configured it as an alias for your gmail username.

How to write a PHP script that read bounce email?

You'll need to configure whichever mail transport agent handles (MTA) "bounce_handle@domain.com" to send the mail to the PHP script that does whatever magic you need it to do. The MTA is what actually handles mail coming into the server. There are many different MTA's, but most of them have some configuration where you can basically tell it to pipe email coming into a certain address into a custom script.

Alternatively, you could setup a mailbox for your bounce handler and have PHP read it via POP3. For this, you'd have to configure an actual email account for your bounce handler. Then you have your PHP script connect to that mailbox using standard protocols. See the php.net documentation on IMAP/POP for how this is accomplished.

How to manage emails bouncing and errors in PHP?

One possible thing you could do aside from having a PHP script read mail from a pop/imap server would be to pipe incoming mail for a certain address to a php script. See Google

You would then read the entire contents of the message in by doing something like $email = file_get_contents('php://stdin'); I've installed the php extension mailparse to assist in parsing RFC emails, but there are other options available. You don't even necessarily have to use anything to parse the message.

Once you have the message, there are a number of indicators you can use to try to flag a message as a bounch. First, see the Wikipedia article about Non Delivery Reports, specifically Format and RFC 6522 - The Multipart/Report Media Type for the Reporting of Mail System Administrative Messages. You can also check for common headers in the message such as X-Failed-Recipients or Diagnostic-Code.

Once you've determined a message as a bounce in the PHP mail processor, you can take appropriate action and set a flag in the database related to that email. Mind you, some errors may not mean the address is no good. For example, if a mail server is down for a few days, your MTA may give up, but it doesn't mean the address is no good. Also a user's mailbox could be full.

It wouldn't be a bad idea to log a copy of the bounced message so it could be checked by a person if necessary to diagnose an issue or reverse the flagging or a particular email address.

Here are some additional references:

http://forums.theplanet.com/lofiversion/index.php/t89873.html (note Improvement possibility 2)

https://stackoverflow.com/questions/5700034/how-do-i-process-a-bounce-email-to-find-the-error-code

Bounce Email handling with PHP?

Hope that helps.

Handle mail bounce with PHP

I would guess that your webserver is running as apache or nobody which is not the owner of the mail folder (so it would therefore have 0 as the permission - which is nothing)

You either need to run the webserver as that user (I wouldnt do that) or change the permission on the folder to allow the webserver to read the folder



Related Topics



Leave a reply



Submit