rob-luke / emails-html-to-pdf

Convert emails without attachments to pdf and send as email
33 stars 6 forks source link

Email to PDF to email

This script will check an imap folder for unread emails. Any unread email that does not have an attachment will be converted to a pdf and then emailed to the address you specify. The script is run at a configurable interval.

This was built to integrate with paperless-ng which works with pdf attachements. However, I get many documents that are html only, so I wanted them converted to pdf for storage in paperless-ng.

Usage

The following parameters are used (defaults in parentheses):

Docker-Compose

1. Use prebuilt image

This image is stored in the github registry, so you can use it without downloading this code repository. The image address is ghcr.io/rob-luke/emails-html-to-pdf:latest. So to use it in a docker-compose it would be something like...

version: "3.8"

services:

  email2pdf:
    image: ghcr.io/rob-luke/emails-html-to-pdf:latest
    container_name: email2pdf
    environment:
      - IMAP_URL=imap.provider.com
      - IMAP_USERNAME=user@provider.net
      - IMAP_PASSWORD=randompassword
      - IMAP_FOLDER=Paperless
      - SMTP_URL=smtp.provider.com
      - MAIL_SENDER=user+paperless@provider.net
      - MAIL_DESTINATION=user+paperless@provider.net
      - INTER_RUN_INTERVAL=600
      - HOSTS=127.0.0.1 tracking.paypal.com
      - WKHTMLTOPDF_OPTIONS={"load-media-error-handling":"ignore"}

2. Build image yourself

Open the docker-compose file and enter your details in the environment. This will run the script every minute.

docker-compose up -d

Python

Or if you prefer you can run the script manually by running these commands.

poetry install
poetry run src/main.py

Hints

Possible Errors

PayPal Mail with HostNotFoundErrors

Development

The recommended editor for development is either IntelliJ or Visual Studio Code

Visual Studio Code

For Visual Studio Code, it is recommended to use the devcontainer included in the repository. With the Remote - Containers extension installed, you should be prompted to open the devcontainer when opening the folder.

For debugging, copy the env.example file and rename it to just env. Then edit the variables inside to the required values for testing. These will be automatically configured when launching via either the debug menu or by pressing F5. The env file is included in the gitignore.

Formatting issues will cause the github build to fail. To fix formatting issues in your script, open the file and run the "Format Document" command.