Comments (7)
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.
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.
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.
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.
@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.
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.
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)
- Create a contact
- internetMessageHeaders HOT 1
- BUG FreeBusyResponse array HOT 1
- toRecipients getting null values for inplace archive mails
- Update Mail Item -> Categorize Email HOT 1
- PHP 8.2 - Deprecated dynamic properties HOT 6
- Oauth is not working with Exchange 2013
- Issue with using toRecipients property in EWS restriction HOT 2
- Daylight Savings
- How to get medias appointment
- Update contact with ExtendedProperty
- Exceptions
- Set a calendar event with custom identifier, so that I can (later on) GET an event by the same identifier HOT 3
- Change "busy" status for event. HOT 2
- Checking if an item exists (by $itemId and $changeKey) results in FATAL ERROR if the object is not found - How to handle this? HOT 1
- Also delete recurring events when master event is deleted? HOT 1
- getLegacyFreeBusyStatus() does not return WorkingElsewhere HOT 1
- Authentication error after Exchange Extended Protection was enabled HOT 2
- Add Kerberos support
- How to forward an Email
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-ews.