Esempio n. 1
0
    def apply(self, ui):
        envelope = ui.current_buffer
        mail = envelope.get_email()
        frm = decode_header(mail.get('From'))
        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):
                    if account.signature_filename:
                        name = account.signature_filename
                    else:
                        name = None
                    helper.attach(sig, mail, filename=name)
                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])
            if not reason:  # sucessfully send mail
                cmd = BufferCloseCommand(buffer=envelope)
                ui.apply_command(cmd)
                ui.notify('mail send successful')
            else:
                ui.notify('failed to send: %s' % reason, priority='error')
        else:
            ui.notify('failed to send: no account set up for %s' % saddr,
                      priority='error')
Esempio n. 2
0
    def _get_header_widget(self, force_update=False):
        """creates/returns the widget that displays the mail header"""
        if not self.displayed_headers:
            return None
        if not self.headerw or force_update:
            mail = self.message.get_email()
            # normalize values if only filtered list is shown
            norm = not (self.displayed_headers == self.all_headers)
            #build lines
            lines = []
            for k, v in mail.items():
                if k in self.displayed_headers:
                    lines.append((k, message.decode_header(v, normalize=norm)))

            cols = [HeadersList(lines)]
            bc = list()
            if self.depth:
                cols.insert(0, self._get_spacer(self.bars_at[1:]))
                bc.append(0)
                cols.insert(1, self._get_arrowhead_aligner())
                bc.append(1)
            self.headerw = urwid.Columns(cols, box_columns=bc)
        return self.headerw
Esempio n. 3
0
 def __str__(self):
     return "to: %s" % decode_header(self.mail['To'])
Esempio n. 4
0
    def apply(self, ui):
        Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8')
        if not self.mail:
            self.mail = ui.current_buffer.get_email()

        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)

        # decode header
        edit_headers = ['Subject', 'To', 'From']
        headertext = u''
        for key in edit_headers:
            value = u''
            if key in self.mail:
                value = decode_header(self.mail.get(key, ''))
            headertext += '%s: %s\n' % (key, value)

        if self.mail.is_multipart():
            for part in self.mail.walk():
                if part.get_content_maintype() == 'text':
                    bodytext = decode_to_unicode(part)
                    break
        else:
            bodytext = decode_to_unicode(self.mail)

        #write stuff to tempfile
        tf = tempfile.NamedTemporaryFile(delete=False)
        content = '%s\n\n%s' % (headertext,
                                bodytext)
        tf.write(content.encode('utf-8'))
        tf.flush()
        tf.close()
        cmd = EditCommand(tf.name, on_success=openEnvelopeFromTmpfile,
                          refocus=False)
        ui.apply_command(cmd)