-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSendEmailWithAttachments.php
38 lines (29 loc) · 1.27 KB
/
SendEmailWithAttachments.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
use Office365\GraphServiceClient;
use Office365\Outlook\BodyType;
use Office365\Outlook\EmailAddress;
use Office365\Outlook\ItemBody;
use Office365\Outlook\Messages\FileAttachment;
use Office365\Outlook\Messages\Message;
use Office365\Runtime\Auth\AADTokenProvider;
use Office365\Runtime\Auth\UserCredentials;
require_once '../vendor/autoload.php';
function acquireToken()
{
$resource = "https://graph.microsoft.com";
$settings = include('../../tests/Settings.php');
$provider = new AADTokenProvider($settings['TenantName']);
return $provider->acquireTokenForPassword($resource, $settings['ClientId'],
new UserCredentials($settings['UserName'], $settings['Password']));
}
$client = new GraphServiceClient("acquireToken");
/** @var Message $message */
$message = $client->getMe()->getMessages()->createType();
$message->setSubject("Meet for lunch?");
$message->setBody(new ItemBody(BodyType::Text,"The new cafeteria is open."));
$message->setToRecipients([new EmailAddress(null,"[email protected]")]);
$attachment = $message->addAttachment(FileAttachment::class);
$attachment->setContentBytes("bWFjIGFuZCBjaGVlc2UgdG9kYXk=");
$attachment->setIsInline(false);
$attachment->setName("attachment.txt");
$client->getMe()->sendEmail($message,false)->executeQuery();