def get_messages(handler_id=None, msg_type='unread', **kwargs):
    q = EmailHandler.objects.all()
    if type(handler_id) in [list, tuple, set]:
        q = q.filter(id__in=handler_id)
    elif handler_id is not None:
        q = q.filter(id__exact=handler_id)
    d = {}
    kwargs.setdefault('mark_as_read', True)
    for h in q:
        d[h.id] = []
        conf = h.incoming_mail_configuration
        if conf.protocol == 'gmail':
            bkwargs = dict(username=conf.login.username,
                           password=conf.login.password,
                           inbox_timezone=h.timezone_name)
            b = build_backend('gmail', **bkwargs)
            if msg_type == 'unread':
                msg_iter = b.get_new_messages
            elif msg_type == 'sent':
                msg_iter = b.get_sent_messages
            else:
                raise MessagingError('invalid message type: %r' % (msg_type))
            for msg in msg_iter(**kwargs):
                try:
                    h.add_message(msg)
                except:
                    traceback.print_exc()
                    return msg
                d[h.id].append(msg)
    return d
def get_messages(handler_id=None, msg_type="unread", **kwargs):
    q = EmailHandler.objects.all()
    if type(handler_id) in [list, tuple, set]:
        q = q.filter(id__in=handler_id)
    elif handler_id is not None:
        q = q.filter(id__exact=handler_id)
    d = {}
    kwargs.setdefault("mark_as_read", True)
    for h in q:
        d[h.id] = []
        conf = h.incoming_mail_configuration
        if conf.protocol == "gmail":
            bkwargs = dict(username=conf.login.username, password=conf.login.password, inbox_timezone=h.timezone_name)
            b = build_backend("gmail", **bkwargs)
            if msg_type == "unread":
                msg_iter = b.get_new_messages
            elif msg_type == "sent":
                msg_iter = b.get_sent_messages
            else:
                raise MessagingError("invalid message type: %r" % (msg_type))
            for msg in msg_iter(**kwargs):
                try:
                    h.add_message(msg)
                except:
                    traceback.print_exc()
                    return msg
                d[h.id].append(msg)
    return d
 def send_message(self, **kwargs):
     conf = self.outgoing_mail_configuration
     bkwargs = dict(username=conf.login.username,
                    password=conf.login.password,
                    hostname=conf.hostname,
                    port=conf.port,
                    use_ssl=conf.use_ssl,
                    inbox_timezone=self.timezone_name,
                    email_address=self.email_address)
     for key in bkwargs.keys():
         if type(bkwargs[key]) == unicode:
             bkwargs[key] = str(bkwargs[key])
     b = build_backend(conf.protocol, **bkwargs)
     msg = b.send_message(**kwargs)
     return msg
 def send_message(self, **kwargs):
     conf = self.outgoing_mail_configuration
     bkwargs = dict(
         username=conf.login.username,
         password=conf.login.password,
         hostname=conf.hostname,
         port=conf.port,
         use_ssl=conf.use_ssl,
         inbox_timezone=self.timezone_name,
         email_address=self.email_address,
     )
     for key in bkwargs.keys():
         if type(bkwargs[key]) == unicode:
             bkwargs[key] = str(bkwargs[key])
     b = build_backend(conf.protocol, **bkwargs)
     msg = b.send_message(**kwargs)
     return msg