10

I am using Amazon SES service. But I couldn't understand how will I track the bounce email messages using PHP and keep store those email logs in database. I have a reference link of Amazon blog, but the solution given there is on C#(http://sesblog.amazon.com/post/TxJE1JNZ6T9JXK/Handling-Bounces-and-Complaints). Need help and assistance. Thank you.

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
Ranajit B
  • 131
  • 1
  • 1
  • 6
  • Have you looked at the docs? http://docs.aws.amazon.com/ses/latest/DeveloperGuide/bounce-complaint-notifications.html you can configure SES to send these messages to a specified SNS topic, and then you just get the messages from there, it says how you can set his up and i am pretty sure there is a php SNS example somewhere too. – joschua011 Oct 18 '13 at 13:13
  • I didn't find there(http://docs.aws.amazon.com/ses/latest/DeveloperGuide/bounce-complaint-notifications.html) any for php, but the following given link is helpful... – Ranajit B Oct 23 '13 at 07:54

2 Answers2

1

Create a SNS topic for bounces and complaints and link it to your SES (go to view details tab - Edit config - link the respective SNS complains & bounces topics).

Make sure to subscribe the SNS topic you created either to your mail ID or http/s based on your requirement. Whenever there is bounce or complaint is tagged for the SES message you will receive a JSON data which can later to processed as per your need.

A very helpful AWS webinar follows here: https://www.youtube.com/watch?v=n3Fr0bCsIvo

1

STEPS TO FOLLOW

  1. Create SNS Topic

  2. Create subscription

  3. Confirm subscription

Code

class AmazonController extends Controller
{
 public function handleBounceOrComplaint(Request $request)
 {
   Log::info($request->json()->all());
   $data = $request->json()->all();
   if($request->json('Type') == 'SubscriptionConfirmation')
   Log::info("SubscriptionConfirmation came at: ".$data['Timestamp']);
   if($request->json('Type') == 'Notification'){
   $message = $request->json('Message');
   switch($message['notificationType']){
    case 'Bounce':
      $bounce = $message['bounce'];
      foreach ($bounce['bouncedRecipients'] as $bouncedRecipient){
        $emailAddress = $bouncedRecipient['emailAddress'];
        $emailRecord = WrongEmail::firstOrCreate(['email' => $emailAddress, 'problem_type' => 'Bounce']);
        if($emailRecord){
          $emailRecord->increment('repeated_attempts',1);
        }
      }
      break;
      case 'Complaint':
      $complaint = $message['complaint'];
      foreach($complaint['complainedRecipients'] as $complainedRecipient){
        $emailAddress = $complainedRecipient['emailAddress'];
        $emailRecord = WrongEmail::firstOrCreate(['email' => $emailAddress, 'problem_type' => 'Complaint']);
        if($emailRecord){
          $emailRecord->increment('repeated_attempts',1);
        }
      }
      break;
      default:
      // Do Nothing
      break;
    }
  }
  return Response::json(['status' => 200, "message" => 'success']);
 }
}
Rob
  • 26,989
  • 16
  • 82
  • 98