Webklex / php-imap

PHP-IMAP is a wrapper for common IMAP communication without the need to have the php-imap module installed / enabled. The protocol is completely integrated and therefore supports IMAP IDLE operation and the "new" oAuth authentication process as well.
https://www.php-imap.com
MIT License
305 stars 143 forks source link

Problems with the client name when it has accents #236

Closed alexv96 closed 1 year ago

alexv96 commented 2 years ago

It has happened to me lately that when I receive emails from people who have accents in their name, these are not displayed properly and shows a long text, I attach a test image.

image

image

class MailboxResourceWebklexResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $today =Carbon::now();
        $date = null;
        $dateMail = Carbon::parse($this->date[0])->format('Y-m-d');

        if ($today->format('Y-m-d') == $dateMail) {
            $date = mb_strtolower(Carbon::parse($this->date[0])->format('H:i A'));
        }

        return [
            'uid'   => $this->uid,
            'from'  => $this->from[0]->mail,
            'client'=> $this->from[0]->personal, //This is the problem
            'to'    => $this->to[0]->full,
            'date'  => (is_null($date) ? ucwords($this->date[0]->formatLocalized('%d %b')) : $date),
            'full_date' => Carbon::parse($this->date[0])->format('d-m-Y').', '.Carbon::parse($this->date[0])->format('H:i'),
            'subject'=> $this->subject[0],
            'flags' => (isset($this->flags['seen']) ? $this->flags['seen'] : null)
        ];
    }
}
mdemori commented 2 years ago

hi, this kind of string is a quoted-printable-encoding (the ?Q? mean this) you can decode it with below code:

$subject = '=?iso-8859-1?Q?Jos=E9_Antonio_Valiencia?=';
var_dump(mb_decode_mimeheader (str_replace('_', ' ', $subject)));

The replace is required because in Q encoding mode, RFC 2047 encodes spaces as _ (Section 4.2).

Just as info, another encoding exist, is the "B" mode, is a base64 Encoding mode, also this case is handled by mb_decode_mimeheader below an example with same string of previous example encoded as base64:

 $subject = '=?iso-8859-1?B?Sm9zw6kgQW50b25pbyBWYWxpZW5jaWE=?=';
 var_dump(mb_decode_mimeheader($subject));
Webklex commented 2 years ago

Hi @mdemori , that's interesting, do you know if those have to be handled separately or are they "included" within these functions?

Best regards,

dominik commented 2 years ago

Hi @Webklex

I would suggest to use the function iconv_mime_decode($subject), as this method will not only take care of the quoted printable encoding but also of the character set and encoding ...

See https://www.php.net/iconv_mime_decode for further information.

Kind regards Dominik

Webklex commented 1 year ago

Please update to v5.1 and give it another try. If you are currently using an older version below v5.0, please read the breaking changes leading up to v5.1 before upgrading.

Best regards,