jdavidbakr / mail-tracker

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

Pixel being injected into incorrect position #130

Closed phleech closed 3 years ago

phleech commented 3 years ago

https://github.com/jdavidbakr/mail-tracker/blob/2a631b8b3e6fa1411966aa2a2c86b4be8e544ead/src/MailTracker.php#L73

This is purely theoretical... however; I assume you are attempting to inject the pixel directly after the opening <body> tag (assuming one is present). This is not what your achieving, and the pixel is actually being injected to the very end of the entire message.

The reason for this is due to your ordering of the new $html. You are using preg_match() to find and split the string on the tag, but then proceed to append the pixel to the very end of the string.

assuming the message is: <html><head><title>Your Email</title></head><body><p>Hello World.</p></body></html>

$matches[1] will be <html><head><title>Your Email</title></head><body> $matches[2] will be <p>Hello World.</p></body></html>

I propose that this line of code should read:

$html = $matches[1].$tracking_pixel.$matches[2];

Happy to raise this as a pull request if you wish.

Cheers.

jdavidbakr commented 3 years ago

I did intentionally inject the pixel at the end to help minimize the impact it might have on the body layout. Do you have thoughts that would indicate that it would be better to place the pixel at the top of the body?