Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added message attach and send examples.
- Loading branch information
1 parent
be0b909
commit 0bd1fa7
Showing
4 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* This example shows how to add an attachment to an existing message. See | ||
* the message/create example for creating a new message and the message/send | ||
* example for sending a message that was created as a draft. | ||
*/ | ||
|
||
require_once '../../vendor/autoload.php'; | ||
|
||
use \jamesiarmes\PhpEws\Client; | ||
use \jamesiarmes\PhpEws\Request\CreateAttachmentType; | ||
|
||
use \jamesiarmes\PhpEws\Enumeration\ResponseClassType; | ||
|
||
use \jamesiarmes\PhpEws\ArrayType\NonEmptyArrayOfAttachmentsType; | ||
|
||
use \jamesiarmes\PhpEws\Type\FileAttachmentType; | ||
use \jamesiarmes\PhpEws\Type\ItemIdType; | ||
|
||
// Replace with the id of the created message and path to the file to be attach. | ||
$message_id = 'AAMkADk0N2E4OTQxLWRlOTYtNGUxZC05NzE1LTU4ZmI5NGVkZTZmYQBGAAAAAADeofKHfJ96S5ndHNLg9VfeBwAr1MfeoTJdQ7jgaw/bSgljAAAAAAEPAAAr1MfeoTJdQ7jgaw/bSgljAABueQnrAAA='; | ||
$file_path = '../assets/exchange.png'; | ||
|
||
// Set connection information. | ||
$host = ''; | ||
$username = ''; | ||
$password = ''; | ||
$version = Client::VERSION_2016; | ||
|
||
$client = new Client($host, $username, $password, $version); | ||
|
||
// Open file handlers. | ||
$file = new SplFileObject($file_path); | ||
$finfo = finfo_open(); | ||
|
||
// Build the request, | ||
$request = new CreateAttachmentType(); | ||
$request->ParentItemId = new ItemIdType(); | ||
$request->ParentItemId->Id = $message_id; | ||
$request->Attachments = new NonEmptyArrayOfAttachmentsType(); | ||
|
||
// Build the file attachment. | ||
$attachment = new FileAttachmentType(); | ||
$attachment->Content = $file->openFile()->fread($file->getSize()); | ||
$attachment->Name = $file->getBasename(); | ||
$attachment->ContentType = finfo_file($finfo, $file_path); | ||
$request->Attachments->FileAttachment[] = $attachment; | ||
|
||
$response = $client->CreateAttachment($request); | ||
|
||
// Iterate over the results, printing any error messages or the id of the new | ||
// attachment. | ||
$response_messages = $response->ResponseMessages | ||
->CreateAttachmentResponseMessage; | ||
foreach ($response_messages as $response_message) { | ||
// Make sure the request succeeded. | ||
if ($response_message->ResponseClass != ResponseClassType::SUCCESS) { | ||
$message = $response_message->ResponseCode; | ||
fwrite(STDERR, "Failed to create attachment with \"$message\"\n"); | ||
continue; | ||
} | ||
|
||
// Iterate over the created attachments, printing the id of each. | ||
foreach ($response_message->Attachments->FileAttachment as $attachment) { | ||
$id = $attachment->AttachmentId->Id; | ||
fwrite(STDOUT, "Created attachment $id\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* This example shows how to send a message that was created as a draft. To | ||
* create such a message, see the message/create example. For this request, the | ||
* change key is required. | ||
*/ | ||
|
||
require_once '../../vendor/autoload.php'; | ||
|
||
use \jamesiarmes\PhpEws\Client; | ||
use \jamesiarmes\PhpEws\Request\SendItemType; | ||
|
||
use \jamesiarmes\PhpEws\Enumeration\DistinguishedFolderIdNameType; | ||
use \jamesiarmes\PhpEws\Enumeration\ResponseClassType; | ||
|
||
use \jamesiarmes\PhpEws\ArrayType\NonEmptyArrayOfBaseItemIdsType; | ||
|
||
use \jamesiarmes\PhpEws\Type\DistinguishedFolderIdType; | ||
use \jamesiarmes\PhpEws\Type\ItemIdType; | ||
use \jamesiarmes\PhpEws\Type\TargetFolderIdType; | ||
|
||
// Replace with the id and change key of the message to be sent. | ||
$message_id = 'AAMkADk0N2E4OTQxLWRlOTYtNGUxZC05NzE1LTU4ZmI5NGVkZTZmYQBGAAAAAADeofKHfJ96S5ndHNLg9VfeBwAr1MfeoTJdQ7jgaw/bSgljAAAAAAEPAAAr1MfeoTJdQ7jgaw/bSgljAABueQnrAAA='; | ||
$change_key = 'CQAAABYAAAAr1MfeoTJdQ7jgaw/bSgljAABugzYP'; | ||
|
||
// Set connection information. | ||
$host = ''; | ||
$username = ''; | ||
$password = ''; | ||
$version = Client::VERSION_2016; | ||
|
||
$client = new Client($host, $username, $password, $version); | ||
|
||
// Build the request. | ||
$request = new SendItemType(); | ||
$request->SaveItemToFolder = true; | ||
$request->ItemIds = new NonEmptyArrayOfBaseItemIdsType(); | ||
|
||
// Add the message to the request. | ||
$item = new ItemIdType(); | ||
$item->Id = $message_id; | ||
$item->ChangeKey = $change_key; | ||
$request->ItemIds->ItemId[] = $item; | ||
|
||
// Configure the folder to save the sent message to. | ||
$send_folder = new TargetFolderIdType(); | ||
$send_folder->DistinguishedFolderId = new DistinguishedFolderIdType(); | ||
$send_folder->DistinguishedFolderId->Id = DistinguishedFolderIdNameType::SENT; | ||
$request->SavedItemFolderId = $send_folder; | ||
|
||
$response = $client->SendItem($request); | ||
|
||
// Iterate over the results, printing any error messages. | ||
$response_messages = $response->ResponseMessages->SendItemResponseMessage; | ||
foreach ($response_messages as $response_message) { | ||
// Make sure the request succeeded. | ||
if ($response_message->ResponseClass != ResponseClassType::SUCCESS) { | ||
$message = $response_message->ResponseCode; | ||
fwrite(STDERR, "Message failed to send with \"$message\"\n"); | ||
continue; | ||
} | ||
|
||
fwrite(STDOUT, "Message sent successfully.\n"); | ||
} |