diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2011-08-18 18:19:55 +0100 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2011-08-18 18:19:55 +0100 |
commit | 74b149d2a20476445ff6ebe78fd235b1b38f1af8 (patch) | |
tree | b03c78a10803730885a8269e851b63e5841e3817 /alot | |
parent | 03fab7d4800ab20e53c6c3b9feb057763315a149 (diff) |
attach signature file to outgoing mails
Diffstat (limited to 'alot')
-rw-r--r-- | alot/account.py | 2 | ||||
-rw-r--r-- | alot/command.py | 57 | ||||
-rw-r--r-- | alot/helper.py | 42 |
3 files changed, 65 insertions, 36 deletions
diff --git a/alot/account.py b/alot/account.py index 720d3878..33cda217 100644 --- a/alot/account.py +++ b/alot/account.py @@ -41,7 +41,7 @@ class Account: gpg_key = None """gpg fingerprint. CURRENTLY IGNORED""" signature = None - """signature to append to outgoing mails. CURRENTLY IGNORED""" + """path to a signature file to append to outgoing mails.""" def __init__(self, address=None, aliases=None, realname=None, gpg_key=None, signature=None, sent_box=None, draft_box=None): diff --git a/alot/command.py b/alot/command.py index 3f7578a6..cc7950c2 100644 --- a/alot/command.py +++ b/alot/command.py @@ -17,8 +17,8 @@ along with notmuch. If not, see <http://www.gnu.org/licenses/>. Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com> """ import os -import glob import code +import glob import logging import threading import subprocess @@ -41,6 +41,7 @@ import buffer import settings import widgets import completion +import helper from db import DatabaseROError from db import DatabaseLockedError from completion import ContactsCompleter @@ -834,6 +835,16 @@ class EnvelopeSendCommand(Command): sname, saddr = email.Utils.parseaddr(frm) account = ui.accountman.get_account_by_address(saddr) if account: + # attach signature file if present + if account.signature: + sig = os.path.expanduser(account.signature) + if os.path.isfile(sig): + helper.attach(sig, mail, filename='signature') + else: + ui.notify('could not locate signature: %s' % sig, priority='error') + if not ui.choice('send without signature') == 'yes': + return + clearme = ui.notify('sending..', timeout=-1, block=False) reason = account.send_mail(mail) ui.clear_notify([clearme]) @@ -849,11 +860,12 @@ class EnvelopeSendCommand(Command): class EnvelopeAttachCommand(Command): - def __init__(self, path=None, **kwargs): + def __init__(self, path=None, mail=None, **kwargs): Command.__init__(self, **kwargs) self.files = [] if path: self.files = glob.glob(os.path.expanduser(path)) + self.mail = mail def apply(self, ui): if not self.files: @@ -865,40 +877,15 @@ class EnvelopeAttachCommand(Command): ui.notify('no matches, abort') return logging.info(self.files) - msg = ui.current_buffer.get_email() + + msg = self.mail + if not msg: + msg = ui.current_buffer.get_email() for path in self.files: - ctype, encoding = mimetypes.guess_type(path) - if ctype is None or encoding is not None: - # No guess could be made, or the file is encoded (compressed), - # so use a generic bag-of-bits type. - ctype = 'application/octet-stream' - maintype, subtype = ctype.split('/', 1) - if maintype == 'text': - fp = open(path) - # Note: we should handle calculating the charset - part = MIMEText(fp.read(), _subtype=subtype) - fp.close() - elif maintype == 'image': - fp = open(path, 'rb') - part = MIMEImage(fp.read(), _subtype=subtype) - fp.close() - elif maintype == 'audio': - fp = open(path, 'rb') - part = MIMEAudio(fp.read(), _subtype=subtype) - fp.close() - else: - fp = open(path, 'rb') - part = MIMEBase(maintype, subtype) - part.set_payload(fp.read()) - fp.close() - # Encode the payload using Base64 - encoders.encode_base64(part) - # Set the filename parameter - part.add_header('Content-Disposition', 'attachment', - filename=os.path.basename(path)) - msg.attach(part) - - ui.current_buffer.set_email(msg) + helper.attach(path, msg) + + if not self.mail: # set the envelope msg iff we got it from there + ui.current_buffer.set_email(msg) # TAGLIST diff --git a/alot/helper.py b/alot/helper.py index b1668d80..6aaaf917 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -22,6 +22,13 @@ from datetime import timedelta import shlex import subprocess import email +import mimetypes +import os +from email.mime.audio import MIMEAudio +from email.mime.base import MIMEBase +from email.mime.image import MIMEImage +from email.mime.text import MIMEText +from email.mime.multipart import MIMEMultipart def shorten(string, maxlen): @@ -52,3 +59,38 @@ def cmd_output(command_line): except OSError: return None return output + + +def attach(path, mail, filename=None): + ctype, encoding = mimetypes.guess_type(path) + if ctype is None or encoding is not None: + # No guess could be made, or the file is encoded (compressed), + # so use a generic bag-of-bits type. + ctype = 'application/octet-stream' + maintype, subtype = ctype.split('/', 1) + if maintype == 'text': + fp = open(path) + # Note: we should handle calculating the charset + part = MIMEText(fp.read(), _subtype=subtype) + fp.close() + elif maintype == 'image': + fp = open(path, 'rb') + part = MIMEImage(fp.read(), _subtype=subtype) + fp.close() + elif maintype == 'audio': + fp = open(path, 'rb') + part = MIMEAudio(fp.read(), _subtype=subtype) + fp.close() + else: + fp = open(path, 'rb') + part = MIMEBase(maintype, subtype) + part.set_payload(fp.read()) + fp.close() + # Encode the payload using Base64 + encoders.encode_base64(part) + # Set the filename parameter + if not filename: + filename = os.path.basename(path) + part.add_header('Content-Disposition', 'attachment', + filename=filename) + mail.attach(part) |