def add(self, message): """ Add the message to mailbox. Message can be an instance of email.Message.Message (including instaces of mailbox.Message and its subclasses ); or an open file handle or a string containing an RFC822 message. Return the highest UID in the mailbox, which should be, but is not guaranteed to be, the UID of the message that was added. Raise ImapNotOkError if a non-OK response is received from the server """ if self.readonly: raise ReadOnlyError("Tried to add to a read-only mailbox") message = ImapMessage(message) flags = message.flagstring() date_time = message.internaldatestring() memoryfile = StringIO() generator = Generator(memoryfile, mangle_from_=False) generator.flatten(message) message_str = memoryfile.getvalue() (code, data) = self._server.append(self.name, flags, \ date_time, message_str) if code != 'OK': raise ImapNotOkError("%s in add: %s" % (code, data)) try: return self.get_all_uids()[-1] except IndexError: return 0
def pipe_message(message, command): """ Pipe the message through a shell command: cat message | commmand > message message is assumed to be an instance of ImapMessage Returns modified message as instance of ImapMessage """ p = subprocess.Popen([command], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True) (child_stdout, child_stdin) = (p.stdout, p.stdin) memoryfile = StringIO() generator = Generator(memoryfile, mangle_from_=False, maxheaderlen=60) generator.flatten(message) child_stdin.write(memoryfile.getvalue()) child_stdin.close() modified_message = ImapMessage(child_stdout) child_stdout.close() modified_message.set_imapflags(message.get_imapflags()) modified_message.internaldate = message.internaldate if hasattr(message, 'myflags'): modified_message.myflags = message.myflags if hasattr(message, 'mailbox'): modified_message.mailbox = message.mailbox return modified_message
def get_message(self, uid): """ Return an ImapMessage object created from the message with UID. Raise KeyError if there if there is no message with that UID. """ rfc822string = self._cache_message(uid) result = ImapMessage(rfc822string) result.set_imapflags(self.get_imapflags(uid)) result.internaldate = self.get_internaldate(uid) result.size = self.get_size(uid) if self._factory is ImapMessage: return result return self._factory(result)
def get_header(self, uid): """ Return an ImapMessage object containing only the Header of the message with UID. Raise KeyError if there if there is no message with that UID. """ (code, data) = self._server.uid('fetch', uid, "(BODY.PEEK[HEADER])") if code != 'OK': raise ImapNotOkError("%s in fetch_header(%s)" % (code, uid)) try: rfc822string = data[0][1] except TypeError: raise KeyError("No UID %s in get_header" % uid) result = ImapMessage(rfc822string) result.set_imapflags(self.get_imapflags(uid)) result.internaldate = self.get_internaldate(uid) result.size = self.get_size(uid) if self._factory is ImapMessage: return result return self._factory(result)