jdavidbakr / mail-tracker

Package for Laravel to inject tracking code into outgoing emails.
MIT License
567 stars 129 forks source link

Multiple logs for one email upon failing #184

Closed pavlinagargova closed 1 year ago

pavlinagargova commented 2 years ago

Hi,

I've recently updated my project to Laravel 9 and my mail-tracker version is 6.0.5 at the time of writing. During testing I've found out this strange behavior:

I'm using mailspons for testing and I've reached out my free quota. Thats when for each email I sent from my app I can see 3 logs for that particular email in the sent_emails table. I have scheduled my workers -> php artisan queue:work --queue=emails --tries=3. I believe that for each try, if unsuccessful, a new log is being created, which hasn't been the behaviour in previous versions. I believe this is an issue due to the fact that no actual email has been sent and it shouldn't have 3 logs for it (I do know that the logs are 3 due to my queue setting).

I see that trackers are being created on upon listening to the MessageSending::class which I believe is causing this issue.

Looking forward to your response.

jdavidbakr commented 2 years ago

Hm, that's an interesting one. There is only a MessageSending event and MessageSent event, there is no failure event I can hook into. The record needs to be created in MessageSending but there is no way to identify that it failed so that it can remove the created trackers. I'm open to ideas as I've not personally got a setup that this happens in.

pavlinagargova commented 2 years ago

I believe that it would be better if the tracker was created upon MessageSent event, if in fact it does fire up after the message has been indeed sent. I see that in previous versions the trackers were created on calling the beforeSendPerformed function.

jdavidbakr commented 2 years ago

The problem is that the original content is modified when the trackers are created, so there's not access to the email content without the trackers in the MessageSent event. If we store the content that MessageSent has access to, then when the admin views the email a tracker will get recorded.