Email actions: unzip an attachment, convert to PDF, decrypt a PDF and run a script

Unzip email attachments, convert or decrypt each file as PDF, then run a script on the result: steps inside steps in Mail Attachment Downloader.

This post shows how to set up a rule that unzips an email attachment, converts each file inside to PDF (or removes a PDF’s password), and then runs a script on the result. It is written for version 4.0.

Which edition you need:

You start from a rule with its conditions and its Save part set up, for example for the emails from acme.example, saved to \\fileserver\Finance\Invoices. New to rules? See save emails and attachments to folders.

Steps work on the saved files. To run them on the email itself too, set What to save to The email and its attachments: the email is then saved as an .eml file, and Convert documents can turn it into a PDF.

1. Unzip the attachment

In the Rule Editor, click Add a step…, find Unzip (type “zip” in the search box) and click Add. It goes under Then. Its options:

  • Only these files: one file name or extension per line, such as .docx and .pdf. Leave it empty to unzip every file.
  • Delete the zip file after unzipping it, if you only want the files inside.
  • Keep the folders inside the zip, to keep the zip’s own folders.
The Rule Editor with an Unzip step under Then, and its options: Delete the zip file after unzipping it, Only these files and Keep the folders inside the zip
The Unzip step and its options.

2. Convert each unzipped file, inside Unzip

Steps inside the Unzip step run for each file it unzips. To add one there:

  1. Select Unzip in the rule’s outline.
  2. Click Add a step… and, in the Add it: box, choose Inside “Unzip”.
  3. Pick Convert documents and click Add.

In the Convert documents step, click Add… to add a conversion. In the Conversion window, choose what is converted in From:, choose pdf in To:, and set the result’s name in Save as:, such as {SAVE_PATH}\{FILENAME}.pdf. Add one conversion for each kind of file, for example Word files and pictures. Tick Delete the originals after converting them if you only want the PDFs.

The Conversion window: From, To pdf, Save as with fields, Run the rules on the result, Convert with Free libraries, and the options for pictures and for the PDF made
The Conversion window of the Convert documents step.

Decrypt a PDF

Do the files arrive as password-protected PDFs? Add a conversion from PDF to pdf, with a new name in Save as: such as {SAVE_PATH}\{FILENAME}_open.pdf. Under A PDF converted, type the password in Decryption password:. The PDF it saves has no password. Remove the restrictions also drops limits such as no printing or no copying. It works the other way too: Encrypt the PDF, under The PDF made, adds a password to the PDFs you make.

The Conversion window: from PDF to pdf, saved as the file name with _open, and under A PDF converted a masked Decryption password
A PDF converted: the password opens a protected PDF, and the copy is saved without it.

3. Run a script on the converted file, inside Convert documents

Now select Convert documents, click Add a step…, choose Inside “Convert documents” in the Add it: box, and pick a step from Programs:

  • Run a script runs a script with the fields and waits for it to finish. Set the Program: (a .bat or .exe file, or powershell.exe for a PowerShell script), its Arguments: and a Timeout (seconds):. What it prints goes to the program’s logs.
  • Run a command line runs a program or a .bat file with the fields: set the Command: and its Arguments:.

Click Fields… to add fields to the arguments. Inside Convert documents, {SAVE_PATH} and {FILENAMEEXT} are the converted PDF, not the zip or the file it was made from. For example, with powershell.exe as the program, the arguments could be -File "C:\Scripts\ArchiveInvoice.ps1" "{SAVE_PATH}\{FILENAMEEXT}" "{EMAIL_FROM}".

Run: says whether the step runs For each saved file or Once per email. Click Run it once (test) to try it with a sample email’s values.

The Run a command line step: Command C:\Tools\ArchiveInvoice.exe, Arguments with the SUPPLIER, SAVE_PATH and FILENAMEEXT fields, Run For each saved file, and Run it once (test)
A Run a command line step, with fields in its arguments.

The finished rule

The rule’s outline now reads:

  • Then
    • Unzip
      • Convert documents
        • Run a script

Each zip is unzipped, each file inside is converted to PDF, and the script runs once for each PDF.

The Rule Editor of the rule Acme invoice zips: under Then, Unzip, with Convert documents inside it and Run a script inside that, running powershell.exe with -File and the saved file
Steps inside steps: Unzip, then Convert documents on each file, then Run a script on each PDF.

Click Test this rule… to try it on emails you pick. Unzipping and converting are done for real, into a temporary folder, so you can open the PDFs. The script is only described, not run. Then click Save.

A step can fail, for example when a PDF’s password is wrong. Steps under If something fails, such as Move or copy the email or Send notification, tell you or set the email aside. The program also tries a failed step again by itself. See nesting email rules and actions.

More steps for files

Other steps go well with these: Convert all files to a single PDF, Merge PDFs, Split PDF and Make PDFs searchable (OCR) (PRO Rules Server), and Upload to cloud or Copy file (PRO Rules Client and up). Need a step that isn’t there? Contact us and we’ll look at it.

Try it: download Mail Attachment Downloader and start with the free edition, or compare the PRO editions, each with a 30-day money-back guarantee.

Related