Sent Mail checking if it is delivered and open to recipient in Laravel

3k Views Asked by At

I want help in scaffolding code with Laravel default Mail package to send an email to the recipient with an enhancement that checks the status either mail is delivered to recipient and then check that either recipient opened the mail or not and then change the status of that email in my db_email_list. I googled it that to add headers just like follow the example but could not get it how to get the status

$message->getHeaders()->addTextHeader('X-Confirm-Reading-To','recipient_mail');

$sendEmail->getHeaders()->addTextHeader('Disposition-Notification-To','recipient_mail');

$sendEmail->getHeaders()->addTextHeader('Return-Receipt-To','recipient_mail');
1

There are 1 best solutions below

2
On

When the user has gotten the email: Simply use this piece of code:

if (count(Mail::failures())) {
    return false;
} else {
    return true;
}

true=delivered, false=not delivered

When user reads the email: Hm sounds like you need to include a trick in your email in order to know if user has opened/read the email by simply adding for instance including an image on your email with a route defined in your end and passing user id as query param.

<img src="http://www.example.com/user-read-email?user_id=20" />

So whenever the user opens the email img src will fire a call to your url and simmply get the user id from the url, and set the flag for that user in db.