rough cut at email replies. fixed bug with expired IMAP sessions.

workspace
Brendan Howell 7 years ago
parent ffa4abb0cd
commit 1dac6a2cdb

@ -224,7 +224,7 @@ class Bureau(object):
htmlfile = os.fdopen(htmlfile, "w")
# run template with kwargs
templfile = os.path.join(self.mdir, template)
self.log.debug("printing with template: ", templfile)
self.log.debug("printing with template: %s", templfile)
templ = Template(filename=templfile)
htmlfile.write(templ.render_unicode(**kwargs))
htmlfile.close()

@ -1,7 +1,11 @@
import email
import email.mime.application
import email.mime.multipart
import email.mime.text
from email.header import decode_header, make_header
import imaplib
import os.path
import smtplib
import code128
import imapclient
@ -67,9 +71,13 @@ class MailRoom(Bureau):
self.postdb_rev = self.dbenv.open_db(b"postdb_rev")
def _connect_imap(self):
"""
connect / reconnect to imap server
"""
try:
self.imapserv.select_folder("INBOX")
except imaplib.abort:
except self.imapserv.abort as err:
print("imap connection error: ", err)
self.imapserv.login(self.login, self.password)
self.imapserv.select_folder("INBOX")
@ -197,9 +205,49 @@ class MailRoom(Bureau):
document scanner.
"""
# look up short code to get IMAP ID
shortcode, _ = data.split(".")
imap_id = self.get_imap_id(shortcode)
self._connect_imap()
# extract the sender and title
resp = self.imapserv.fetch([imap_id],
['INTERNALDATE', 'RFC822'])
internaldate = resp[imap_id][b'INTERNALDATE']
bodytext = """
This email is sent from a screenless office. If this is
too wierd or awkward, just reply and let us find a more
acceptable channel. http://screenl.es
"""
msg_data = resp[imap_id][b'RFC822'].decode('utf-8')
msg_obj = email.message_from_string(msg_data)
# put together the reply
pass
msg = email.mime.multipart.MIMEMultipart()
msg["From"] = msg_obj["To"]
# TODO: deal with ReplyTo headers properly
msg["To"] = msg_obj["From"]
msg["Date"] = email.utils.formatdate(localtime=True)
msg.attach(email.mime.text.MIMEText(bodytext))
# attach scanned image
photo = self.send("PX", "photo")["photo"]
with open(photo, "rb") as fil:
base = os.path.basename(photo)
part = email.mime.application.MIMEApplication(fil.read(), Name=base)
part['Content-Disposition'] = 'attachment; filename="%s"' % base
msg.attach(part)
# send SMTP
smtp = smtplib.SMTP(self.host, 587)
smtp.starttls()
smtp.login(self.login, self.password)
smtp.sendmail(msg["From"], msg["To"], msg.as_string())
smtp.close()
# flag as replied
self.imapserv.add_flags(imap_id, [imapclient.ANSWERED])
@add_api("unread", "Get unread mails")
def unread(self):

@ -30,7 +30,7 @@ class Photography(Bureau):
#cmd1 = "fswebcam --jpeg 95 --no-banner --resolution 320x240 /dev/null"
#cmd2 = "fswebcam --jpeg 95 --no-banner --resolution 1920x1080 "
#cmd2 += "-F 2 -S 1" + tmpimg.name
cmd2 = "uvccapture -x1920 -y1080 -o" + tmpimg.name
cmd2 = "uvccapture -d/dev/video1 -x1920 -y1080 -o" + tmpimg.name
#subprocess.check_output(cmd1.split())
subprocess.check_output(cmd2.split())
return {"photo": tmpimg.name}

Loading…
Cancel
Save