skip to Main Content

How could I create email drafts?
I’ve been trying to find out how I can save emails as drafts using php in outlook.
By executing script draft email is generated on outlook draft section and i can send the email any time if required.

2

Answers


  1. This kind of question is way too open-ended for SO, but here’s a top-level view of what you could do.

    You could create a message with PHP and upload it to your drafts folder via Outlook’s API or IMAP (there is an example of doing this for sent messages in the PHPMailer gmail example).

    Conversely, you could create and save a draft in Outlook, then access it through an API or IMAP to pull it into your PHP script where you can do what you like with it, including sending it or uploading to another account.

    Login or Signup to reply.
  2. See: the Graph API docs

    Which you can use via their PHP SDK:

    composer require microsoft/microsoft-graph

    You basically just need to make sure isDraft is set to true, and then the message will go into your Drafts folder.

    $token = 'your API token';
    $user = 'the userid of the account you want to put the draft into';
    
    $body = [
        'isDraft' => true,
        'toRecipients' => 'the recipient(s)',
        'subject' => 'the subject',
        'body' => [
           'content' => 'the HTML body',
           'contentType' => 'HTML',
        ],
    ];
    
    (new MicrosoftGraphGraph())
        ->setAccessToken($token)
        ->createRequest('POST', "/users/$user/messages")
        ->attachBody($body)
        ->execute();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search