yiioverflow / yii2-imap

https://yiioverflow.com
BSD 3-Clause "New" or "Revised" License
41 stars 33 forks source link

Attachment id issue #31

Open klysiak opened 4 years ago

klysiak commented 4 years ago

Hi,

I received an email with attachments and those were not recognized by your library. The issue was that originally imap_fetchstructure got the following:

[type] => 3 [encoding] => 3 [ifsubtype] => 1 [subtype] => PDF [ifdescription] => 0 [ifid] => 1 [id] => <> [bytes] => 277320 [ifdisposition] => 1 [disposition] => attachment [ifdparameters] => 1 Your code: $attachmentId = $partStructure->ifid ? trim($partStructure->id, " <>") : (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null);

was generating empty $attachmentId so if($attachmentId) returned false and the attachments were not processed.

Temporarily I added $attachmentId = $partStructure->ifid ? ((trim($partStructure->id, " <>") != '')?trim($partStructure->id, " <>"):mt_rand() . mt_rand()) : (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null);

and it is working. I will check if it is causing any further issues.

Maybe you have better suggestion?

yiioverflow commented 4 years ago

Sure dear. Can you send me one pull request pls ?

s1lver commented 4 years ago

This is a very bad construction

yiioverflow commented 4 years ago

Yes. So I didnt merged it yet.

s1lver commented 4 years ago

I would suggest doing so, but check:

    /**
     * @param object $structure
     * @param array $params
     * @return string|null
     */
    public static function getAttachmentId($structure, $params): ?string
    {
        if ($structure->ifid && !empty(trim($structure->id, " <>"))) {
            $attachmentId = trim($structure->id, " <>");
        } elseif (isset($params['filename']) || isset($params['name'])) {
            $attachmentId = mt_rand() . mt_rand();
        } else {
            $attachmentId = null;
        }

        return $attachmentId;
    }
yiioverflow commented 4 years ago

@klysiak can you verify the above solution please ?

klysiak commented 4 years ago

I have implemented suggested solution. First impression is good. It is working fine. Please give me few days to verify if all is correct.

yiioverflow commented 4 years ago

@klysiak okay, Thanks

yiioverflow commented 4 years ago

@klysiak did you get time check this ?

yiioverflow commented 4 years ago

I have implemented suggested solution. First impression is good. It is working fine. Please give me few days to verify if all is correct.

If you dont mind, Can you give me an update please ?

garryseldon commented 1 year ago

Hello. If the name of the attached file is too long, then the OS does not allow it to be saved. Can the file name be generated randomly, for example through the uniqid function? It would be nice not to save the files, but to get its contents through the method.

s1lver commented 1 year ago

Hello. If the name of the attached file is too long, then the OS does not allow it to be saved. Can the file name be generated randomly, for example through the uniqid function? It would be nice not to save the files, but to get its contents through the method.

Yes it would be nice. We check the length of the file, if it is too large, then we replace it with a unique value