markup: first email code
This commit is contained in:
72
markup/email.py
Normal file
72
markup/email.py
Normal file
@ -0,0 +1,72 @@
|
||||
import sys
|
||||
import string
|
||||
import random
|
||||
import smtplib
|
||||
from pathlib import Path
|
||||
from mailbox import Maildir
|
||||
from email.message import EmailMessage
|
||||
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
body_ok = """Hi,
|
||||
|
||||
Attached is a spreadsheet with the articles you marked up in the
|
||||
catalog.
|
||||
|
||||
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):
|
||||
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)
|
||||
|
||||
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'
|
||||
msg['Subject'] = f'Re: {subj}'
|
||||
msg['Message-ID'] = msgid()
|
||||
|
||||
maildir = Maildir('/tmp/markup_submit_mail')
|
||||
maildir.add(msg)
|
||||
|
||||
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>'
|
||||
Reference in New Issue
Block a user