Пример #1
0
def create_and_send_mail(case, template, params):    
    mail = Email(sender=params['sender'], to=params['to'], subject=params['subject'],
                 body=params['body'], case=case)
    mail.save()
    # fake mail fields for now, template is not used
    send_mail(params['subject'], params['body'], params['sender'],
              [params['to']], fail_silently=False)
Пример #2
0
def collect_mail(request):
    user = getattr(settings, "IMAP_USER", None)
    passwd = getattr(settings, "IMAP_PASSWD", None)
    host = getattr(settings, "IMAP_HOST", None)
    port = int(getattr(settings, "IMAP_PORT", "993"))
    log = ""
    if user and passwd and host:
        m = imaplib.IMAP4_SSL(host, port)
        m.login(user, passwd)
        m.select("SecurityTest")
        resp, items = m.search(None, 'ALL')
        items = items[0].split()
        nbNewMailObjects = 0
        for emailid in items[::-1]:
            resp, data = m.fetch(emailid, "(RFC822)")
            for response_part in data:
                if isinstance(response_part, tuple):
                    msg = email.message_from_string(response_part[1])
                    log += ("%d " % nbNewMailObjects) + str(msg['subject']) + "<br>"
                    case = find_case_for_email(msg['subject'])
                    log += str(case) + "<br>"
                    if case:
                        date = datetime.datetime.fromtimestamp(time.mktime(email.utils.parsedate(msg["Date"])))
                        mailObject = Email(date=date,
                                           sender=msg["From"],
                                           to=msg["To"],
                                           cc=msg["cc"],
                                           bcc=msg["bcc"],
                                           subject=msg['subject'],
                                           body=msg.get_payload(),
                                           case=case)
                        mailObject.save()
                        handle_new_mail(case, mailObject)
                        nbNewMailObjects += 1
        m.close()
        m.logout()
        # XXX provide better output and translation of it
        return HttpResponse(log+"<html><body>Read %d mails and attached %d to cases</body></html>" % (len(items), nbNewMailObjects))
    else:
        # XXX provide better output and translation of it
        return HttpResponse("<html><body>Unable to find IMAP user or password</br>Giving up</body></html>")