Skip to content

Instantly share code, notes, and snippets.

@anildhiman03
Forked from fukuball/EmailController.php
Created August 16, 2022 10:21
Show Gist options
  • Save anildhiman03/bc1597186321853b4f998da0a39b95dc to your computer and use it in GitHub Desktop.
Save anildhiman03/bc1597186321853b4f998da0a39b95dc to your computer and use it in GitHub Desktop.
Laravel 5.1 Controller that receives AWS SES Bounce and Complaint notifications sent via AWS SNS. It records the email addresses that bounced or complained in a database.
<?php
public function postBounceOrComplaint(Request $request, Mailer $mailer){
Log::info($request->json()->all());
if($request->json('Type') == 'SubscriptionConfirmation'){
$client = new Client();
$client->get($request->json('SubscribeURL'));
return response()->json();
}
//
if($request->json('Type') == 'Notification'){
$message = json_decode($request->json('Message'));
switch($message->notificationType){
case 'Bounce':
$bounce = $message->bounce;
foreach ($bounce->bouncedRecipients as $bouncedRecipient){
$emailAddress = $bouncedRecipient->emailAddress;
$emailRecord = ProblemEmail::firstOrCreate(['email_address' => $emailAddress]);
$emailRecord->incrementBounceCount(1);
};
break;
case 'Complaint':
$complaint = $message->complaint;
foreach($complaint->complainedRecipients as $complainedRecipient){
$emailAddress = $complainedRecipient->emailAddress;
$emailRecord = ProblemEmail::firstOrCreate(['email_address' => $emailAddress]);
$emailRecord->incrementComplainedCount(1);
}
break;
default:
// Do Nothing
break;
}
}
return response()->json(['message' => 'success']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment