├── mailprinter ├── __init__.py ├── core │ ├── __init__.py │ ├── config.py │ ├── printer.py │ └── reader.py └── __main__.py ├── .gitignore ├── config.json ├── setup.py ├── LICENSE └── README.md /mailprinter/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mailprinter/core/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # installed files 2 | *egg* 3 | build 4 | dist 5 | 6 | __pycache__ 7 | *temp* 8 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "IMAP_port": 993, 3 | "IMAP_server": "imap.gmail.com", 4 | "SSL_required": true, 5 | "printer_name": "", 6 | "send_receive_interval": 60 7 | } 8 | -------------------------------------------------------------------------------- /mailprinter/core/config.py: -------------------------------------------------------------------------------- 1 | import json 2 | import sys 3 | 4 | 5 | def get_config(): 6 | 7 | with open('config.json', 'r') as f: 8 | try: 9 | data = json.load(f) 10 | return data 11 | except Exception: 12 | print('Invalid configuration detected!', file=sys.stderr) 13 | sys.exit(-1) 14 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from setuptools import setup, find_packages 4 | 5 | setup( 6 | name='mailprinter', 7 | author='Abhyudaya Sharma', 8 | author_email='sharmaabhyudaya@gmail.com', 9 | description='Print by sending an email', 10 | packages=find_packages(), 11 | package_data={'': ['config.json']}, 12 | install_requires=['pycups', 'schedule'], 13 | include_package_data=True, 14 | license='MIT' 15 | ) 16 | 17 | -------------------------------------------------------------------------------- /mailprinter/core/printer.py: -------------------------------------------------------------------------------- 1 | import cups 2 | import os 3 | 4 | 5 | def __print_file(_file, printer, description=""): 6 | #try: 7 | conn = cups.Connection() 8 | conn.printFile(printer, _file, description, {}) 9 | # except Exception as e: 10 | # print('Error:', e) 11 | 12 | 13 | def get_printers(): 14 | printers = [] 15 | for printer in cups.Connection().getPrinters(): 16 | printers.append(printer) 17 | return printers 18 | 19 | 20 | def print_pdf(pdf_bytes, printer): 21 | with open('temp.pdf', 'bw') as f: 22 | f.write(pdf_bytes) 23 | __print_file(f.name, printer) 24 | os.remove(f.name) # delete file when not needed 25 | -------------------------------------------------------------------------------- /mailprinter/__main__.py: -------------------------------------------------------------------------------- 1 | import schedule 2 | import sys 3 | import time 4 | 5 | from mailprinter.core import reader 6 | from mailprinter.core import config 7 | from mailprinter.core import printer 8 | 9 | if __name__ == '__main__': 10 | configuration = config.get_config() 11 | if not configuration['printer_name']: 12 | print('Please set up a printer_name from the available names:') 13 | print(printer.get_printers()) 14 | print('Please set the \'printer_name\' in config.json') 15 | sys.exit(1) 16 | 17 | schedule.every(int(configuration['send_receive_interval'])) \ 18 | .seconds.do(reader.read_email) 19 | 20 | print('MailPrinter is now running...') 21 | 22 | while True: 23 | schedule.run_pending() 24 | time.sleep(1) 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Abhyudaya Sharma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /mailprinter/core/reader.py: -------------------------------------------------------------------------------- 1 | import imaplib 2 | import email 3 | import os 4 | import sys 5 | 6 | from mailprinter.core import printer 7 | from mailprinter.core import config 8 | 9 | 10 | def read_email(): 11 | configuration = config.get_config() 12 | print('Checking for emails') 13 | 14 | if configuration['SSL_required']: 15 | m = imaplib.IMAP4_SSL(configuration['IMAP_server'], configuration['IMAP_port']) 16 | else: 17 | m = imaplib.IMAP4(configuration['IMAP_server'], configuration['IMAP_port']) 18 | 19 | try: 20 | username = os.environ['MAIL_PRINTER_USERNAME'] 21 | password = os.environ['MAIL_PRINTER_PASSWORD'] 22 | except KeyError: 23 | print('Please set the username and password as environment variables. ' 24 | 'See README.md for more details.', file=sys.stderr) 25 | sys.exit(-1) 26 | 27 | m.login(username, password) 28 | m.select('Inbox') 29 | 30 | # only check unseen emails 31 | typ, data = m.search(None, '(UNSEEN)') 32 | for num in data[0].split(): 33 | typ, data = m.fetch(num, '(RFC822)') 34 | msg = email.message_from_bytes(data[0][1]) 35 | 36 | for part in msg.walk(): 37 | if part.get_content_type() == 'application/pdf': 38 | printer.print_pdf(part.get_payload(decode=True), 39 | configuration['printer_name']) 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MailPrinter 2 | 3 | A Python 3 app to automatically print email attachments sent to your printer. 4 | Requires a printer configured with CUPS (Common Unix Printing System). 5 | Works on any Unix-like system including macOS and Linux, even the Raspberry Pi. 6 | It is recommended to use an account separate from your own email account. 7 | 8 | ## Setup 9 | 10 | - Configure your printer using your operating system settings. 11 | - Clone the repository and `cd` to the cloned folder. 12 | - Run `[sudo] ./setup.py install`. 13 | - Make the configurations for your email and printer in config.json. 14 | - Set the email and password for the account to be used: 15 | 16 | ```bash 17 | export MAIL_PRINTER_USERNAME="foo@bar.com" 18 | export MAIL_PRINTER_PASSWORD="password" 19 | ``` 20 | 21 | - Run `python mailprinter` to start printing your email attachments from anywhere in the world. 22 | 23 | ### Configuration options 24 | 25 | All configurations are made in config.json: 26 | 27 | ```JSON 28 | { 29 | "IMAP_port": 993, 30 | "IMAP_server": "imap.gmail.com", 31 | "SSL_required": true, 32 | "printer_name": "My_Printer", 33 | "send_receive_interval": 60 34 | } 35 | ``` 36 | 37 | - `IMAP_port:` The port used for IMAP by your email provider. Generally port `993`. 38 | - `IMAP_server`: The IP/domain of the IMAP server. 39 | - `SSL_required`: A boolean signifying whether the server requires SSL 40 | - `printer_name`: The name of the printer as used by CUPS. Run the program to see the list 41 | of all configured printers visible to the program. Copy the name of the printer to use 42 | and paste it in this field. 43 | - `send_recieve_interval`: The time interval (in seconds) between re-checking emails. 44 | 45 | NOTE: Only PDF attachments are printed right now. 46 | --------------------------------------------------------------------------------