none
How to subscribe to the sent mail event RRS feed

  • Question

  • Hello,

    In my app, the user create a draft email by using the office365 microsoft graph API, then, I display the edit email page using the weblink inside an iframe.
    But, I want my app to know when this draft is sent to save some information in my database about this email.
    So I search for a service which allow me to do this and I stumble on this: https://msdn.microsoft.com/en-us/office/office365/api/notify-rest-operations

    But when I try to call this url using my Graph client (PHP) I got a 404 not found error:

    POST https://outlook.office.com/api/v2.0/me/subscriptions HTTP/1.1

    use Microsoft\Graph\Model\Subscription;
    
    $subscription = new Subscription([
          'Resource'        => 'https://outlook.office.com/api/v1.0/me/messages/' . $draftEmail->getId(),
          'NotificationURL' => Url::fromRoute('mymodule.mail.notification')->setAbsolute()->toString(TRUE)->getGeneratedUrl(),
          'ChangeType'      => 'Modified',
          'ClientState'     => $draftEmail->getId()
        ]);
    
    $office365Client = new Graph();
    $office365Client->setApiVersion('/v2.0');
    $office365Client->setBaseUrl('https://outlook.office.com/api');
    $office365Client->setAccessToken($clientToken);
    $office365Client = $office365Client->createRequest('post', '/me/subscriptions')
                              ->attachBody($subscription);
    $office365Client->execute()
    GuzzleHttp\Exception\ClientException: Client error: `POST https://outlook.office.com/v2.0/me/subscriptions` resulted in a `404 Not Found` response: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xml (truncated...) in GuzzleHttp\Exception\RequestException::create() (line 113 of /srv/www/drupal/src/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php).

    Can you help me to figure out what is my mistake ?

    Thanx,

    Maxime Tual


    Thursday, December 14, 2017 2:58 PM

All replies

  • Wrong documentation: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/subscription_post_subscriptions

    I'm now facing this error:

    {"error":true,"context":"Error while calling Microsoft Graph API","message":{"error":{"code":"InvalidRequest",

    "message":"The remote name could not be resolved: my.dev.domain.name.fr","innerError":{"request-id":"169eb423-d454-4159-8346-989b86c2653d","date":"2017-12-14T15:14:31"}}}}

    How can i test this feature in dev mode ?
    Thursday, December 14, 2017 3:27 PM
  • Hello Tual Maxime,

    This forum(Outlook for Developers) is for development issues related to Outlook Object model, and I think your issue is more related to Office 365 Development, so I would move the thread to Office 365 for Developers forum. Thanks for understanding.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 15, 2017 7:52 AM
    Moderator
  • @Terry: Ok, thanx you

    @Everybody: I find the way to use this feature in dev mode using ngrok: https://ngrok.com/

    BUT... I got this error now: 

    {"error":true,"context":"Error while calling Microsoft Graph API","message":{"error":{"code":"ExtensionError","message":"Operation: Create; Exception: [Status Code: BadRequest; Reason: Bad Request]","innerError":{"request-id":"a288d197-6139-496f-be1a-e7c68dd19407","date":"2017-12-15T14:34:35"}}}}

    No luck on google and it seems I'm not the first one to get this:

    https://answers.microsoft.com/en-us/onedrive/forum/odwork-odfiles-sdweb/getting-400-bad-request-error-for-msgraphs-create/d8d0588b-edaf-4b35-9475-fa6ecc9aa77d?auth=1

    https://stackoverflow.com/questions/42304324/getting-400-bad-request-error-for-msgraphs-create-subscription-api-error-code

    Help?


    Friday, December 15, 2017 2:41 PM
  • Found it !

    Here's the working code:
    $subscription = new Subscription([
          'resource'        => "me/mailFolders('Inbox')/messages?filter=id eq %27" . $draftEmail->getId() . '%27', 
          'notificationUrl' => 'https://my.domain.fr',
          'changeType'      => 'updated',
          'expirationDateTime' => date(DATE_ISO8601, strtotime('+48 hours'))
        ]);

    The line which was wrong was:

    'resource' => 'me/messages/' . $draftEmail->getParentFolderId(),

    And i replace it with 

    'resource' => "me/mailFolders('Inbox')/messages?filter=id eq %27" . $draftEmail->getId() . '%27', 

    I found my answer here but in my opinion the resource parameter need to be more documented on graph api documentation and the error message return must specify WHY this is a BadRequest.

    Friday, December 15, 2017 4:15 PM