beyondcode / beyondcode/laravel-mailbox

"Incorrect string value" when saving non-UTF-8 emails to MySQL database

Open
#62 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
PHP
Stars
1.1k
Forks
127
PR merge metrics
No merged PRs in 30d

Description

Hey there

This issue is mainly as a reference to people coming across the same hair-pulling issue as I did.

When receiving an email in ISO-8859-1 encoding with special characters (such as the danish "æøå") I found that a QueryException will be thrown such as

```
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xE6ftet ...' for column 'message' at row 1 (SQL: insert into `mailbox_inbound_emails` (`message`, `message_id`, `subject`, `text`, `from`, `updated_at`, `cr...
```

This is due to attempting to insert ISO-8859-1 characters into a utf8mb4 MySQL column.

It would be cool if the package had a way to handle this internally - however due to this (hopefully) being a generally rare issue with most clients using UTF-8 I can see why this might not be a priority.

For those interested I solved it by introducing a new method on a custom `App\Email` model:

```php
message, ['UTF-8', 'ISO-8859-1']); // Add more encodings to support here

if ($encoding !== 'UTF-8') {
$this->message = mb_convert_encoding($this->message, 'UTF-8', $encoding);
$this->mimeMessage = null;
}

return $this;
}
}
```

And in my `AppServiceProvider@boot`

```php
Mailbox::catchAll(function (Email $email) {
$email->ensureUtf8()->save();
});
```

Hope this can help somebody else out there :-)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.