Skip to content

Commit

Permalink
Added message attach and send examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesiarmes committed Jan 13, 2017
1 parent be0b909 commit 0bd1fa7
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
Binary file added examples/assets/exchange.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions examples/message/add-attachment.php
@@ -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");
}
}
11 changes: 9 additions & 2 deletions examples/message/create.php
@@ -1,4 +1,10 @@
<?php
/**
* This example shows how to create a simple message as a draft. If you wish to
* send this immediately, change the message disposition to match your desired
* operation. To add attachments to this message see the message/add-attachment
* example. To send this message later, see the message/send example.
*/
require_once '../../vendor/autoload.php';

use \jamesiarmes\PhpEws\Client;
Expand Down Expand Up @@ -79,7 +85,8 @@

// Iterate over the created messages, printing the id for each.
foreach ($response_message->Items->Message as $item) {
$id = $item->ItemId->Id;
fwrite(STDOUT, "Created message $id\n");
$output = '- Id: ' . $item->ItemId->Id . "\n";
$output .= '- Change key: ' . $item->ItemId->ChangeKey . "\n";
fwrite(STDOUT, "Message created successfully.\n$output");
}
}
64 changes: 64 additions & 0 deletions examples/message/send.php
@@ -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");
}

0 comments on commit 0bd1fa7

Please sign in to comment.