예제 #1
0
    def clean_sender(self):
        """Cleans the :attr:`~openode.forms.AskByEmail.sender` attribute

        If the field is valid, cleaned data will receive value ``email``
        """
        raw_email = self.cleaned_data['sender']
        email = extract_first_email_address(raw_email)
        if email is None:
            raise forms.ValidationError('Could not extract email address')
        self.cleaned_data['email'] = email
        return self.cleaned_data['sender']
예제 #2
0
    def clean_sender(self):
        """Cleans the :attr:`~openode.forms.AskByEmail.sender` attribute

        If the field is valid, cleaned data will receive value ``email``
        """
        raw_email = self.cleaned_data['sender']
        email = extract_first_email_address(raw_email)
        if email is None:
            raise forms.ValidationError('Could not extract email address')
        self.cleaned_data['email'] = email
        return self.cleaned_data['sender']
예제 #3
0
    def handle_noargs(self, **options):
        """reads all emails from the INBOX of
        imap server and posts questions based on
        those emails. Badly formatted questions are
        bounced, as well as emails from unknown users

        all messages are deleted thereafter
        """
        if not openode_settings.ALLOW_ASKING_BY_EMAIL:
            raise CommandError('Asking by email is not enabled')

        #open imap server and select the inbox
        if django_settings.IMAP_USE_TLS:
            imap_getter = imaplib.IMAP4_SSL
        else:
            imap_getter = imaplib.IMAP4
        imap = imap_getter(
            django_settings.IMAP_HOST,
            django_settings.IMAP_PORT
        )
        imap.login(
            django_settings.IMAP_HOST_USER,
            django_settings.IMAP_HOST_PASSWORD
        )
        imap.select('INBOX')

        #get message ids
        junk, ids = imap.search(None, 'ALL')

        if len(ids[0].strip()) == 0:
            #with empty inbox - close and exit
            imap.close()
            imap.logout()
            return

        #for each id - read a message, parse it and post a question
        for msg_id in ids[0].split(' '):
            junk, data = imap.fetch(msg_id, '(RFC822)')
            #message_body = data[0][1]
            msg = email.message_from_string(data[0][1])
            imap.store(msg_id, '+FLAGS', '\\Deleted')
            try:
                (sender, subject, body) = parse_message(msg)
            except CannotParseEmail:
                continue

            sender = mail.extract_first_email_address(sender)
            mail.process_emailed_question(sender, subject, body)

        imap.expunge()
        imap.close()
        imap.logout()
예제 #4
0
    def handle_noargs(self, **options):
        """reads all emails from the INBOX of
        imap server and posts questions based on
        those emails. Badly formatted questions are
        bounced, as well as emails from unknown users

        all messages are deleted thereafter
        """
        if not openode_settings.ALLOW_ASKING_BY_EMAIL:
            raise CommandError('Asking by email is not enabled')

        #open imap server and select the inbox
        if django_settings.IMAP_USE_TLS:
            imap_getter = imaplib.IMAP4_SSL
        else:
            imap_getter = imaplib.IMAP4
        imap = imap_getter(django_settings.IMAP_HOST,
                           django_settings.IMAP_PORT)
        imap.login(django_settings.IMAP_HOST_USER,
                   django_settings.IMAP_HOST_PASSWORD)
        imap.select('INBOX')

        #get message ids
        junk, ids = imap.search(None, 'ALL')

        if len(ids[0].strip()) == 0:
            #with empty inbox - close and exit
            imap.close()
            imap.logout()
            return

        #for each id - read a message, parse it and post a question
        for msg_id in ids[0].split(' '):
            junk, data = imap.fetch(msg_id, '(RFC822)')
            #message_body = data[0][1]
            msg = email.message_from_string(data[0][1])
            imap.store(msg_id, '+FLAGS', '\\Deleted')
            try:
                (sender, subject, body) = parse_message(msg)
            except CannotParseEmail:
                continue

            sender = mail.extract_first_email_address(sender)
            mail.process_emailed_question(sender, subject, body)

        imap.expunge()
        imap.close()
        imap.logout()