Skip to content

Commit

Permalink
improve mail service
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsauder committed Dec 19, 2023
1 parent f5bb809 commit e3905da
Showing 1 changed file with 175 additions and 7 deletions.
182 changes: 175 additions & 7 deletions src/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,174 @@ public function send( string|array $to, string $subject, string $content, string
}


/**
* @param string|string[] $to
* @param string $subject
* @param string $content
* @param string $from
*
* @return \Microsoft\Graph\Model\Message
*
* @throws \andrewsauder\microsoftServices\exceptions\serviceException
*/
public function createDraft( string|array $to, string $subject, string $content, string $from, string|array $cc = [], string|array $bcc = [] ): \Microsoft\Graph\Model\Message {
//get application access token
$accessToken = $this->getMicrosoftAccessToken();

//get file list
$graph = new \Microsoft\Graph\Graph();
$graph->setAccessToken( $accessToken );

$toRecipients = [];
if( !is_array( $to ) ) {
$to = [ $to ];
}
foreach( $to as $emailAddress ) {
$toRecipients[] = [
'emailAddress' => [
'address' => $emailAddress
]
];
}

$ccRecipients = [];
if( !is_array( $cc ) ) {
$cc = [ $cc ];
}
foreach( $cc as $emailAddress ) {
$ccRecipients[] = [
'emailAddress' => [
'address' => $emailAddress
]
];
}

$bccRecipients = [];
if( !is_array( $bcc ) ) {
$bcc = [ $bcc ];
}
foreach( $bcc as $emailAddress ) {
$bccRecipients[] = [
'emailAddress' => [
'address' => $emailAddress
]
];
}

if( $from=='' ) {
$from = $this->config->fromAddress;
}

$mailBody = [
'subject' => $subject,
'body' => [
'contentType' => 'HTML',
'content' => $content
],
'from' => [
'emailAddress' => [
'address' => $from
]
],
'toRecipients' => $toRecipients,
'ccRecipients' => $ccRecipients,
'bccRecipients' => $bccRecipients
];

if( count( $this->attachments )>0 ) {
$mailBody[ 'Message' ][ 'attachments' ] = $this->attachments;
}

$headers = [
'Prefer' => 'IdType="ImmutableId"'
];

try {
$message = $graph->createRequest( 'POST', '/users/' . $from . '/messages' )
->setReturnType( \Microsoft\Graph\Model\Message::class )
->addHeaders( $headers )
->attachBody( $mailBody )
->execute();
}
catch( GraphException $e ) {
error_log( $e );
throw new serviceException( 'Failed to create draft: ' . $e->getMessage(), 500, $e );
}
catch( GuzzleException $e ) {
error_log( $e );
throw new serviceException( 'Failed to create draft: ' . $e->getMessage(), $e->getCode(), $e );
}

return $message;
}


/**
* @param string|string[] $to
* @param string $subject
* @param string $content
* @param string $from
*
* @return \Microsoft\Graph\Model\Message
*
* @throws \andrewsauder\microsoftServices\exceptions\serviceException
*/
public function getMessage( string $messageId, string $from ): \Microsoft\Graph\Model\Message {
//get application access token
$accessToken = $this->getMicrosoftAccessToken();

//get file list
$graph = new \Microsoft\Graph\Graph();
$graph->setAccessToken( $accessToken );

$headers = [
'Prefer' => 'IdType="ImmutableId"'
];

try {
$message = $graph->createRequest( 'GET', '/users/' . $from . '/messages/' . $messageId )
->setReturnType( \Microsoft\Graph\Model\Message::class )
->addHeaders( $headers )
->execute();
}
catch( GraphException $e ) {
error_log( $e );
throw new serviceException( 'Failed to create draft: ' . $e->getMessage(), 500, $e );
}
catch( GuzzleException $e ) {
error_log( $e );
throw new serviceException( 'Failed to create draft: ' . $e->getMessage(), $e->getCode(), $e );
}

return $message;
}


public function sendDraft( string $messageId, string $from ): \Microsoft\Graph\Http\GraphResponse {
//get application access token
$accessToken = $this->getMicrosoftAccessToken();

//get file list
$graph = new \Microsoft\Graph\Graph();
$graph->setAccessToken( $accessToken );

try {
$response = $graph->createRequest( 'POST', '/users/' . $from . '/messages/' . $messageId . '/send' )
->execute();
}
catch( GraphException $e ) {
error_log( $e );
throw new serviceException( 'Failed to create draft: ' . $e->getMessage(), 500, $e );
}
catch( GuzzleException $e ) {
error_log( $e );
throw new serviceException( 'Failed to create draft: ' . $e->getMessage(), $e->getCode(), $e );
}

return $response;
}


/**
* @param string $emailAddress
* @param \andrewsauder\microsoftServices\mail\headers\prefer $preferHeader [optional] Defaults to html
Expand All @@ -138,7 +306,7 @@ public function getAllMessages( string $emailAddress, \andrewsauder\microsoftSer
* @throws \andrewsauder\microsoftServices\exceptions\serviceException
*/
public function getMessagesInFolder( string $emailAddress, string $mailFolderId, \andrewsauder\microsoftServices\mail\headers\prefer $preferHeader = \andrewsauder\microsoftServices\mail\headers\prefer::HTML ): array {
return $this->listMessages( '/users/' . $emailAddress . '/mailFolder/' . $mailFolderId . '/messages', $preferHeader );
return $this->listMessages( '/users/' . $emailAddress . '/mailFolders/' . $mailFolderId . '/messages', $preferHeader );
}


Expand All @@ -164,7 +332,7 @@ public function getFolders( string $emailAddress, bool $includeHiddenFolders = f

try {
$iterator = $graph->createCollectionRequest( 'GET', $url )
->setReturnType( \Microsoft\Graph\Model\MailFolder::class );
->setReturnType( \Microsoft\Graph\Model\MailFolder::class );
$folders = $iterator->getPage();
while( !$iterator->isEnd() ) {
$folders = array_merge( $folders, $iterator->getPage() );
Expand Down Expand Up @@ -199,8 +367,8 @@ private function listMessages( string $url, \andrewsauder\microsoftServices\mail

try {
$messageIterator = $graph->createCollectionRequest( 'GET', $url )
->setReturnType( \Microsoft\Graph\Model\Message::class )
->addHeaders( [ 'Prefer' => "outlook.body-content-type='" . $preferHeader->value . "'" ] );
->setReturnType( \Microsoft\Graph\Model\Message::class )
->addHeaders( [ 'Prefer' => 'outlook.body-content-type="' . $preferHeader->value . '", IdType="ImmutableId"' ] );
$messages = $messageIterator->getPage();
while( !$messageIterator->isEnd() ) {
$messages = array_merge( $messages, $messageIterator->getPage() );
Expand Down Expand Up @@ -234,8 +402,8 @@ public function getAttachments( string $emailAddress, string $messageId ): array
$graph->setAccessToken( $accessToken );

try {
$attachmentIterator = $graph->createCollectionRequest( "GET", '/users/'.$emailAddress.'/messages/' . $messageId . "/attachments" )
->setReturnType( \Microsoft\Graph\Model\Attachment::class );
$attachmentIterator = $graph->createCollectionRequest( "GET", '/users/' . $emailAddress . '/messages/' . $messageId . "/attachments" )
->setReturnType( \Microsoft\Graph\Model\Attachment::class );

$attachments = $attachmentIterator->getPage();

Expand Down Expand Up @@ -285,4 +453,4 @@ public function deleteMessage( string $emailAddress, string $messageId ): \Micro
}
}

}
}

0 comments on commit e3905da

Please sign in to comment.