php-imap icon indicating copy to clipboard operation
php-imap copied to clipboard

Problems with the client name when it has accents

Open alexv96 opened this issue 3 years ago • 3 comments

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)
        ];
    }
}

alexv96 avatar Jun 13 '22 06:06 alexv96

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));

mdemori avatar Jun 17 '22 07:06 mdemori

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

  • mailparse_rfc822_parse_addresses
  • imap_mime_header_decode
  • iconv
  • mb_convert_encoding

Best regards,

Webklex avatar Aug 08 '22 10:08 Webklex

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

dominik avatar Sep 20 '22 21:09 dominik

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,

Webklex avatar Mar 16 '23 00:03 Webklex