How to catch bounced emails in PHP?

10,316

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 = "[email protected]";
$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<[email protected]>\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "Return-Path: [email protected]\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 = '[email protected]';
$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

Share:
10,316
Naveed Metlo
Author by

Naveed Metlo

Web Specialist with 10 years experience in PHP, MySql, Ajax, CSS, HTML5, XHTML, XML, JSON, SOAP, CURL, Google Apps, Chrome Extensions, Web Services, WSDL, Joomla, Wordpress, Opencart, Javascript, JQuery. API Specialist, worked on most popular &amp; demanded APIs like Amazon, eBay, Chargify, Twilio, Send Grid, Google Shopping, Google Maps, Shop Style, Bing, Paypal, Popshops, Kigo, hasOffers, shipStation, Commission Junction, UPS, DHL, Fedex

Updated on June 25, 2022

Comments

  • Naveed Metlo
    Naveed Metlo about 2 years

    I want to save emails if it is bounced while sending email through PHP function mail() ? What is best way to catch that ?