Convert Outlook MSG to PDF in Python using Aspose Cloud APIs

Hi,

I have been trying to convert .msg files to .pdf with just the API.
I read the article explaining that it is a 2-step process, first to a .mht using the Email endpoint, and then to .pdf using either the Word or the PDF endpoint, however I have been unable to get that to work.

Sometimes I get an error related to the new authorization model, and sometimes it’s something else.
I have had no problems at all converting .doc…docx, .xls, .xlsx, .ppt or .pptx files as they are all converted in place.

None of the examples I have seen seem to apply.

Can you please point me in the right direction, or post an example, preferably in Python.

Thanks,
Matt Gibberman

@mattg1138

Thank you for contacting Aspose Support.

You can convert .msg file to .pdf in two steps:

  1. .msg to .mht using Aspose.Email REST APIs
  2. .mht to .pdf using Aspose.Words REST APIs

Following is a cURL example to convert .msg file to .pdf:

// First get Access Token
// Get App Key and App SID from https://dashboard.aspose.cloud/
curl -v "https://api.aspose.cloud/oauth2/token" \
-X POST \
-d 'grant_type=client_credentials&client_id=0B17F60A-6D69-426B-9ABD-79F35A6E9F7B&client_secret=53b8b19adffa41a3e87dbbd8858977ae' \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

// cURL example to convert .msg file to .mht
curl -v "https://api.aspose.cloud/v1.1/email/email_test2.msg?format=mht" \
-X GET \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer I1FG-zie0T0MXgKbc3JDTz9LH0TAtpHJHYJi79gyCmWau85sW8FthCD0lx1AvQxbvXMQWbJDLoOq2xG_jEj7lzlj9WGw9HtGxs_wnlA_WJ6__ogo7TJon2oRAzbGdqcwv6uvgRIWHn77HtOPbWG7TRZextZSFgK2by8U0iuhxdAFj_7PWJQcRiTgiwbLISNc6cefNDk6K81MiTvOvNeupLDQDafS7YphERPvBMJ8-ZZXoqx9jgD_kWWY7-Ew_IrOyYe7cpItHDXSzCsUcWrsu0OgqMeOVz8hialFvHAbhvp4AgTJMPrll8zgMJ-u1aVQ9UmjuE3qZoewEZeMQjhdQEca1LDITr2xYZz4gRlrRy3CzxjaMx_oTTCNXEmIQ3LQmE4DPZ9_JOlwVu-uFzKyTneEbkcHuPBA71UHMcPJS0aUsapb" \
-o email_test2.mht

// cURL example to convert .mht file to .pdf
curl -v "https://api.aspose.cloud/v1.1/words/convert?format=pdf" \
-X PUT \
-T email_test2.mht \
-H "Content-Type: multipart/form-data" \
-H "Accept: multipart/form-data" \
-H "Authorization: Bearer I1FG-zie0T0MXgKbc3JDTz9LH0TAtpHJHYJi79gyCmWau85sW8FthCD0lx1AvQxbvXMQWbJDLoOq2xG_jEj7lzlj9WGw9HtGxs_wnlA_WJ6__ogo7TJon2oRAzbGdqcwv6uvgRIWHn77HtOPbWG7TRZextZSFgK2by8U0iuhxdAFj_7PWJQcRiTgiwbLISNc6cefNDk6K81MiTvOvNeupLDQDafS7YphERPvBMJ8-ZZXoqx9jgD_kWWY7-Ew_IrOyYe7cpItHDXSzCsUcWrsu0OgqMeOVz8hialFvHAbhvp4AgTJMPrll8zgMJ-u1aVQ9UmjuE3qZoewEZeMQjhdQEca1LDITr2xYZz4gRlrRy3CzxjaMx_oTTCNXEmIQ3LQmE4DPZ9_JOlwVu-uFzKyTneEbkcHuPBA71UHMcPJS0aUsapb" \
-o email_test2.pdf

Thank you for your quick response, and while it seems that this code should work, I am repeatedly getting the following error:

{“Message”:“AmazonS3 exception: Error \‘The specified key does not exist.\’, Bucket \‘afc-filestorage\’, FilePath \‘42404/f7656ec5-5c46-41ee-b804-c84aaf02d65c/sample2.msg\’”}

I was not receiving this error prior, using the same credentials.
I checked and verified that the appSID I am using is associated with the Amazon S3 bucket I have listed as storage for that SID.
I have also verified the Amazon credentials are good.
Any help you can render would be appreciated.

Thanks,
Matt Gibberman

@mattg1138

{“Message”:“AmazonS3 exception: Error \‘The specified key does not exist.\’, Bucket \‘afc-filestorage\’, FilePath \‘42404/f7656ec5-5c46-41ee-b804-c84aaf02d65c/sample2.msg\’”}

This error means that the source file you are referring is missing on the Cloud Storage. Please ensure that before calling following API email_test2.msg file exist on the Cloud Storage:

https://api.aspose.cloud/v1.1/email/email_test2.msg?format=mht

The following cURL example explains how to upload email_test2.msg file to Aspose Cloud Storage:

    curl -v https://api.aspose.cloud/v1.1/storage/file?path=email_test2.msg \
    -X PUT \
    -T email_test2.msg \
    -H "Content-Type: multipart/form-data" \
    -H "Accept: application/json" \
    -H "Authorization: Bearer hoZ3Sn23EkZUkrSLEWOrCaJTN3TIoBoGWj4tcbYM3INFIYs1b4L07KOsT-VPMNS-LPzGMIic1AmmuJZYJA4x9tvkhN5_EL1h7GZX6y606qoppkP87I0E0COM3yU9sXwyg_dYast_q6CJHebquzP7quMyckAykeL-V2htBR7fb0uEep0hXnnjevbDf6b3_9w9GdF7N7OWuBbb2uRBQm87NO-qrR7gEmiWcCp6DyYfZBqZXmVlOmMYJF3AUThBflWwG-FoOk34yk9XiznuNH9NwoX2VqtH8GZ158COaxEf1Gu20dVoldNmZnPySv4vwjUP7HKFVpnR9Ed2IQId7vAsn3x3zLmhsW-rAp7JzF-x1PNwDCwNCtYLacYW00cCbwKjjcfQ--svXAwSFVkCRtFJFaht_YHIw039MxrUXEmSWM8pWJ8V"

@mattg1138

Kindly note the latest Aspose.Email Cloud API Version 4.0 uses JSON Web Token for the request authorization. It does not support URL signing and Oauth2 for the request authorization. Secondly, now it provides Storage API methods. So you do not need to use Aspose.Storage API anymore. Please check the sample cURL command to upload a file to cloud storage using Aspose.Email REST API. Please feel free to ask any questions or concerns.

# First get Access Token
# Get Customer Key and Customer ID from https://dashboard.aspose.cloud/
curl -X POST "https://api.aspose.cloud/connect/token" 
-d "grant_type=client_credentials&client_id=xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx&client_secret=xxxxxxxxxxxxxxxxxx" 
-H "Content-Type: application/x-www-form-urlencoded" 
-H "Accept: application/json"

#Upload file to cloud storage
curl -X PUT "https://api.aspose.cloud/v4.0/email/storage/file/test.msg" 
-H "accept: application/json" 
-H "authorization: Bearer [Access_Token]" 
-H "Content-Type: multipart/form-data" 
-F "File"="@C:/Temp/test.msg"