Files
procat2/markup/email.py

85 lines
2.0 KiB
Python

import sys
import string
import random
import smtplib
from pathlib import Path
from mailbox import Maildir
from email.message import EmailMessage
from email.utils import formatdate
from email.header import Header, make_header
import logging
log = logging.getLogger(__name__)
body_ok = """Hi,
Attached is a copy of your marked up catalog and a spreadsheet with
the articles you selected.
Enjoy,
ProCatalog Markup Bot
"""
body_missing = """Hi,
I couldn't find a pdf attached to your message. I can't do much
without a marked up catalog pdf, so please include that when you try
again.
Thanks,
ProCatalog Markup Bot
"""
def reply(frm, subj, xls_path, pdf_path):
msg = EmailMessage()
msg.set_content(body_ok)
with open(xls_path, 'rb') as fp:
msg.add_attachment(fp.read(),
maintype='application',
subtype='vnd.openxmlformats-officedocument.spreadsheetml.sheet',
filename=Path(xls_path).name)
with open(pdf_path, 'rb') as fp:
msg.add_attachment(fp.read(),
maintype='application',
subtype='pdf',
filename=Path(pdf_path).name)
send(frm, subj, msg)
def reply_missing(frm, subj):
msg = EmailMessage()
msg.set_content(body_missing)
send(frm, subj, msg)
def send(frm, subj, msg):
msg['From'] = 'Keen ProCatalog Markup Bot <support@procatalog.io>'
msg['To'] = frm
msg['Bcc'] = 'alx-markup@procatalog.io'
subj = f'Re: {subj}'
msg['Subject'] = Header(subj).encode()
msg['Message-ID'] = msgid()
msg['Date'] = formatdate()
maildir = Maildir('/tmp/markup_submit_mail')
maildir.add(msg.as_bytes())
log.info(f'sending email to "{frm}": {subj}')
with smtplib.SMTP('mail.mk.int') as s:
s.starttls()
s.login('remotevm', '5gW311IOs')
s.send_message(msg)
def msgid():
rand = ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits, k=16))
return f'<{rand}@markup.procatalog.io>'