jdavidbakr / mail-tracker

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

Emails with Embedded Images Broken in Apple Mail/iCloud Mail/iOS Mail #194

Closed Jason-Morcos closed 1 year ago

Jason-Morcos commented 2 years ago

Hi,

My Env: Laravel 9.27.0, PHP 8.1.9, Mail-Tracker: 6.0.8

It appears that when emails are sent that have embedded images along with textual content in the laravel-correct style (<img src="{{ $message->embed(public_path('/images/anImage.png')) }}" alt="The Logo" style="max-height:40px;width:auto;height:auto;"/>), mail-tracker mutates these emails in a way that breaks rendering in Apple mail clients. In Apple mail clients, the embedded image becomes the only visible content of the email.

I confirmed that disabling mail-tracker is the differentiator in this case ($mailObject->getHeaders()->addTextHeader('X-No-Track', Str::random(10));). With this header set, emails are delivered totally normally.

It appears that the issue lies in MailTracker.php line 244 ($message->setBody(new AlternativePart(...$newParts));). Despite the incoming mail being either AlternativePart, MixedPart, or RelatedPart, the outgoing type set on 244 is AlternativePart. This breaks MixedPart and RelatedPart emails. Changing line 244 to match the incoming part type ($message->setBody(new (get_class($original_content))(...$newParts));) appears to fix the issue for me locally

Related: https://github.com/jdavidbakr/mail-tracker/issues/172, https://github.com/jdavidbakr/mail-tracker/issues/177