Skip to content
Snippets Groups Projects
Commit 7d6e6842 authored by nimrod's avatar nimrod
Browse files

Upload receipts to Paperless.

parent 32caa914
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,10 @@ last_month != date +%Y-%m --date 'last month'
last_zip != find -maxdepth 1 -name '*.zip' \! -name '${last_month}.zip' -printf '%f\n' | sort -r | head -1
new_files != find reciepts/ -type f -newer ${last_zip}
.PHONY: send
send: ${last_month}.zip
./send_receipts "$<"
${last_month}.zip: ${new_files}
zip $@ $(patsubst %,'%',$^)
......
#!/usr/bin/env python3
"""Send last month's receipts to Paperless.tax."""
import argparse
import datetime
import email
import email.message
import mimetypes
import pathlib
import smtplib
import zipfile
import dateutil.relativedelta # pylint: disable=import-error
import passhole.passhole # pylint: disable=import-error
KEEPASS_DB = pathlib.Path("~/Documents/Database.kdbx").expanduser()
SMTP_HOST = "smtp.shore.co.il"
if __name__ == "__main__":
arg_parser = argparse.ArgumentParser(description=__doc__)
arg_parser.add_argument(
"zip_file",
help="Path to the zip file with the receipts.",
type=pathlib.Path,
)
args = arg_parser.parse_args()
# Get passwords from the Keepass database.
db = passhole.passhole.open_database(database=KEEPASS_DB)
entry = db.find_entries_by_title("Paperless")[0]
paperless_address = entry.get_custom_property("Direct email address")
entry = db.find_entries_by_title(
"LDAP", group=db.find_groups_by_name("shore.co.il")[0]
)[0]
smtp_username = entry.username
smtp_password = entry.password
# What month these receipts belong to?
now = datetime.datetime.now()
last_month = now + dateutil.relativedelta.relativedelta(months=-1)
# Build the email message.
msg = email.message.EmailMessage()
msg["Subject"] = last_month.strftime("%B %Y receipts")
msg["From"] = "nimrod@shore.co.il"
msg["To"] = paperless_address
# Attach the receipts.
with zipfile.ZipFile(args.zip_file) as zf:
for receipt in zf.namelist():
with zf.open(receipt) as attachment:
maintype, subtype = mimetypes.guess_type(receipt)[0].split("/")
msg.add_attachment(
attachment.read(),
maintype=maintype,
subtype=subtype,
filename=receipt,
)
# Send the message.
with smtplib.SMTP(SMTP_HOST) as s:
s.starttls()
s.login(smtp_username, smtp_password)
s.send_message(msg)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment