|
|
|
@ -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):
|
|
|
|
|