SSilence / php-imap-client

a easy solution for simple IMAP email access in php
MIT License
268 stars 136 forks source link

Null email attachment name #191

Closed filipefreitas82 closed 6 years ago

filipefreitas82 commented 6 years ago

Bug

foreach($attachments as $attachment){
    echo $attachment->name;
}

Should return the attachment name instead of NULL.

Debug Attached an email with the following file:

201709 Caderneta Predial Finanças.pdf

This is the info part of the attachment object:

[info] => SSilence\ImapClient\Section Object
        (
            [_structure:SSilence\ImapClient\Section:private] => stdClass Object
                (
                    [type] => 3
                    [encoding] => 3
                    [ifsubtype] => 1
                    [subtype] => PDF
                    [ifdescription] => 0
                    [ifid] => 0
                    [bytes] => 186596
                    [ifdisposition] => 1
                    [disposition] => inline
                    [ifdparameters] => 1
                    [dparameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => filename*
                                    [value] => utf-8''201709%20Caderneta%20Predial%20Financ%CC%A7as.pdf
                                )

                        )

                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => filename*
                                    [value] => utf-8''201709%20Caderneta%20Predial%20Financ%CC%A7as.pdf
                                )

                            [1] => stdClass Object
                                (
                                    [attribute] => x-unix-mode
                                    [value] => 0644
                                )

                            [2] => stdClass Object
                                (
                                    [attribute] => name
                                    [value] => =?utf-8?Q?201709_Caderneta_Predial_Financ=CC=A7as=2Epdf?=
                                )

                        )

                )
mattparksjr commented 6 years ago

Are you sure it is being decoded correctly?

filipefreitas82 commented 6 years ago

How can I be sure?

mattparksjr commented 6 years ago

Make sure the var dump name is equal to that of when it was sent

Garzia commented 6 years ago

Modify the IncomingMessageAttachment.php file. I converted the name of the attribute associated with the name of the attachment in lowercase since at times it can be written all in uppercase.

/**
 * Returns the name of the attachment along with file extension.
 *
 * @return string
 */
protected function getName()
{
    // Check for different types of inline attachments.
    if ($this->_incomingObject->structure->ifdparameters) {
        foreach ($this->_incomingObject->structure->dparameters as $param) {
            if (strtolower($param->attribute) === 'filename') { //FG I converted the name of the attribute associated with the name of the attachment in lowercase since at times it can be written all in uppercase
                $this->name = $param->value;
                break;
            }
        }
    } elseif ($this->_incomingObject->structure->ifparameters) {
        foreach ($this->_incomingObject->structure->parameters as $param) {
            if (strtolower($param->attribute) === 'name') { //FG I converted the name of the attribute associated with the name of the attachment in lowercase since at times it can be written all in uppercase
                $this->name = $param->value;
                break;
            }
        }
    }
}
mattparksjr commented 6 years ago

Hey! What were you trying to do? If you want to submit improvements please use githubs pull request feature!

mattparksjr commented 6 years ago

Hi! I have changed some code. Also in your example I see the name was properly kept. Please pull the latest code and let me know.

mattparksjr commented 6 years ago

No comment for extended time. We assume this issue has been fixed.