Mar 20 2023 02:23 AM
Hi,
I'm searching to create an email draft with power automate.
I would like power automate creates a email draft on outlook, i modify some elemets and after i send it
is it possible?
thanks
Bruno
Mar 20 2023 02:37 AM
@Bruno62 Yes you can do that by using the Graph API and a power automate flow. you need create the message using the Graph API first Create message - Microsoft Graph v1.0 | Microsoft Learn.
the Graph Send an HTTP Request Action, we can create a draft email in one simple action. The content can include dynamic data including title, to, subject, body and of course attachments.
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily.
Mar 23 2023 04:48 AM
Mar 23 2023 11:48 PM
Mar 24 2023 01:08 AM
Hello@Bruno62
You can use Graph API. You can select HTTP request Action instead of HTTP request (preview).
Reference : https://learn.microsoft.com/en-us/graph/api/user-post-messages?view=graph-rest-1.0&viewFallbackFrom=...
Mar 24 2023 01:42 AM
Mar 24 2023 08:37 AM - edited Mar 24 2023 08:39 AM
I am able to send an email on Outlook with the following code (Google Apps Script), but when I change 'me/sendmail' to 'me/messages', I get the following response: {"error":{"code":"UnableToDeserializePostBody","message":"were unable to deserialize "}}
function createDraftEmail() {
var outlookUrl = "https://outlook.office.com/api/v2.0/me/sendmail";
var accessToken = getAccessToken();
var headers = {
"Authorization": "Bearer " + accessToken,
"Content-type": "application/json"
};
var body = {
"Message": {
"Subject": "Test email",
"Body": {
"ContentType": "HTML",
"Content": "<html><body>Hello World!</body></html>"
},
"ToRecipients": [{
"EmailAddress": {
"Address": "email address removed for privacy reasons"
}
}],
"CcRecipients": [{
"EmailAddress": {
"Address": "email address removed for privacy reasons"
}
}]
}
};
var options = {
"method": "post",
"headers": headers,
"payload": JSON.stringify(body),
"muteHttpExceptions": true,
};
var response = UrlFetchApp.fetch(outlookUrl, options);
Logger.log(response);
}
Please advise. Here the code for how I am generating the accessToken (minus confidential information), but I don't believe that is where the issue lies:
function getAccessToken() {
var outlookOAuthUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
var clientId = "str";
var clientSecret = "str";
var refreshToken = "str";
var headers = {
"Content-Type": "application/x-www-form-urlencoded",
};
var payload = {
"grant_type": "refresh_token",
"client_id": clientId,
"client_secret": clientSecret,
"refresh_token": refreshToken,
"scope": "https://outlook.office.com/mail.readwrite"
};
var options = {
"method": "post",
"headers": headers,
"payload": payload,
"muteHttpExceptions": true,
};
var response = UrlFetchApp.fetch(outlookOAuthUrl, options);
var content = response.getContentText();
var tokenResponse = JSON.parse(content);
var accessToken = tokenResponse.access_token;
return accessToken;
}
Thank you.
Mar 24 2023 12:17 PM
Never mind, found the answer to my problem here: https://learn.microsoft.com/en-us/answers/questions/544038/unabletodeserializepostbody-error-when-te...