Convert Emails to PDF using REST API in PHP

Convert Emails to PDF using REST API in PHP.

In certain cases, we may need to convert our emails and Outlook messages to PDF documents. Such conversion allows us to keep a record of important emails or share them in a portable form. We can also convert the documents attached in emails to PDF documents programmatically. In this article, we will learn how to convert emails and MSG files to PDF documents using a REST API in PHP.

The following topics shall be covered in this article:

Email to PDF Conversion REST API and PHP SDK

For converting EML and MSG files to PDF documents, we will be using the PHP SDK of GroupDocs.Conversion Cloud API. It enables us to seamlessly convert documents and images of any supported file format to any format we require. Please install it using the following command in the console:

composer require groupdocscloud/groupdocs-conversion-cloud

After installation, please use the Composers’ autoload to use the SDK as shown below:

require_once('vendor/autoload.php');

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

Convert Emails to PDF using a REST API in PHP

We can easily convert emails to PDF documents programmatically on the cloud by following the simple steps given below:

  1. Upload the EML file to the cloud
  2. Convert EML to PDF
  3. Download the converted PDF file

Upload the EML File

Firstly, we will upload the EML file to the cloud using the following code sample:

As a result, the uploaded file will be available in the files section of the dashboard on the cloud.

Convert EML to PDF in PHP

Now, we will convert emails from the uploaded EML file to a PDF document by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, provide the uploaded EML file path, the convert format and output file path.
  • Then, set various EmlLoadOptions such as setDisplayHeader, setDisplayEmailAddress, etc.
  • Optionally, set various PdfConvertOptions such as setCenterWindowsetMarginTopsetMarginLeft, etc.
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, convert EML using the convertDocument() method with ConvertDocumentRequest.

The following code sample shows how to convert an EML file to a PDF document using a REST API in PHP.

Convert EML to PDF using a REST API in PHP
Convert EML to PDF using a REST API in PHP.

Download the Converted File

The above code sample will save the converted PDF file on the cloud. It can be downloaded using the following code sample:

Outlook MSG to PDF Conversion using REST API in PHP

We can convert Outlook MSG files to PDF documents by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, provide the uploaded MSG file path, the convert format and output file path.
  • Then, set various MsgLoadOptions such as setDisplayCcEmailAddress, etc.
  • Optionally, set various PdfConvertOptions such as setCenterWindowsetMarginTopsetMarginLeft, etc.
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, convert MSG to PDF using the convertDocument() method with ConvertDocumentRequest.

The following code sample shows how to convert an MSG file to a PDF document using a REST API in PHP. Please follow the steps mentioned earlier to upload and download a file.

Outlook MSG to PDF Conversion using REST API in PHP.
Outlook MSG to PDF Conversion using REST API in PHP.

How to Convert Email Attachments to PDF in PHP

We can also convert email attachments to PDF documents programmatically by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, provide the uploaded MSG file path, the convert format and output file path.
  • Create an instance of the MsgLoadOptions
  • Set the convertAttachments property to true
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, convert EML using the convertDocument() method with ConvertDocumentRequest.

The following code sample shows how to convert email attachments to a PDF document using a REST API in PHP. Please follow the steps mentioned earlier to upload and download a file.

How to Convert Email Attachments to PDF in PHP
Convert Email Attachments to PDF in PHP.

Try Online

Please try the following free online EML to PDF and MSG to PDF conversion tools, which are developed using the above API.

Conclusion

In this article, we have learned:

  • how to convert EML to PDF using PHP;
  • how to convert Outlook MSG file to a PDF document;
  • Save email attachments as PDF in PHP;
  • how to programmatically upload EML file to the cloud;
  • how to download PDF file from the cloud.

Besides, you can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also