markup email handle encoded headers

This commit is contained in:
2019-10-30 15:46:23 -07:00
parent a1aff2cb99
commit 14003e9dc2
2 changed files with 13 additions and 4 deletions

View File

@ -5,6 +5,8 @@ import smtplib
from pathlib import Path from pathlib import Path
from mailbox import Maildir from mailbox import Maildir
from email.message import EmailMessage from email.message import EmailMessage
from email.utils import formatdate
from email.header import Header, make_header
import logging import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -59,11 +61,14 @@ def send(frm, subj, msg):
msg['From'] = 'Keen ProCatalog Markup Bot <support@procatalog.io>' msg['From'] = 'Keen ProCatalog Markup Bot <support@procatalog.io>'
msg['To'] = frm msg['To'] = frm
msg['Bcc'] = 'alx-markup@procatalog.io' msg['Bcc'] = 'alx-markup@procatalog.io'
msg['Subject'] = f'Re: {subj}'
subj = f'Re: {subj}'
msg['Subject'] = Header(subj).encode()
msg['Message-ID'] = msgid() msg['Message-ID'] = msgid()
maildir = Maildir('/tmp/markup_submit_mail') maildir = Maildir('/tmp/markup_submit_mail')
maildir.add(msg) maildir.add(msg.as_bytes())
log.info(f'sending email to "{frm}": {subj}') log.info(f'sending email to "{frm}": {subj}')

View File

@ -11,6 +11,7 @@ from pathlib import Path
from email.feedparser import FeedParser from email.feedparser import FeedParser
from email.message import EmailMessage from email.message import EmailMessage
from email.header import decode_header, make_header
import django import django
from django.conf import settings from django.conf import settings
@ -33,15 +34,18 @@ def process_message(path):
parser.feed(line) parser.feed(line)
msg = parser.close() msg = parser.close()
frm = str(make_header(decode_header(msg['From'])))
subject = str(make_header(decode_header(msg['Subject'])))
found_pdf = False found_pdf = False
if msg.is_multipart(): if msg.is_multipart():
for attach in msg.walk(): for attach in msg.walk():
if attach.get_content_type() == 'application/pdf': if attach.get_content_type() == 'application/pdf':
process_attachment(msg['From'], msg['Subject'], attach)
found_pdf = True found_pdf = True
process_attachment(frm, subject, attach)
if not found_pdf: if not found_pdf:
reply_missing(msg['From'], msg['Subject']) reply_missing(frm, subject)
def process_attachment(from_address, subject, attachment): def process_attachment(from_address, subject, attachment):