Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am using Amazon SES. How will I track the bounce email messages using PHP?

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.

like image 217
Ranajit B Avatar asked Oct 18 '13 12:10

Ranajit B


1 Answers

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']);
 }
}
like image 64
Merwin Poulose Avatar answered Nov 06 '22 01:11

Merwin Poulose