/Azure-Function-Convert-EML-to-PDF

This repository contains source files for a containerized Azure Function for converting EML files into PDF 📧. There is also a 💪 Bicep file for easy deployment.

Primary LanguageBicepMIT LicenseMIT

Azure Function (Containerized) Convert EML to PDF

This repository contains source files for a containerized Azure Function for converting EML files into PDF 📧. There is also a 💪 Bicep file for easy deployment. Built container available with docker pull ghcr.io/miberr/azure-function-convert-email-to-pdf:latest.

Features

  • 🤓 Function source code for creating your own containers
  • 💪 Bicep file for deploying the resources right away
  • 🤖 Ready made Azure Logic App to convert incoming emails
  • 📦 Built container available with docker pull ghcr.io/miberr/azure-function-convert-email-to-pdf:latest

Installation

Note

Prerequisites

  • Azure subscription
  • Resource group in Azure
  • Azure CLI installed on your machine
  • Basic understanding of containerized applications
  1. Download the 💪 Bicep file to your machine

  2. Open terminal

  3. Login to Azure CLI with command az login. A popup opens, login with your credentials.

  4. If you have multiple subscriptions, select the one you want to deploy to by typing its number and pressing enter

  5. Run command az deployment group create --template-file .\infrastructure.bicep --resource-group rg-emailtopdftest-dev --parameters environment=dev appName=emailtopdf with the following parameters:

    • .\infrastructure.bicep is the path to the downloaded template file
    • rg-emailtopdftest-dev the resource group name
    • dev is the environment, either dev or prod accepted
    • emailtopdf is the app name, this needs to be unique

    In case there is a conflict with a name for example, the CLI will tell you about it. If you see no red, that means the deployment was successful 🥳.

  6. Navigate to your resource group in Azure Portal. It should now look like this:

    Screenshot of deployed resources in Azure
  7. To initialize the secrets in Key Vault we have to send a POST request to the new endpoint.

    • Find the url by navigating to the Container App

    • Copy the value

      Finding the url for the container app
    • Add the uri at the end /api/convertEmlToPdf

    • Send a request with your chosen tool. Make sure you include the x-functions-key header. The value can be what ever, for example test. You should get 401 Unauthorized response back.

      POST https://xxxx.westeurope.azurecontainerapps.io/api/convertEmlToPdf
      x-functions-key: test
      
      {
          "file": "UmVjZWl2ZWQ6IGZyb20gVkkwUDE5MU1..."
      }
  8. The last configuration to do is to open the API connections for Outlook and OneDrive to authorize them. Click on the first one.

    Connections needing authorization
  9. Click on the error, then Authorize, login with your account and finally select Save

    Authorize connection
  10. Repeat for the another connection.

  11. Now send an email to the inbox you connected to and wait for the file to appear in OneDrive 😎

Warning

You should use what ever account you want to connect to an email to and what users OneDrive. It's best practice to use service accounts for these kind of scenarios.

Usage

As you now have everything installed, you can just sit back and relax 🏖️. If you require modifications to the Logic App for example, you cannot use the 💪 Bicep file anymore cause it would overwrite the workflow.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

Acknowledgements

This project was created as part of a blog post on converting emails into PDFs. You can read the full post here.