how to check if the mail I have sent from laravel has been delivered to recipient or not?

114 Views Asked by At

I have searched the entire internet but found nothing for my problem. I am using Laravel 10 for my project to send bulk mails. Laravel is sending the mails to the provided mail address. But the problem: Is there any way to check the status of a mail delivered successfully to the recipient or not?

I found solutions in many forums telling that to use Mail::failures(). But this method is not available in Laravel anymore. So what is the proper way to get the status of the mail delivered to the recipient mail or not? Please help.

My code in controller:

try {
    foreach ($emails as $key => $email) {
        $result = Mail::to($email)->send(new MarketingMail($email_content));
    }
                
} catch (Exception $e) {
    return response()->json([
        'message' => 'Email not valid',
        'status' => 500
    ], 500);
}
1

There are 1 best solutions below

7
alkhatibdev On

A popular marketing method called the pixel approach, to implement it you can create a route in your application that corresponds to an image URL. This route will handle requests made to this URL, and you can use it to mark an email as delivered in your system. The concept is to embed a small, transparent image in your email templates, and when the recipient opens the email and loads the image, it triggers the route and updates the email status.

For example, in your web.php routes file:

Route::get('/images/email-delivered-event/{email_id_or_any_identifier}', function ($email_id_or_any_identifier) {
    // Find the corresponding model instance based on the provided identifier
    $model = YourModel::findOrFail($email_id_or_any_identifier);

    // Update the model to mark the email as delivered
    $model->is_delivered = true;
    $model->save();
    // Or Consider this as an event listener to your email delivered event you can use any action

    // You can return an empty response as the image, or a 1x1 transparent pixel
    return response('', 200)->header('Content-Type', 'image/png');

});

Your generated image URL should look like

https://yourdomain.com/images/email-delivered-event/4356349859348759

Note

Remember that some email clients may block image loading by default for security and privacy reasons. In such cases, this pixel approach might not work.

Disclaimer

Always consider the privacy implications and comply with regulations when using such tracking mechanisms.