Forum Discussion
Bruno62
Mar 20, 2023Copper Contributor
email draft
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? th...
lbastian99
Mar 24, 2023Copper Contributor
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.
lbastian99
Mar 24, 2023Copper Contributor
Never mind, found the answer to my problem here: https://learn.microsoft.com/en-us/answers/questions/544038/unabletodeserializepostbody-error-when-testing-wit