def __init__(self, mail=None, headers={}, **kwargs): Command.__init__(self, **kwargs) if not mail: self.mail = MIMEMultipart() self.mail.attach(MIMEText('', 'plain', 'UTF-8')) else: self.mail = mail for key, value in headers.items(): self.mail[key] = encode_header(key, value)
def openEnvelopeFromTmpfile(): # This parses the input from the tempfile. # we do this ourselves here because we want to be able to # just type utf-8 encoded stuff into the tempfile and let alot worry # about encodings. # get input f = open(tf.name) enc = settings.config.get('general', 'editor_writes_encoding') editor_input = f.read().decode(enc) headertext, bodytext = editor_input.split('\n\n', 1) # go through multiline, utf-8 encoded headers key = value = None for line in headertext.splitlines(): if re.match('\w+:', line): #new k/v pair if key and value: # save old one from stack del self.mail[key] # ensure unique values in mails self.mail[key] = encode_header(key, value) # save key, value = line.strip().split(':', 1) # parse new pair elif key and value: # append new line without key prefix to value value += line if key and value: # save last one if present del self.mail[key] self.mail[key] = encode_header(key, value) if self.mail.is_multipart(): for part in self.mail.walk(): if part.get_content_maintype() == 'text': if 'Content-Transfer-Encoding' in part: del(part['Content-Transfer-Encoding']) part.set_payload(bodytext, 'utf-8') break f.close() os.unlink(tf.name) if self.openNew: ui.apply_command(EnvelopeOpenCommand(mail=self.mail)) else: ui.current_buffer.set_email(self.mail)
def apply(self, ui): # TODO: fill with default header (per account) # get From header if not 'From' in self.mail: accounts = ui.accountman.get_accounts() if len(accounts) == 0: ui.notify('no accounts set') return elif len(accounts) == 1: a = accounts[0] else: cmpl = AccountCompleter(ui.accountman) fromaddress = ui.prompt(prefix='From>', completer=cmpl, tab=1) validaddresses = [a.address for a in accounts] + [None] while fromaddress not in validaddresses: # TODO: not cool ui.notify('no account for this address. (<esc> cancels)') fromaddress = ui.prompt(prefix='From>', completer=cmpl) if not fromaddress: ui.notify('canceled') return a = ui.accountman.get_account_by_address(fromaddress) self.mail['From'] = "%s <%s>" % (a.realname, a.address) #get To header if 'To' not in self.mail: to = ui.prompt(prefix='To>', completer=ContactsCompleter()) if to == None: ui.notify('canceled') return self.mail['To'] = encode_header('to', to) if settings.config.getboolean('general', 'ask_subject') and \ not 'Subject' in self.mail: subject = ui.prompt(prefix='Subject>') if subject == None: ui.notify('canceled') return self.mail['Subject'] = encode_header('subject', subject) ui.apply_command(EnvelopeEditCommand(mail=self.mail))
def apply(self, ui): if not self.message: self.message = ui.current_buffer.get_selected_message() mail = self.message.get_email() reply = MIMEMultipart() Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8') if self.inline: # inline mode # set body text author = self.message.get_author()[0] mailcontent = '\nForwarded message from %s:\n' % author for line in self.message.accumulate_body().splitlines(): mailcontent += '>' + line + '\n' bodypart = MIMEText(mailcontent.encode('utf-8'), 'plain', 'UTF-8') reply.attach(bodypart) else: # attach original mode # create empty text msg bodypart = MIMEText('', 'plain', 'UTF-8') reply.attach(bodypart) # attach original msg reply.attach(mail) # copy subject subject = mail.get('Subject', '') subject = 'Fwd: ' + subject reply['Subject'] = Header(subject.encode('utf-8'), 'UTF-8').encode() # set From my_addresses = ui.accountman.get_addresses() matched_address = '' in_to = [a for a in my_addresses if a in mail.get('To', '')] if in_to: matched_address = in_to[0] else: cc = mail.get('Cc', '') + mail.get('Bcc', '') in_cc = [a for a in my_addresses if a in cc] if in_cc: matched_address = in_cc[0] if matched_address: account = ui.accountman.get_account_by_address(matched_address) fromstring = '%s <%s>' % (account.realname, account.address) reply['From'] = encode_header('From', fromstring) ui.apply_command(ComposeCommand(mail=reply))
def __init__(self, key='', value=u'', replace=True, **kwargs): self.key = key self.value = encode_header(key, value) self.replace = replace Command.__init__(self, **kwargs)
def apply(self, ui): if not self.message: self.message = ui.current_buffer.get_selected_message() mail = self.message.get_email() # set body text mailcontent = '\nOn %s, %s wrote:\n' % (self.message.get_datestring(), self.message.get_author()[0]) for line in self.message.accumulate_body().splitlines(): mailcontent += '>' + line + '\n' Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8') bodypart = MIMEText(mailcontent.encode('utf-8'), 'plain', 'UTF-8') reply = MIMEMultipart() reply.attach(bodypart) # copy subject subject = mail.get('Subject', '') if not subject.startswith('Re:'): subject = 'Re: ' + subject reply['Subject'] = Header(subject.encode('utf-8'), 'UTF-8').encode() # set From my_addresses = ui.accountman.get_addresses() matched_address = '' in_to = [a for a in my_addresses if a in mail.get('To', '')] if in_to: matched_address = in_to[0] else: cc = mail.get('Cc', '') + mail.get('Bcc', '') in_cc = [a for a in my_addresses if a in cc] if in_cc: matched_address = in_cc[0] if matched_address: account = ui.accountman.get_account_by_address(matched_address) fromstring = '%s <%s>' % (account.realname, account.address) reply['From'] = encode_header('From', fromstring) # set To del(reply['To']) if self.groupreply: cleared = self.clear_my_address(my_addresses, mail.get('To', '')) if cleared: logging.info(mail['From'] + ', ' + cleared) to = mail['From'] + ', ' + cleared reply['To'] = encode_header('To', to) logging.info(reply['To']) else: reply['To'] = encode_header('To', mail['From']) # copy cc and bcc for group-replies if 'Cc' in mail: cc = self.clear_my_address(my_addresses, mail['Cc']) reply['Cc'] = encode_header('Cc', cc) if 'Bcc' in mail: bcc = self.clear_my_address(my_addresses, mail['Bcc']) reply['Bcc'] = encode_header('Bcc', bcc) else: reply['To'] = encode_header('To', mail['From']) # set In-Reply-To header del(reply['In-Reply-To']) reply['In-Reply-To'] = '<%s>' % self.message.get_message_id() # set References header old_references = mail.get('References', '') if old_references: old_references = old_references.split() references = old_references[-8:] if len(old_references) > 8: references = old_references[:1] + references references.append('<%s>' % self.message.get_message_id()) reply['References'] = ' '.join(references) else: reply['References'] = '<%s>' % self.message.get_message_id() ui.apply_command(ComposeCommand(mail=reply))