jdavidbakr / mail-tracker

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

Attachments break injection #182

Closed JapSeyz closed 2 years ago

JapSeyz commented 2 years ago

A markdown email w/o attachments has the following $part->getMediaSubtypes:

[2022-05-24 02:50:58] local.DEBUG: plain  
[2022-05-24 02:50:58] local.DEBUG: html  

Whereas simply adding two attachments like so:

$mail->attach($this->order->invoicePath(), [
    'as' => 'invoice.pdf',
    'mime' => 'application/pdf',
]);

Changes the Media Subtypes to:

[2022-05-24 02:42:37] local.DEBUG: alternative  
[2022-05-24 02:42:37] local.DEBUG: pdf  

Inside the alternative is the html and plain contents, but they're never checked.

Here it is with $part->asDebugString() logged too:

[2022-05-24 02:56:53] local.DEBUG: alternative  
[2022-05-24 02:56:53] local.DEBUG: multipart/alternative
  └ text/plain charset: utf-8
  └ text/html charset: utf-8  
[2022-05-24 02:56:53] local.DEBUG: pdf  
[2022-05-24 02:56:53] local.DEBUG: application/pdf disposition: attachment filename: invoice.pdf  
JapSeyz commented 2 years ago

Adding this part to createTrackers, inside the foreach ($messageBody->getParts as $part) { in MailTracker.php fixes the issue:

if (method_exists($part, 'getParts')) {
    foreach ($part->getParts() as $p) {
        if($p->getMediaSubtype() == 'html') {
            $original_html = $p->getBody();
            $newParts[] = new TextPart(
                $this->addTrackers($original_html, $hash),
                $message->getHtmlCharset(),
                $p->getMediaSubtype(),
                null
            );

            break;
        }
    }
}

I've added it above above the usual code in the loop. Right above this line:

https://github.com/jdavidbakr/mail-tracker/blob/master/src/MailTracker.php#L200

jdavidbakr commented 2 years ago

@JapSeyz if you want to go ahead and make a PR (be sure to include a test) I'll accept it, otherwise I'll try to look at it before too long.

JapSeyz commented 2 years ago

I’ll try and get a PR going, just wanted to note everything down before signing off yesterday :) On 24 May 2022, 15.06 +0200, J David Baker @.***>, wrote:

@JapSeyz if you want to go ahead and make a PR (be sure to include a test) I'll accept it, otherwise I'll try to look at it before too long. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>