jdavidbakr / mail-tracker

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

Replace strlen and substr functions with mb_* variants #152

Closed DuckThom closed 2 years ago

DuckThom commented 2 years ago

Error

SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xC3...' for column 'content'

Summary

Currently, the content length is calculated with strlen and then trimmed accordingly using substr. For single byte characters, this is fine. However, SQL errors start to occur when sending emails in, for example, French. This is because characters like  are not treated properly by substr and strlen.

Issue:

Using strlen and substr:

❯ php -r 'echo strlen("Â") . "\n";'
2
❯ php -r 'echo substr("Â", 0, 1) . "\n";'
�

Using mb_substr and mb_strlen:

❯ php -r 'echo mb_strlen("Â") . "\n";'
1
❯ php -r 'echo mb_substr("Â", 0, 1) . "\n";'
Â

Suggested change:

Old:

https://github.com/jdavidbakr/mail-tracker/blob/45b0923537912b073aeb520d33de04c52c2d2191/src/MailTracker.php#L171

New:

 'content' => config('mail-tracker.log-content', true) ? (mb_strlen($original_content) > config('mail-tracker.content-max-size', 65535) ? mb_substr($original_content, 0, config('mail-tracker.content-max-size', 65535) - 3) . '...' : $original_content) : null, 

Note: I also added - 3 to the substr call, this is because the max value of the text column is 65535 but '...' is being appended to the trimmed string, therefor the string would still be too long.

jdavidbakr commented 2 years ago

This probably should actually be changed to use Str:: functions instead. Feel free to send a pull request, or I might get a chance to update it next week.

jdavidbakr commented 2 years ago

Switched to Str::function in https://github.com/jdavidbakr/mail-tracker/releases/tag/5.0.7