Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subject encoding #295

Closed
Naghal opened this issue Mar 23, 2020 · 9 comments
Closed

Subject encoding #295

Naghal opened this issue Mar 23, 2020 · 9 comments

Comments

@Naghal
Copy link

Naghal commented Mar 23, 2020

Describe the bug
When saving subject to database, sometimes the subject output is different for messages with the same subject. (Ex. Two reply to the same message)
Reply A subject: 'Re: Accès SQL Anthony'
Reply B subject: 'Re: Acce`s SQL Anthony'
To Reproduce
Steps to reproduce the behavior:

  1. Call getMessage() on message A
  2. Call getMessage() on message B
  3. The message A subject is Re: Accès SQL Anthony while message b subject is Re: =?utf-8?Q?Acc=C3=A8s_?=SQL Anthony

Expected behavior
Message should be the same for the two messages since the email source has the same subject and encoding information.

Additionnal information
To convert Re: =?utf-8?Q?Acc=C3=A8s_?=SQL Anthony to proper UTF8, I use the following function which when saved to database is correctly set to 'Re: Accès SQL Anthony'

private function convertSubjectEncoding($subject)
    {
        $encoding = mb_detect_encoding($subject);
        if($encoding != 'UTF-8') {
            return iconv_mime_decode($subject, 0, "UTF-8");
        }
        return $subject;
    }

The message which is saved as Re: Acces SQL Anthony in the database is already UTF-8 when I call getSubject()I have tried UTF-8, ASCII and nothing asdecoder.message.subject` setting.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop / Server (please complete the following information):

  • OS: [Linux]
  • PHP: [7.4]

Additional context
Here is the email source of email a and B (Subject part only)
A: Subject: Re: =?utf-8?Q?Acc=C3=A8s_?=SQL Anthony
B: Subject: =?UTF-8?Q?Re=3A_Acc=C3=A8s_SQL_Anthony?=

@OysteinHoiby
Copy link

Hi,

I had the same issue, and found a workaround. I'm not sure if this is the best approach to this issue, anyway this worked for me:

In Message.php on line 401 change
$this->subject = imap_utf8($header->subject);
to
$this->subject = iconv_mime_decode($header->subject);

@Naghal
Copy link
Author

Naghal commented Apr 21, 2020

Hi, I ended up doing something similar. However, I am also forced to replace accented caracters with normal ones.
I.E: é become e
normalizeChars(iconv_mime_decode($subject, 2, "UTF-8"));

The workaround you mentionned can not be used in my case as I need to compare the subjects of multiple messages to group them by conversation. (Looking at references also)
If I did only use iconv_mime_decode, message A would have an accent and not the B, so the comparison would fail.

apiaget added a commit to apiaget/laravel-imap that referenced this issue Jun 14, 2020
Addresses issue Webklex#295 regarding weird subjects.
Example: =?utf-8?Q?Suivez=20l=27actualit=C3=A9=20de=20l=27Ecole=20Nemesis=20=22intra=20muros=22?=`
@Vishrut-1
Copy link

mb_decode_mimeheader($oMessage->getSubject());
Try this it will work.

@Webklex
Copy link
Owner

Webklex commented Aug 20, 2020

Please update to the latest version and take a look at the available config option imap.options.decoder.message.subject.
If you want to use mb_decode_mimeheader() set it to mimeheader or to iconv if you want to use iconv_mime_decode().

Best regards,

@Webklex Webklex closed this as completed Aug 20, 2020
@sachinaquilmedia
Copy link

sachinaquilmedia commented Dec 24, 2022

When I compare the encode subject with some other language then its show null data

This is the query
$messages = $query->from($value->email_address)->subject($value->subject)->setFetchOrderDesc()->get();

@freescout-helpdesk
Copy link
Contributor

Webklex/php-imap#410

@Hardik0078
Copy link

Subject issue- fetching email from the mail server with this package subject coming in this format =?UTF-8?B?4KSo4KS/4KS14KS/4KSm4KS+IOCkuOCkguCkluCljeCkr+CkviBHRU0vMjAyMy9CLzM0OA==?= =?UTF-8?B?MTcwOCDgpK7gpYfgpIIg4KSr4KS+4KSH4KSo4KWH4KSC4KS44KS/4KSv4KSyIOCksOClh+CknyDgpJU=?=

how can i fix this?
i used iconv_mime_decode function but it not works

@freescout-helpdesk
Copy link
Contributor

Working solution is described here: Webklex/php-imap#410

Here is the result:
2023-06-08_13-55-01

@Hardik0078
Copy link

it works, Thank you so much @freescout-helpdesk .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants