Coder Social home page Coder Social logo

Comments (7)

Garethp avatar Garethp commented on September 14, 2024

I can see a couple of possible problems here. The first is that you're not getting the right mailItem. You say there's only 1 message in the mailbox, and you're right, the array of MailItems only has one item in the array. So $mailItem = $mail[1]; should be $mailItem = $mail[0];

Secondly, there are two kinds of attachments. Email attachments and File Attachments. In your original post on james' repo, you were trying to get an ItemAttachment, not a FileAttachment. Can you do a var_dump on $mailItem->getAttachments() ?

In order to get an ItemAttachment, you should instead do $itemAttachment= $mailItem->getAttachments()->getItemAttachment()[0]; In a release in the next week there'll be a way to get both ItemAttachments and FileAttachments in one go, but for the moment you need to get them separately like this.

from php-ews.

secdecompiled avatar secdecompiled commented on September 14, 2024

Sorry about the rookie mistake with the mailItem. I have corrected it and I am able to get the attachment's name and content type, but not the content itself (to save to a file).

Here's the php file:
require_once "vendor/autoload.php";
use jamesiarmes\PEWS\API\Type;
use jamesiarmes\PEWS\Mail\MailAPI;
$api = MailAPI::withUsernameAndPassword($server, $username, $password);

$mail = $api->getMailItems();

$mailItem = $mail[0];
$mailItem = $api->getItem($mailItem->getItemId());

var_dump($mailItem->getAttachments() );

$itemAttachment= $mailItem->getAttachments()->getItemAttachment()[0];
//var_dump ($itemAttachment);

$attachment = $api->getAttachment($itemAttachment->getAttachmentId());
$name = $attachment->getName();
$contentType = $attachment->getContentType();
////The line below errors out
$content = $attachment->getContent();

echo "The name is $name, the type is $contentType\n";

here is the php_error log:
[22-Nov-2015 10:55:01 America/Chicago] PHP Fatal error: Uncaught exception 'Exception' with message 'Property Content does not exist' in /var/www/html/php-ews2/vendor/garethp/php-ews/src/API/MagicMethodsTrait.php:59
Stack trace:
#0 /var/www/html/php-ews2/vendor/garethp/php-ews/src/API/MagicMethodsTrait.php(28): jamesiarmes\PEWS\API\Type->get('Content')
#1 /var/www/html/php-ews2/check_phish.php(34): jamesiarmes\PEWS\API\Type->__call('getContent', Array)
#2 /var/www/html/php-ews2/check_phish.php(34): jamesiarmes\PEWS\API\Type\ItemAttachmentType->getContent()
#3 {main}
thrown in /var/www/html/php-ews2/vendor/garethp/php-ews/src/API/MagicMethodsTrait.php on line 59
[22-Nov-2015 10:55:07 America/Chicago] PHP Fatal error: Uncaught exception 'Exception' with message 'Property Content does not exist' in /var/www/html/php-ews2/vendor/garethp/php-ews/src/API/MagicMethodsTrait.php:59
Stack trace:
#0 /var/www/html/php-ews2/vendor/garethp/php-ews/src/API/MagicMethodsTrait.php(28): jamesiarmes\PEWS\API\Type->get('Content')
#1 /var/www/html/php-ews2/check_phish.php(34): jamesiarmes\PEWS\API\Type->__call('getContent', Array)
#2 /var/www/html/php-ews2/check_phish.php(34): jamesiarmes\PEWS\API\Type\ItemAttachmentType->getContent()
#3 {main}
thrown in /var/www/html/php-ews2/vendor/garethp/php-ews/src/API/MagicMethodsTrait.php on line 59

Here is the var_dump:

object(jamesiarmes\PEWS\API\Type\NonEmptyArrayOfAttachmentsType)#14 (4) {
["itemAttachment":protected]=>
object(jamesiarmes\PEWS\API\Type\ItemAttachmentType)#7 (20) {
["item":protected]=>
NULL
["message":protected]=>
NULL
["calendarItem":protected]=>
NULL
["contact":protected]=>
NULL
["meetingMessage":protected]=>
NULL
["meetingRequest":protected]=>
NULL
["meetingResponse":protected]=>
NULL
["meetingCancellation":protected]=>
NULL
["task":protected]=>
NULL
["postItem":protected]=>
NULL
["attachmentId":protected]=>
object(jamesiarmes\PEWS\API\Type\AttachmentIdType)#42 (5) {
["rootItemId":protected]=>
NULL
["rootItemChangeKey":protected]=>
NULL
["id":protected]=>
string(180) "AAMkADNmMTFkZWMzLWZmZTItNGYzZC1hMzY0LTM1ZmVlOTgyMGY3NABGAAAAAABp+AqPUrsDS5mJYBbgT8cMBwDr14N485PESKioLJfn3CaYAAAARQJbAACApJo0H5XLTphU22eP91LkAAApg+3lAAABEgAQAGbvRzmMKe1CvWZ7HKssY8Q="
["_"]=>
string(0) ""
["typeMap":protected]=>
array(0) {
}
}
["name":protected]=>
string(19) "Daily Report"
["contentType":protected]=>
NULL
["contentId":protected]=>
NULL
["contentLocation":protected]=>
NULL
["size":protected]=>
int(19779)
["lastModifiedTime":protected]=>
string(19) "2015-11-21T18:43:05"
["typeMap":protected]=>
array(1) {
["lastModifiedTime"]=>
string(8) "dateTime"
}
["isInline":protected]=>
bool(false)
["
"]=>
string(0) ""
}
["fileAttachment":protected]=>
NULL
["
"]=>
string(0) ""
["_typeMap":protected]=>
array(0) {
}
}

from php-ews.

Garethp avatar Garethp commented on September 14, 2024

Okay, so if you take a look at ItemAttachmentType class and the Microsoft Documentation, you see that ItemAttachments and FileAttachments don't work in the same way.

Rather than having a content like FileAttachments, it has the Item directly. For example, if you're attaching a Mail Message (MessageType) to another mail, when you get your ItemAttachment you can do $itemAttachment->getMessage()->getSubject() to get the subject. Or if the attachment is a Calendar Item, you can do $itemAttachment->getCalendarItem()->getStart() to get the start time.

I'm still investigating this, and I'll probably include a feature to make it easier to get the item soon, but first I need to be able to analyze an email that has one of these ItemAttachments attached to it. For now, this should work. So it turns out that there's a good reason that FileAttachments and ItemAttachments are separated

from php-ews.

Garethp avatar Garethp commented on September 14, 2024

That being said, looking at this stackoverflow answer, there could be a way of saving the item. Maybe something like $content = base64_encode((string) $itemAttachment->getMessage()->getMimeContent()); but I can't confirm that yet and it's untested, just a theory

from php-ews.

Garethp avatar Garethp commented on September 14, 2024

@mtfi-lorenzo

Okay, so if you run on the dev-master branch of this library, you'll be able to save ItemAttachments now. It'll be included in the next release, but dev-master now includes MimeContent of ItemAttachments. So you should be able to save attachments like

$attachments = $item->getAttachments();
$itemAttachment = $attachments[0]->getItemAttachment();
$itemAttachment = $api->getAttachment($itemAttachment->getAttachmentId());

$contentType = $itemAttachment()->getContentType();
$content = (string) $itemAttachment()->getItemAttachment()->getMimeContent();

You'll have to figure out the file extension on your own though

from php-ews.

secdecompiled avatar secdecompiled commented on September 14, 2024

Garethp, Thank you for all your assistance. I verified I was able to get access to the attached message's attachments!

I sincerely appreciate your time and effort. Now I need to convert my old script from the original fork to use your fork for everything (e.g. moving messages to a folder, reply-all, etc).

Here is the final snippet that worked for me.

$attachments = $mailItem->getAttachments();
$theItemAttachment = $attachments->getItemAttachment();

$itemAttachment = $api->getAttachment($theItemAttachment[0]->getAttachmentId());

$contentType = $itemAttachment->getContentType();
$content = base64_decode((string) $itemAttachment->getItemAttachment()->getMimeContent());

///Use https://github.com/php-mime-mail-parser/php-mime-mail-parser to extract the MIME attachments to a file(s)

from php-ews.

Garethp avatar Garethp commented on September 14, 2024

While I don't have a Reply-All function, you can use the API::moveItem() function for moving emails. If there's any other features you come across that you use constantly, let me know and I'll see what I can do to build it in.

from php-ews.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.