2

I would like to filter my incoming emails in Thunderbird in a way that the attachment (invoice as pdf file) is printed automatically without additional confirmation or pop ups.

The email body should not be printed, only the attached pdf file.

Is there any way to achieve autoprinting of the attached files?

I tried using FilterQuilla, but with this I am only able to print the email itself, not the attachment.

There is a related question here on superuser, however that one asks for bulk/batch printing all attachments of manually selected messages (and has no answers). I want to specifically print the attachment of all incoming emails from one sender email address.

While it would be nice to have a Thunderbird based solution, this is not a requirement. It would be totally OK to use some filters in Thunderbird and forward emails to another email address, which is then accessed by another product.

By the way, I would prefer a free/open source solution.

user1251007
  • 1,175

4 Answers4

2

Here is my solution to the problem, combining some aspects of the other answers and overcoming limitations of those.

Advantages of this solution:

  • Open source solution, thus also available for commercial backgrounds
  • Really invisible, silent printing. No windows are popping up

The approach combines several steps:

  1. Filtering of emails and extraction of the attachments with Filtaquilla in Thunderbird
  2. A python script to print the files through FoxitReader. Other tools, such as AdobeReader or Ghostscript are not capable of silent printing - a window is visible for AdobeReader for quite a while and even for Ghostscript, a window is popping up.
  3. A scheduled task to run the python script on a regular basis.

Step 1

All emails are filtered in Thunderbird with Filtaquilla (available for Thunderbird 52.0-60.* as of 2019). Filtering for email sender and subject provides enough "security" for now. Emails are moved to an IMAP subfolder (as an archive for later inspection if anything went wrong). Filtaquilla extracts all attachments to a specified folder (C:\invoices). Extraction of attachments is not enabled per default in Filtaquilla - be sure to check the settings of Filtaquilla.

Step 2

Place the following print.pyw in C:\invoices:

import os
import subprocess
import sys
import glob
import time

foxit = "C:\Program Files (x86)\Foxit Software\Foxit Reader\FoxitReader.exe"
script_dir = os.path.dirname(os.path.realpath(__file__))

# get all pdf files
pdf_files = glob.glob(script_dir + "/*.pdf")

# print each pdf and delete it
for pdf_file in pdf_files:
    command = []
    command.append(foxit)
    command.append("/p")
    command.append("/h")
    command.append(pdf_file)
    proc = subprocess.Popen(command, stdout=subprocess.PIPE)
    time.sleep(10) 
    os.remove(pdf_file)

Step 3

In principle, the print.pyw file can be run with Filtaquilla. However, this can result in multiple printing of extracted pdf files if the filter finds more than one email.

To overcome this, a scheduled task (action: run program) is helpful, executed every 10 minutes or so.

  • program/script: "C:\Program Files (x86)\Python36-32\pythonw.exe"
  • arguments: "C:\invoices\print.pyw"
  • run in: C:\invoices\

Please note the missing quotes in the run in field, otherwise the script won't run. It is also important, the script and the extracted files are not located on a network drive!

user1251007
  • 1,175
1
  1. Create folder 'AttachmentsToPrint', in FilterQuilla check Save Attachments To & specify that folder
  2. Make a batch/script file that waits 60sec (to give FilterQuilla time to extract attachment before we do something with it), then print file, then delete file
  3. In FilterQuilla check Run Program & specify that batch/script file
  4. Cross your fingers

Commercial software certainly exists for this so the request is VERY possible & easy if purchased. Open-source is likely to be possible, but more complicated than the above even. Here are some links to possibly get you started: https://blog.thomashampel.com/blog/tomcat2000.nsf/dx/print-email-attachments-with-a-raspberrypi.htm https://ubuntuforums.org/showthread.php?t=935489

The problem I'd worry about is without a mail client how do you filter for spam/junk so you don't print that, also a bit worrying extract attachments could possibly cause an infection, but maybe I am just being paranoid

gregg
  • 6,307
1

You may use a two-step solution if the email server uses IMAP:

  1. Download attachments from new emails to a folder
  2. Monitor the folder for new arrivals and print them

For the first step, you could use a free product such as Mail Attachment Downloader Free Edition, described as:

Mail Attachment Downloader securely downloads and processes all your email attachments at-once based on your search preference. Leave all your mail on your server -- it does not interfere with any other email programs you use today to download your mail. You can setup various filters, like size, file type, who it is from, subject of email, date and time stamp, to specify what you want to download.

For the second step, and once the attached files are stored in the specified folder, you could print all of them using this PowerShell one-liner:

Get-ChildItem -Path 'C:\Temp\tmp2' -File | ForEach-Object { Start-Process -FilePath $_.Fullname -Verb Print -PassThru | %{sleep 10}}

The above requires an installed PDF product that supports the Print verb, for example the free Foxit Reader, where a graphical environment (login) is not required.

This PowerShell script can be stored in a .ps1 file and scheduled to run periodically in the Task Scheduler.

If you are using Adobe Reader, the latest version may stay open after the print. This can be solved by modifying the script to:

Get-ChildItem -Path 'C:\Temp\tmp2' -File | ForEach-Object { Start-Process -FilePath $_.Fullname -Verb Print -PassThru | %{sleep 10;$_} | kill}
harrymc
  • 498,455
1

You could use a (pretty small) python script like the one below, that will connect and get the first message from the sender you specify and print the message to standard output. Then, with munpack (package mpack in Debian) you can get the attachment and process it.

The message parsing can also be done in python, meaning you don't need mpack and your solution would be portable to more environments.

Check imaplib for more options, like removing the message when you are done, or doing other type of searches.

import getpass, imaplib

M = imaplib.IMAP4("yourserver")
M.login("user","password")
M.select("INBOX")
typ, data = M.search(None, 'from','your_sender')

num = data[0].split()[0]
typ, data = M.fetch(num, '(RFC822)')
print 'Message %s\n%s\n' % (num, data[0][1])

M.close()
M.logout()