Fetch icon indicating copy to clipboard operation
Fetch copied to clipboard

Undefined property: stdClass::$bytes

Open joshralph93 opened this issue 9 years ago • 3 comments

Hi there,

I'm getting the following error when an email has an attachment of 0 bytes Undefined property: stdClass::$bytes

Attachment.php:101 $this->size = $structure->bytes;

I've wrapped it in an isset, which to gets rid of the error, but not sure if this size is required elsewhere?

joshralph93 avatar Nov 10 '14 09:11 joshralph93

Did the same to "fix" this issue, please take a look into it and make a proper fix?

mfutselaar avatar Feb 04 '15 12:02 mfutselaar

Feel free to make a pull request.

tedivm avatar Jun 07 '15 06:06 tedivm

I wrapped that line in isset

Attachment.php:101

  if (isset($structure->bytes)) {
            $this->size = $structure->bytes;
        } else {
            $this->size = 0; // _mysql complained on null so I made the size 0_
        }

but now get some odd output and more attachments than were actually attached to the email

Here are my attachments

Number of attachments = 8 in total

screen shot 2016-11-23 at 09 01 15

Here is my debug code in this function

Attachment.php saveAS() function

   /**
     * This function saves the attachment to the exact specified location.
     *
     * @param  string $path
     * @return bool
     */
    public function saveAs($path)
    {
        $encoding = $this->encoding;
        $dirname = dirname($path);
        if (file_exists($path)) {
            if (!is_writable($path)) {
                return false;
            }
        } elseif (!is_dir($dirname) || !is_writable($dirname)) {
            return false;
        }

        if (($filePointer = fopen($path, 'w')) == false) {
            return false;
        }
        **echo "ATTACHMENT.PHP saveAs() filePointer is $filePointer.\n";**

        switch ($encoding) {
            case 3: //base64
                $streamFilter = stream_filter_append($filePointer, 'convert.base64-decode', STREAM_FILTER_WRITE);
                break;

            case 4: //quoted-printable
                $streamFilter = stream_filter_append($filePointer, 'convert.quoted-printable-decode', STREAM_FILTER_WRITE);
                break;

            default:
                $streamFilter = null;
        }

Here is the debug output

[2016-11-23 09:12:48] [Email Bookings] Successfully created a new task with id [59607]
You have new mail in /var/mail/nadeem
iMac:seraph3 nadeem$ php artisan bookings:email
this size is 6451
this size is 16057
this size is 0
this size is 16057
this size is 0
this size is 51521
this size is 0
this size is 10799
this size is 0
this size is 14830
this size is 0
this size is 10644
this size is 0
this size is 6454
[2016-11-23 09:13:18] [Email Bookings] Message from staff
[2016-11-23 09:13:18] [Email Bookings] Preparing to book the message in from [email protected]
[2016-11-23 09:13:18] [Email Bookings] Searching for [email protected] within site users
Message.php getAttachments() filename is 
[2016-11-23 09:13:18] [Email Bookings] Getting attachments
[2016-11-23 09:13:18] [Email Bookings] Has attachments
ATTACHMENT.PHP saveAs() filePointer is Resource id #748.
ATTACHMENT.PHP saveAs() filePointer is Resource id #750.
ATTACHMENT.PHP saveAs() filePointer is Resource id #751.
ATTACHMENT.PHP saveAs() filePointer is Resource id #752.
ATTACHMENT.PHP saveAs() filePointer is Resource id #753.
ATTACHMENT.PHP saveAs() filePointer is Resource id #754.
ATTACHMENT.PHP saveAs() filePointer is Resource id #755.
ATTACHMENT.PHP saveAs() filePointer is Resource id #756.
ATTACHMENT.PHP saveAs() filePointer is Resource id #757.
ATTACHMENT.PHP saveAs() filePointer is Resource id #758.
ATTACHMENT.PHP saveAs() filePointer is Resource id #759.
ATTACHMENT.PHP saveAs() filePointer is Resource id #760.
ATTACHMENT.PHP saveAs() filePointer is Resource id #761.
ATTACHMENT.PHP saveAs() filePointer is Resource id #762.
[2016-11-23 09:13:19] [Email Bookings] Successfully created a new task with id [59608]

and here are my attachments saved in my application.

screen shot 2016-11-23 at 09 16 55

It's hard to see exactly how many are being attached - there are 14 font awesome file/document icons and 17 delete icons (red x's) so does anybody have a clue why there are more attachments being created than actual real attachments?

I think it's got something to do with reading the structure of each section to find any attachments in each section.

Attachments.php:14

/**
 * This library is a wrapper around the Imap library functions included in php. This class wraps around an attachment
 * in a message, allowing developers to easily save or display attachments.
 *
 * @package Fetch
 * @author  Robert Hafner <[email protected]>
 */
class Attachment
{

    /**
     * This is the structure object for the piece of the message body that the attachment is located it.
     *
     * @var \stdClass
     */
    protected $structure;

    /**
     * This is the unique identifier for the message this attachment belongs to.
     *
     * @var int
     */
    protected $messageId;

    /**
     * This is the ImapResource.
     *
     * @var resource
     */
    protected $imapStream;

    /**
     * This is the id pointing to the section of the message body that contains the attachment.
     *
     * @var int
     */
    protected $partId;

    /**
     * This is the attachments filename.
     *
     * @var string
     */
    protected $filename;

    /**
     * This is the size of the attachment.
     *
     * @var int
     */
    protected $size;

    /**
     * This stores the data of the attachment so it doesn't have to be retrieved from the server multiple times. It is
     * only populated if the getData() function is called and should not be directly used.
     *
     * @internal
     * @var array
     */
    protected $data;

Clearly the above variables show this file handles attachments.

Help would be appreciated!

PS. This function's comments are incomplete so it's not easy to understand

Message.php:702

 /**
     * This function returns the attachments a message contains. If a filename is passed then just that ImapAttachment
     * is returned, unless
     *
     * @param  null|string $filename
     * @return array|bool|Attachment[]
     */
    public function getAttachments($filename = null)
    {
        echo "Message.php getAttachments() filename is $filename\n";
//        echo "Message.php getAttachments() this->attachments";
//        print_r($this->attachments);
//        echo "\n";

        if (!isset($this->attachments) || count($this->attachments) < 1)
            return false;

        if (!isset($filename))
            return $this->attachments;

        $results = array();
        foreach ($this->attachments as $attachment) {
            if ($attachment->getFileName() == $filename)
                $results[] = $attachment;
        }

        switch (count($results)) {
            case 0:
                return false;

            case 1:
                return array_shift($results);

            default:
                return $results;
                break;
        }
    }

deemyboy avatar Nov 23 '16 09:11 deemyboy