Exemple #1
0
    def fetch_header(self, email_uid):
        """Retrieve the header of an email specified by return id."""
        fetch_fields = "(RFC822.SIZE BODY.PEEK[HEADER.FIELDS (%s)])"
        # print(fetch_fields, self._fields, email_uid)
        status, data = self.imapclient.uid(
            'fetch', email_uid, fetch_fields % " ".join(self._fields))
        if status != 'OK':
            print('Error retrieving headers.')
            return -1

        header = {}
        parser = HeaderParser()
        msg = parser.parsestr(str(data[0][1], 'utf-8'), True)
        for key, val in msg.items():
            if key in ['From', 'To']:
                # email addresses include two parts, (realname, email_addr)
                name, email_addr = parseaddr(val)
                header[key.lower()] = email_addr
                header[key.lower() + "name"] = MailHelper.iconv_header(name)
            else:
                header[key.lower()] = MailHelper.iconv_header(val)

        # header['size'] = long(data[0][0].split()[2])
        header['size'] = int(data[0][0].split()[2])

        # for key, val in header.items():
        #     print(key, type(val), val)

        return header
Exemple #2
0
Fichier : mail.py Projet : smbrd/o2
def main():
    mailparser = HeaderParser()

    imap = imaplib.IMAP4_SSL(host=IMAP_HOST, port=993)
    imap.login(IMAP_USER, IMAP_PASS)

    # names = [folder_name.decode('utf-8').split(' "/" ')[1] for folder_name in imap.list()[1]]
    for folder_name in INCLUDED_FOLDERS:
        imap.select(folder_name)

        tmp, messages = imap.search(None, 'ALL')
        # tmp, messages = imap.search(None, '(FROM "*****@*****.**")')

        for message_id in reversed(messages[0].split()):
            tmp, data = imap.fetch(message_id, '(BODY.PEEK[HEADER])')
            msg = mailparser.parsestr(str(data[0][1].decode('utf-8')))
            email = get_email(msg['From'])

            if email in EXCLUDE_EMAILS:
                print(f'DELETED: {decode_word(msg["Subject"])}')
                ret = imap.store(message_id, '+FLAGS', '\\Deleted')
                if ret[0] != 'OK':
                    print(ret)
    imap.close()
    imap.logout()
Exemple #3
0
def get_email_headers(filepath_list):
    """Decode header_text for each file"""

    for filepath in filepath_list:
        with open(filepath, 'r') as f:
            msg = email.message_from_file(f)  # Returns a message object structure tree from an open file object

        parser = HeaderParser()
        headers = parser.parsestr(msg.as_string())  # msg.as_string returns the entire message flattened as a string & parses the headers; returns an instance object

        msg_id = headers['Message-ID']
        date_str = headers['Date']
        from_str = headers['From']
        to_str = headers['To']
        cc_str = headers['Cc']
        bcc_str = headers['Bcc']
        subject_line = headers['Subject']

        #normalize data
        msg_id = msg_id.strip()
        datetime_obj = get_datetime(date_str)
        recipient_set = get_recipient_set(to_str, cc_str, bcc_str)
        sender_set = get_sender_set(from_str)
        subject_clean = clean_subject_line(subject_line)

        seed.load_database(msg_id, datetime_obj, recipient_set, sender_set, subject_clean)
Exemple #4
0
    def fetch(self):
        mail = imaplib.IMAP4_SSL(self._smtp_server)
        log.debug("Email smtp: {}".format(self._smtp_server))
        log.debug("Email from: {}".format(self._from_email))
        mail.login(self._from_email, self._from_pwd)
        mail.select('inbox')

        type, data = mail.search(None, 'ALL')
        mail_ids = data[0]
        id_list = mail_ids.split()

        for email_id in id_list:
            email_subject = None
            sender = None
            data = mail.fetch(str(int(email_id)), '(BODY[HEADER])')
            header_data = data[1][0][1].decode()
            parser = HeaderParser()
            msg = parser.parsestr(header_data)
            email_subject = msg['Subject'].strip()
            sender = msg['From'].strip()
            if self._sender_is_nok(sender):
                log.warning("Non OK sender: {}".format(sender))
            elif self._subject_is_cancel(email_subject):
                log.info("Got Cancel")
                self._delete(mail, id_list)
                return
            elif self._subject_is_time(email_subject):
                self._create_will_leave_at(email_subject)
                self._delete(mail, id_list)
                return

        self._delete(mail, id_list)
def checkMailForNewCommands():
    logger.info('checking gmail for commands...')
    conn = imaplib.IMAP4_SSL(imap_server)

    try:
        (retcode, capabilities) = conn.login(USERNAME, PASSWORD)
    except:
        logger.error(sys.exc_info()[1])
        sys.exit(1)

    conn.select()  # Select inbox or default namespace
    (retcode, messages) = conn.uid('search', None, '(UNSEEN)')
    if retcode == 'OK':
        for uid in messages[0].split():
            logger.info('Found an unread message with id: ' + uid)
            data = conn.uid('fetch', uid, '(BODY[HEADER])')
            header_data = data[1][0][1]
            parser = HeaderParser()
            msg = parser.parsestr(header_data)
            subjectText = msg["Subject"].lower()

            if not subjectText.startswith("[command]"):
                continue

            # if we've already processed this email, move on
            if uid in completedCommands:
                continue

            commandString = subjectText.split("[command]")[1].strip()
            completedCommands[uid] = commandString
            markCommandMailAsRead(conn, uid)
            sendCommand(commandString)

    pickle.dump(completedCommands, open(storageFileName, "wb"))
    conn.close()
def main():
	mail = imaplib.IMAP4_SSL('imap.gmail.com')#tested with gmail but should work with any IMAP email
	mail.login('*****@*****.**', 'password')#set this
	mail.list()
	mail.select("inbox")

	#Grab all the messages and pull header information from them 
	#(Restricted by date just because my inbox has thousands of messages ^_^;;;
	#Otherwise should be mail.uid('search',None,"ALL") )
	result, data = mail.uid('search',"(SINCE 22-Nov-2012)") #search and return unique ids http://tools.ietf.org/html/rfc3501#section-6.4.4
	messages = data[0].split()


	emails = []
	for uid in messages:
		result, data = mail.uid('fetch', uid, '(RFC822.SIZE BODY[HEADER.FIELDS (SUBJECT TO FROM RECEIVED)])') #Fetch just desired data
		raw_email = data[0][1]

		parser = HeaderParser()
		msg = parser.parsestr(raw_email)

		#Reorganizing the data for the template
		#probably a much better way to do this, interested in knowing it ^_^;;;
		email = dict()
		email['From'] = msg['From']
		email['To'] = msg['To']
		email['Subject'] = msg['Subject']
		email['Received'] = msg['Received'] #Returns IP addresses and SMTP id as well.
		emails.append(email)
		
	return render_template('messages.html', msg=emails)
Exemple #7
0
    def each(self, target):
        self.results = {}

        header_raw = open(target, "r").read()
        header = HeaderParser()
        parsed_headers = header.parsestr(header_raw)

        # Get Useful Headers
        self.results["From"] = decode_mime_words(parsed_headers["From"])
        self.results["ReturnPath"] = decode_mime_words(
            parsed_headers["Return-Path"])
        self.results["ReplyTo"] = decode_mime_words(parsed_headers["Reply-To"])
        self.results["To"] = decode_mime_words(parsed_headers["To"])
        self.results["Subject"] = decode_mime_words(parsed_headers["Subject"])
        self.results["Date"] = parsed_headers["Date"]
        self.results["Cc"] = decode_mime_words(parsed_headers["Cc"])

        # Parse Received and Authentication Headers
        self.results["Received"] = self.parse_received(
            parsed_headers.get_all("Received"))
        self.results["DKIM"] = self.parse_dkim(list(parsed_headers.items()))
        self.results["SPF"] = self.parse_spf(list(parsed_headers.items()))
        self.results["DMARC"] = self.parse_dmarc(list(parsed_headers.items()))

        self.results["headers"] = list(parsed_headers.items())
        self.results["highlight"] = self.highlight

        return True
Exemple #8
0
def parse_email_header(header):
    """
    Parses email headers and returns it as ordered list.
    :param header: headers
    :type header: str
    :return: parsed headers
    :rtype: tuple
    """
    headers_order = {
        'date': 1,
        'from': 2,
        'to': 3,
        'subject': 4,
        'return-path': 5,
        'delivered-to': 6,
        'in-reply-to': 7,
        'message-id': 8,
        'mime-version': 9,
        'received': 10,
        'x-mailer': 11
    }
    try:
        parser = HeaderParser()
        header = header.encode('utf-8')
        parsed_headers = parser.parsestr(header)
        if parsed_headers:
            return True, [{
                'key': header[0],
                'value': header[1],
                'order': headers_order.get(header[0].lower()) or 100
            } for header in parsed_headers.items()]
        else:
            return False, {}
    except (HeaderParseError, Exception):
        return False, {}
Exemple #9
0
def parse_docstring(docstring):
    """
    Parse out the parts of a docstring.  Return (title, body, metadata).
    """
    if not docstring:
        return '', '', {}
    docstring = cleandoc(docstring)
    parts = re.split(r'\n{2,}', docstring)
    title = parts[0]
    if len(parts) == 1:
        body = ''
        metadata = {}
    else:
        parser = HeaderParser()
        try:
            metadata = parser.parsestr(parts[-1])
        except HeaderParseError:
            metadata = {}
            body = "\n\n".join(parts[1:])
        else:
            metadata = dict(metadata.items())
            if metadata:
                body = "\n\n".join(parts[1:-1])
            else:
                body = "\n\n".join(parts[1:])
    return title, body, metadata
Exemple #10
0
def send_message(message, payload):
    """ Sends the AS2 message to the partner. Takes the message and payload as arguments and posts the as2 message to
     the partner."""

    try:
        # Parse the message header to a dictionary
        header_parser = HeaderParser()
        message_header = header_parser.parsestr(message.headers)

        # Set up the http auth if specified in the partner profile
        auth = None
        if message.partner.http_auth:
            auth = (message.partner.http_auth_user, message.partner.http_auth_pass)
        verify = True
        if message.partner.https_ca_cert:
            verify = message.partner.https_ca_cert.path

        # Send the AS2 message to the partner
        try:
            response = requests.post(message.partner.target_url,
                                     auth=auth,
                                     verify=verify,
                                     headers=dict(message_header.items()),
                                     data=payload)
            response.raise_for_status()
        except Exception, e:
            # Send mail here
            as2utils.senderrorreport(message, _(u'Failure during transmission of message to partner with error '
                                                u'"%s".\n\nTo retry transmission run the management '
                                                u'command "retryfailedas2comms".' % e))
            message.status = 'R'
            models.Log.objects.create(message=message, status='E', text=_(u'Message send failed with error %s' % e))
            return
        models.Log.objects.create(message=message, status='S', text=_(u'AS2 message successfully sent to partner'))

        # Process the MDN based on the partner profile settings
        if message.partner.mdn:
            if message.partner.mdn_mode == 'ASYNC':
                models.Log.objects.create(message=message, status='S',
                                          text=_(u'Requested ASYNC MDN from partner, waiting for it ........'))
                message.status = 'P'
                return
            # In case of Synchronous MDN the response content will be the MDN. So process it.
            # Get the response headers, convert key to lower case for normalization
            mdn_headers = dict((k.lower().replace('_', '-'), response.headers[k]) for k in response.headers)
            # create the mdn content with message-id and content-type header and response content
            mdn_content = '%s: %s\n' % ('message-id', mdn_headers['message-id'])
            mdn_content += '%s: %s\n\n' % ('content-type', mdn_headers['content-type'])
            mdn_content += response.content
            models.Log.objects.create(message=message, status='S', text=_(u'Synchronous mdn received from partner'))
            pyas2init.logger.debug('Synchronous MDN for message %s received:\n%s' % (message.message_id, mdn_content))
            save_mdn(message, mdn_content)
        else:
            message.status = 'S'
            models.Log.objects.create(message=message,
                                      status='S',
                                      text=_(u'No MDN needed, File Transferred successfully to the partner'))

            # Run the post successful send command
            run_post_send(message)
Exemple #11
0
def analyse(raw_headers):
    """
    sample output:
    {
        'To': u'*****@*****.**',
        'From': u'Dhruv <*****@*****.**>',
        'Cc': u'Shivam <*****@*****.**>',
        'Bcc': u'Abhishek <*****@*****.**>',
        'total_delay': 2,
        'trail': [
            {
                'from': '',
                'protocol': 'HTTP',
                'receivedBy': '10.31.102.130',
                'timestamp': 1452574216,
                'delay': 0
            },
            {
                'from': '',
                'protocol': 'SMTP',
                'receivedBy': 'mail-vk0-x22b.google.com',
                'timestamp': 1452574218,
                'delay': 2
            },
            {
                'from': 'mail-vk0-x22b.google.com',
                'protocol': 'ESMTPS',
                'receivedBy': 'mx.google.com',
                'timestamp': 1452574218,
                'delay': 0
            },
            {
                'from': '',
                'protocol': 'SMTP',
                'receivedBy': '10.66.77.65',
                'timestamp': 1452574218,
                'delay': 0
            }
        ]
    }
    """
    if raw_headers is None:
        return None
    raw_headers = raw_headers.strip()
    parser = HeaderParser()
    headers = parser.parsestr(raw_headers.encode('ascii', 'ignore'))
    received_headers = headers.get_all('Received')

    trail = generate_trail(received_headers)

    analysis = {
        'From': decode_and_convert_to_unicode(headers.get('From')),
        'To': decode_and_convert_to_unicode(headers.get('To')),
        'Cc': decode_and_convert_to_unicode(headers.get('Cc')),
        'Bcc': decode_and_convert_to_unicode(headers.get('Bcc')),
        'trail': trail,
        'total_delay': sum([hop['delay'] for hop in trail]) if trail else 0
    }

    return analysis
Exemple #12
0
def parse_content(content):
    parser = HeaderParser()
    data = dict()

    msg = parser.parsestr(content)
    subject = msg.get_all('subject')
    if subject:
        data['Subject'] = subject

    from_ = getaddresses(msg.get_all('from', []))
    if len(from_) > 0:
        data['From'] = from_

    to_ = getaddresses(msg.get_all('to', []))
    if len(to_) > 0:
        data['To'] = to_

    cc = getaddresses(msg.get_all('cc', []))
    if len(cc) > 0:
        data['Cc'] = cc

    bcc = getaddresses(msg.get_all('bcc', []))
    if len(bcc) > 0:
        data['Bcc'] = bcc

    sender_ip = msg.get_all('x-originating-ip')  # For Microsoft Exchange
    sender_ip_list = list()
    if sender_ip:
        for ips in sender_ip:
            sender_ip_list.append(ips.strip('[]'))
    if len(sender_ip_list) > 0:
        data['Sender IP'] = sender_ip_list

    return data
def check_mail():
	global l_s
	a,b=l_s.select('INBOX')
	dataa=[]
	try:
		typ, dataa = l_s.uid('search','(SUBJECT "'+sub+'")','UNSEEN')
	except Exception as s:
		print s
	data=dataa[0].split(' ')
	if data != [''] and len(data)>0:
		for x in range(0,len(data)):
			typ,mail= l_s.uid('fetch', data[x], '(RFC822)')
			parser = HeaderParser()
			h = parser.parsestr(mail[0][1])
			r_string=mail[0][1].decode('utf-8')
			e_msg=email.message_from_string(r_string)
			for part in e_msg.walk():
				if part.get_content_type()=='text/plain':
					body=part.get_payload(decode=True)
					body=body.split('--')[0].replace('\r','').replace(' ','').replace('\t','')
					print "Message: ",body
					for scm in body.lower().replace('\r','').split('\n'):
						dev,mode,val=wwe(scm.replace(' ','').replace('\t',''))
						if str(val)=='0' or str(val)=='1':
							dev_rply(h['Subject'],h['Message-ID'],h['From'],str(dev),str(val))
						sleep(0.5)
Exemple #14
0
 def sync_result(self, sync_result):
     self.data = sync_result["Properties"]["Body"]["Data"]
     print "MSG SYNC RESULT", sync_result["Properties"]["Body"]
     if int(sync_result["Properties"]["Body"]["Type"]) == 4:  # mime type
         # process headers
         hp = HeaderParser()
         self.parsed_message = hp.parsestr(self.data, True)
Exemple #15
0
def get_mail_details():
	r=raw_input("\nEnter the mail number:\n")
	data = mail.fetch(r, '(BODY[HEADER])')
	header_data = data[1][0][1]
	parser = HeaderParser()
	msg = parser.parsestr(header_data)
	print "\nFrom: "+msg['From'],"\nTo: "+msg['To'],"\nSubject: "+msg['Subject'],"\nDate: "+msg['Date']+"\n"
Exemple #16
0
 def UploadAllEmails(self):
     self.LoginEmail()
     self.LoginDocs()
     
     retcode, msg_ids = self.mail.search(None, 'UNSEEN')
     assert(retcode=='OK')
     
     msg_ids = [int(msg_id) for msg_id in msg_ids[0].split()] #int-ify response
     
     for msg_id in msg_ids:
         retcode, data = self.mail.fetch(msg_id, '(BODY.PEEK[HEADER])')
         assert(retcode == 'OK')
         
         p = HeaderParser()
         headers = p.parsestr(data[0][1])
         
         retcode, data = self.mail.fetch(msg_id, '(BODY.PEEK[TEXT])')
         assert(retcode == 'OK')
         
         message = email.message_from_string(data[0][1])
         for part in message.walk():
             print 'type', part.get_content_type()
             print 'body', part.get_payload()
         
         #self.UploadMsgAsGDoc(headers, message)
         
     self.mail.close()
Exemple #17
0
    def find_emails_by_subject(self, subject, limit=50):
        """
        Searches for Email by Subject.  Returns email's imap message IDs 
        as a list if matching subjects is found.

        Args:
            subject (str) - Subject to search for.

        Kwargs:
            limit (int) - Limit search to X number of matches, default 50

        Returns:
            list - List of Integers representing imap message UIDs.

        """
        # Select inbox to fetch the latest mail on server.
        self._mail.select("inbox")

        matches = []
        parser = HeaderParser()

        matching_msg_nums = self.__search_email_by_subject(subject)

        for msg_num in matching_msg_nums[-limit:]:
            _, msg_data = self._mail.fetch(msg_num, '(RFC822)')
            raw_msg = msg_data[0][1]
            msg_headers = parser.parsestr(raw_msg, True)
            if msg_headers['subject'] == subject:
                uid = re.search("UID\\D*(\\d+)\\D*",
                                self._mail.fetch(msg_num,
                                                 'UID')[1][0]).group(1)
                matches.append(uid)

        return matches
def message_delete(subject_message=None):
    try:
        server = connect_for_delete(GMAIL_IMAP_SERVER, GMAIL_IMAP_PORT,
                                    GMAIL_USER, GMAIL_APP_PASSWORD)
        # get list of mailboxes
        mailparser = HeaderParser()
        list = server.list()
        server.select(GMAIL_FOLDER_NAME)
        typ, data = server.search(None, 'ALL')
        uids = data[0].split()
        for num in uids:
            try:
                resp, data_second = server.fetch(num, '(RFC822)')
                data_decode = data_second[0][1].decode()
                msg = mailparser.parsestr(data_decode)
                print('\nMessage №', num.decode())
                print('From:', msg['From'], '\nDate:', msg['Date'],
                      '\nSubject:', msg['Subject'])
                if msg['Subject'] == subject_message:
                    server.store(num, '+FLAGS', '\\Deleted')
                    print('_________Deleted Message_________\n')
            except Exception as exception:
                print("Error: %s!" % exception)
                print('Id:', num)
        server.expunge()
        server.close()
        print()

    except Exception as exception:
        print("Error: %s!\n\n" % exception)
def checkMailForNewCommands():
	logger.info('checking gmail for commands...')
	conn = imaplib.IMAP4_SSL(imap_server)

	try:
	    (retcode, capabilities) = conn.login(USERNAME, PASSWORD)
	except:
	    logger.error(sys.exc_info()[1])
	    sys.exit(1)

	conn.select() # Select inbox or default namespace
	(retcode, messages) = conn.uid('search', None, '(UNSEEN)')
	if retcode == 'OK':
	    for uid in messages[0].split():
		logger.info('Found an unread message with id: ' + uid)
		data = conn.uid('fetch', uid, '(BODY[HEADER])')
		header_data = data[1][0][1]
		parser = HeaderParser()
		msg = parser.parsestr(header_data)
		subjectText = msg["Subject"].lower()

		if not subjectText.startswith("[command]"):
			continue
			
		# if we've already processed this email, move on
		if uid in completedCommands:
			continue
		
		commandString = subjectText.split("[command]")[1].strip()
		completedCommands[uid] = commandString
		markCommandMailAsRead(conn, uid)
		sendCommand(commandString)
	
	pickle.dump(completedCommands, open(storageFileName, "wb"))
	conn.close()
Exemple #20
0
 def imap_get_fetch_header(self, email_id):
     '''
         get header of messages
         return parsed header messages
         parameter is email_id returned from serach result
     '''
     
     # check serialize cache
     email_cache = self.imap_unserialize_email_from_file(email_id)
     if email_cache:
         return {'status':'OK', 'msg':email_cache}
     
     email_info = self.imap_get_fetch(email_id, '(BODY[HEADER])')
     if email_info.get('status').lower() != 'ok':
         return None
     
     header = email_info.get('msg')[0][1].decode('UTF-8') #get header string then decode
     parser = HeaderParser()
     
     # serialize
     parsed_header = parser.parsestr(header)
     serialized_eml = {}
     serialized_eml['ID'] = email_id
     serialized_eml['From'] = parsed_header.get('From')
     serialized_eml['To'] = parsed_header.get('To')
     serialized_eml['CC'] = parsed_header.get('CC')
     serialized_eml['BCC'] = parsed_header.get('BCC')
     serialized_eml['Subject'] = parsed_header.get('Subject')
     serialized_eml['Date'] = parsed_header.get('Date')
     self.imap_serialize_email_to_file(serialized_eml)
         
     return {'status':'OK', 'msg':parsed_header}
Exemple #21
0
def haalHeader():

    # Maak de verbinding met de mail server
    mailserver = imaplib.IMAP4_SSL(HOSTNAME)
    mailserver.login(USERNAME, PASSWORD)
    mailserver.select(MAILBOX, readonly=True)

    # Haal alle emails op die nog niet gelezen zijn
    typ, data = mailserver.search(None, 'UNSEEN')

    # Check of de verbinding is gelukt
    if typ == "OK":

        aantal_mails = len(data[0].split())

        if aantal_mails == 0:
            veranderLicht(aantal_mails)

        for num in data[0].split():
            mail = mailserver.fetch(num, '(BODY[HEADER])')

            parser = HeaderParser()
            msg = parser.parsestr(mail[1][0][1].decode("utf-8"))

            # Roep csvCheck aan met de afzender en datum/tijd
            csvCheck(msg["From"], msg["Date"], aantal_mails)
    else:
        print("Er is iets fout gegaan met het ophalen van de emails: " + typ)

    # Sluit de mail verbinding
    mailserver.close()
    mailserver.logout()

    return
Exemple #22
0
    def send(self, args):
        """Send an AMI request "action" given a dict of header values

        Keyword Arguments:
        args    A dictionary of headers and values

        Returns:
        An email.message.Message which we could wrap with our own AMIMessage
        class if we were feeling industrious. You can get
        access the  headers via:
            ami = SyncAMI()
            res = ami.send({'action': 'ping'})
            res.items() => [("header", "value"), ...]
            res.get("header") => "value"
            res.get_all("header") => ["value", ...]
        """

        headers = {}
        params = "?" + urlencode(args)
        if self.cookie is not None:
            headers['Cookie'] = self.cookie
        self.con.request("GET", self.path + params, headers=headers)
        res = self.con.getresponse()
        if res.status != 200:
            raise InvalidAMIResponse(res)
        self.cookie = res.getheader('set-cookie', None)
        data = res.read().decode('utf-8')
        parser = HeaderParser()

        return parser.parsestr(data)
Exemple #23
0
    def each(self, target):
        self.results = {}

        header_raw = open(target, 'r').read()
        header = HeaderParser()
        parsed_headers = header.parsestr(header_raw)

        # Get Useful Headers
        self.results['From'] = decode_mime_words(parsed_headers['From'])
        self.results['ReturnPath'] = decode_mime_words(
            parsed_headers['Return-Path'])
        self.results['ReplyTo'] = decode_mime_words(parsed_headers['Reply-To'])
        self.results['To'] = decode_mime_words(parsed_headers['To'])
        self.results['Subject'] = decode_mime_words(parsed_headers['Subject'])
        self.results['Date'] = parsed_headers['Date']
        self.results['Cc'] = decode_mime_words(parsed_headers['Cc'])

        # Parse Received and Authentication Headers
        self.results['Received'] = self.parse_received(
            parsed_headers.get_all('Received'))
        self.results['DKIM'] = self.parse_dkim(parsed_headers.items())
        self.results['SPF'] = self.parse_spf(parsed_headers.items())
        self.results['DMARC'] = self.parse_dmarc(parsed_headers.items())

        self.results['headers'] = parsed_headers.items()
        self.results['highlight'] = self.highlight

        return True
Exemple #24
0
def gmail_for_techxpo_test_refresh():
    global imap_server
    global imap_nickname
    global imap_username
    global imap_pass
    global mail
    global unread_count_initial
    unread_count_current = re.search("UNSEEN (\d+)",
                                     mail.status("INBOX",
                                                 "(UNSEEN)")[1][0]).group(1)
    if unread_count_current != unread_count_initial:
        mail.select()
        #finding total mail count where latest email = mail count
        status, mail_count = mail.search(None, 'ALL')
        mail_count = mail_count[0].split()
        mail_count = int(mail_count[-1])

        #getting data from latest email
        email_data = mail.fetch(mail_count, '(BODY.PEEK[])')
        header_data = email_data[1][0][1]
        parser = HeaderParser()
        email_info = parser.parsestr(header_data)

        #email sender
        email_from = email_info['From']
        email_from = email_from.split("<")[0]

        #email subject line
        email_subject = email_info['Subject']

        #print to prompt for debugging purposes
        unread_count_initial = unread_count_current
        return "New " + imap_nickname + " from " + email_from

    return None
Exemple #25
0
def gmail_for_techxpo_test_refresh():
    global imap_server
    global imap_nickname
    global imap_username
    global imap_pass
    global mail
    global unread_count_initial
    unread_count_current = re.search("UNSEEN (\d+)", mail.status("INBOX", "(UNSEEN)")[1][0]).group(1)
    if unread_count_current != unread_count_initial:
        mail.select()
        #finding total mail count where latest email = mail count
        status, mail_count = mail.search(None, 'ALL')
        mail_count = mail_count[0].split()
        mail_count = int(mail_count[-1])
        
        #getting data from latest email
        email_data = mail.fetch(mail_count, '(BODY.PEEK[])')
        header_data = email_data[1][0][1]
        parser = HeaderParser()
        email_info = parser.parsestr(header_data)

        #email sender
        email_from = email_info['From']
        email_from = email_from.split("<")[0]

        #email subject line
        email_subject = email_info['Subject']

        #print to prompt for debugging purposes
        unread_count_initial = unread_count_current
        return "New " + imap_nickname +" from " + email_from

    return None
Exemple #26
0
    def __init__(self, server, uid):
        """The folder must be selected on the server before this function is called"""
        self.OK = False

        result, data = server.uid('FETCH', uid, '(UID FLAGS INTERNALDATE BODY[HEADER.FIELDS (FROM TO CC DATE SUBJECT X-GMAIL-RECEIVED MESSAGE-ID)])')
        # data == [("uid internaldate", "headers"), ')']
        if result == 'OK':
            try:
                # The UID changes based on the selected folder, but when serializing
                # we always serialize the UID for AllMailFolder
                self['uid'] = uIdMatch.search(data[0][0]).group(1)
                self['flags'] = flagsMatch.search(data[0][0]).group(1)
                self['internaldate'] = intDateMatch.search(data[0][0]).group(1)

                parser = HeaderParser()
                msg = parser.parsestr(data[0][1], headersonly=True)
                self['sha1'] = EmailMsg.computeSha1(self['internaldate'], msg)

                # The list of folders in which this message appears
                self['folder'] = []

                self.OK = True
            except:
                logger.exception("Error retrieving message")
                self.OK = False
Exemple #27
0
def getLastMail(connection_object):
	inbox = connection_object.select('Inbox')
	raw_mail = connection_object.fetch(int(inbox[1][0]),'(BODY[last_mail])')
	raw_last_mail = raw_mail[1][0][1]
	parser_object = HeaderParser()
	last_mail = parser_object.parsestr(raw_last_mail)
	return last_mail
    def send(self, args):
        """Send an AMI request "action" given a dict of header values

        Keyword Arguments:
        args    A dictionary of headers and values

        Returns:
        An email.message.Message which we could wrap with our own AMIMessage
        class if we were feeling industrious. You can get
        access the  headers via:
            ami = SyncAMI()
            res = ami.send({'action': 'ping'})
            res.items() => [("header", "value"), ...]
            res.get("header") => "value"
            res.get_all("header") => ["value", ...]
        """

        headers = {}
        params = "?" + urlencode(args)
        if self.cookie is not None:
            headers['Cookie'] = self.cookie
        self.con.request("GET", self.path + params, headers=headers)
        res = self.con.getresponse()
        if res.status != 200:
            raise InvalidAMIResponse(res)
        self.cookie = res.getheader('set-cookie', None)
        data = res.read()
        parser = HeaderParser()

        return parser.parsestr(data)
    def handle(self, *args, **options):

        if len(args) != 1:
            print('error: Need a project.')
            sys.exit(1)

        try:
            project = Project.objects.get(linkname=args[0])
        except Project.DoesNotExist:
            print("error: can't find project '%s'" % args[0])
            sys.exit(1)

        parser = HeaderParser()
        query = Patch.objects.filter(project=project)
        count = query.count()
        for i, patch in enumerate(query.iterator()):
            if (i % 10) == 0:
                sys.stdout.write("%06d/%06d\r" % (i, count))
                sys.stdout.flush()

            headers = parser.parsestr(patch.headers)
            new_project = find_project(headers)
            if new_project == patch.project:
                continue

            patch.project = new_project
            patch.save()
            series = find_series_for_patch(patch)
            if not series:
                continue
            series.project = new_project
            series.save()

        sys.stdout.write("%06d/%06d\r" % (count, count))
        sys.stdout.write('\ndone\n')
Exemple #30
0
def respondToEmails(username,password,response_subject,automated_response):
    if username == None and password == None:
        mains=mongo.getEntry("fields","main",{})
        if mains.count() > 0:
            username = mains[0]["email"]
            password = mains[0]["password"]
    conn = imaplib.IMAP4_SSL("imap.gmail.com")
    status,user = conn.login(username,password)
    if status == 'OK':
        status,numMessages = conn.select("INBOX")
        if status == 'OK':
            status,messages = conn.search(None,"(UNSEEN)")
            messages = messages[0].split()
            if len(messages) > 0:
                recipients=[]
                parser = HeaderParser()
                for msg_id in messages:
                    status,data = conn.fetch(msg_id,'(BODY[HEADER.FIELDS (FROM)])')
                    response = parser.parsestr(data[0][1])
                    recipients.append(response["From"])
                    conn.store(msg_id,'+FLAGS','\Seen')
                smtpserver = smtplib.SMTP("smtp.gmail.com",587)
                smtpserver.ehlo()
                smtpserver.starttls()
                smtpserver.ehlo()
                smtpserver.login(username, password)
                response = email.MIMEMultipart.MIMEMultipart()
                response["From"]=username
                response["Subject"]=response_subject
                response.attach(email.MIMEText.MIMEText(automated_response,"plain"))
                smtpserver.sendmail(username,recipients,response.as_string())
                smtpserver.quit()
    conn.close()
Exemple #31
0
    def find_emails_by_subject(self, subject, limit=50):
        """
        Searches for Email by Subject.  Returns email's imap message IDs 
        as a list if matching subjects is found.

        Args:
            subject (str) - Subject to search for.

        Kwargs:
            limit (int) - Limit search to X number of matches, default 50

        Returns:
            list - List of Integers representing imap message UIDs.

        """
        # Select inbox to fetch the latest mail on server.
        self._mail.select("inbox")
        
        matches = []
        parser = HeaderParser()
        
        matching_msg_nums = self.__search_email_by_subject(subject)

        for msg_num in matching_msg_nums[-limit:]:
            _, msg_data = self._mail.fetch(msg_num, '(RFC822)')
            raw_msg = msg_data[0][1]
            msg_headers = parser.parsestr(raw_msg, True)
            if msg_headers['subject'] == subject:
                uid = re.search("UID\\D*(\\d+)\\D*", self._mail.fetch(msg_num, 'UID')[1][0]).group(1)
                matches.append(uid)

        return matches
Exemple #32
0
 def check_encoding():
     nonlocal encoding
     if not msgid and msgstr:
         # See whether there is an encoding declaration
         p = HeaderParser()
         charset = p.parsestr(msgstr.decode(encoding)).get_content_charset()
         if charset:
             encoding = charset
Exemple #33
0
 def fetch_header(self, email_id, location='INBOX'):
     status, header = self.imap_conn.select(location)
     header = None
     t, header = self.imap_conn.fetch(email_id, '(BODY[HEADER])')
     header_data = header[0][1].decode('utf-8', 'ignore')
     parser = HeaderParser()
     header = parser.parsestr(header_data)
     return header
Exemple #34
0
 def fetch_header(self, email_id, location='INBOX'):
     status, header = self.imap_conn.select(location)
     header = None
     t, header = self.imap_conn.fetch(email_id, '(BODY[HEADER])')
     header_data = header[0][1].decode('utf-8', 'ignore')
     parser = HeaderParser()
     header = parser.parsestr(header_data)
     return header
def header_parser(filename):
    # parse email headers
    with open(filename, 'r') as msg:
        my_email = msg.read()
        parser = HeaderParser()
        h = parser.parsestr(my_email)
        headers = dict(h.items())
    return (headers)
Exemple #36
0
def fetch_and_store():
    detach_dir = '.'
    m = imaplib.IMAP4_SSL("imap.gmail.com")
    m.login('*****@*****.**',getPassword())
    m.select("inbox")

    resp, items = m.search(None, "(UNSEEN)")
    items = items[0].split()

    for emailid in items:
        resp, data = m.fetch(emailid, "(RFC822)")
        email_body = data[0][1]
        resp2, header = m.fetch(emailid, '(BODY[HEADER.FIELDS (SUBJECT)])')
        subject = header[0][1]
        parser = HeaderParser()
        msg = parser.parsestr(subject)
        subjectline = "".join(re.findall(r'[0-9a-zA-Z\-]', msg["Subject"]))

        mail = email.message_from_string(email_body)
        from email.utils import parseaddr
        fromaddr = parseaddr(mail['from'])[1]
        name = parseaddr(mail['from'])[0]
        temp = m.store(emailid,'+FLAGS', '\\Seen')
        m.expunge()
        sender = checkSender(fromaddr)
        if not parseSubLine(subjectline) or not sender :             #Ifall mailet har fel rubrik, går vi in här
            if not parseSubLine(subjectline) :
                print "Warning: Mail has wrong subject"
                sendEmail(name, fromaddr, subjectline, "2")     #Skickar ett mail till avsändaren om att dess mail haft fel format på rubriken
            elif not sender :
                print "Warning: Address not in senders file"
                sendEmail(name, fromaddr, subjectline, "1")     #Skickar ett mail till avsändaren om adressen inte finns i "sender.txt"-listan
        else:
            if not os.path.exists(subjectline):
                os.makedirs(subjectline)

            if mail.get_content_maintype() != 'multipart':
                continue

            filenamelist = []
            for part in mail.walk():

                if part.get_content_maintype() == 'multipart':
                    continue
                if part.get('Content-Disposition') is None:
                    continue

                filename = "".join(re.findall(r'[.0-9a-zA-Z\-]',part.get_filename()))
                att_path = os.path.join(detach_dir + "/" + subjectline, filename)
                filenamelist.append(filename)
                if not os.path.isfile(att_path) :
                    fp = open(att_path, 'wb')
                    fp.write(part.get_payload(decode=True))
                    fp.close()
            course = sender[0]
            name = sender[1]
            dest = "www/uppladdat/" + course + "/{0}.pdf".format(setFileName(sender,subjectline))
            convertStoredToPdf(subjectline, dest)
Exemple #37
0
 def set_headers(self, part):
     parser = HeaderParser()
     # get all headers and store them in dict with lower key
     headers = {
         key.lower().strip('.'): unicode(make_header(decode_header(value)))
         for key, value in parser.parsestr(part).items()
     }
     self.headers = headers
     return
Exemple #38
0
def parseHeadersResponse(headersResponse):
    headersStr = headersResponse.decode('utf-8')
    parser = HeaderParser()
    headersDict = parser.parsestr(headersStr)
    decodedHeadersDict = {}
    for k, v in headersDict.items():
        decodedHeadersDict[k.lower()] = str(make_header(decode_header(v)))

    return (decodedHeadersDict)
Exemple #39
0
def check_for_new_mail():

    new_status = open('lastmail.txt', 'r')
    stat = new_status.read()
    new_status.close()

    inbox = conn.select('Inbox')
    data = conn.fetch(int(inbox[1][0]), '(BODY[HEADER])')
    raw_header = data[1][0][1]
    parser = HeaderParser()
    header = parser.parsestr(raw_header)

    if stat == '':
        update_new_stat = open('lastmail.txt', 'w')
        pickle.dump(header['From'],
                    update_new_stat)  #Sender's name and mail-id
        pickle.dump(header['Subject'], update_new_stat)  #Subject of mail
        pickle.dump(header['Received'].split()[7:10],
                    update_new_stat)  #Received Date ['day','month','year']
        pickle.dump(header['Received'].split()[10],
                    update_new_stat)  #Received Time
        update_new_stat.close()
        last_sender = header['From']
        last_subject = header['Subject']
        last_date = header['Received'].split()[7:10]
        last_time = header['Received'].split()[10]
    else:
        last_mail = open('lastmail.txt', 'r')

        last_sender = pickle.load(last_mail)
        last_subject = pickle.load(last_mail)
        last_date = pickle.load(last_mail)
        last_time = pickle.load(last_mail)

    current_sender = header['From']
    current_subject = header['Subject']
    current_date = header['Received'].split()[7:10]
    current_time = header['Received'].split()[10]
    val1 = current_sender == last_sender
    val2 = current_subject == last_subject
    val3 = current_date == last_date
    val4 = current_time == last_time

    if current_sender == last_sender:
        if current_subject == last_subject:
            if current_date == last_date:
                if current_time == last_time:
                    pass
                else:
                    preamp(header)
            else:
                preamp(header)
        else:
            preamp(header)
    else:
        preamp(header)
Exemple #40
0
 def fetch_and_parse(self, ids, query):
     data = self.fetch(ids, query)
     parser = HeaderParser()
     emails = []
     
     for email in data:
         if len(email) < 2: continue
         emails.append(parser.parsestr(email[1])  )
         
     return emails
Exemple #41
0
    def fetch_and_parse(self, ids, query):
        data = self.fetch(ids, query)
        parser = HeaderParser()
        emails = []

        for email in data:
            if len(email) < 2: continue
            emails.append(parser.parsestr(email[1]))

        return emails
Exemple #42
0
    def delete_draft(self, inbox_uid):
        """
        Move the message from the "Drafts" folder and into the "Trash" folder.

        Parameters
        ----------
        inbox_uid : str
            The public_id of the draft we want to delete on the remote,
            which is its X-INBOX-ID header too.

        Notes
        -----
        Need the public_id == inbox_uid since that is the only unique
        identifier for the message that both we and the remote know.

        """
        assert inbox_uid

        criteria = ['DRAFT', 'NOT DELETED']
        all_draft_uids = self.conn.search(criteria)
        # It would be nice to just search by X-INBOX-ID header too, but some
        # backends don't support that. So fetch the header for each draft and
        # see if we can find one that matches.
        # TODO(emfree): are there other ways we can narrow the result set a
        # priori (by subject or date, etc.)
        matching_draft_headers = self.conn.fetch(all_draft_uids,
                                                 ['BODY.PEEK[HEADER]'])
        for uid, response in matching_draft_headers.iteritems():
            headers = response['BODY[HEADER]']
            parser = HeaderParser()
            x_inbox_id = parser.parsestr(headers).get('X-Inbox-Id')
            if x_inbox_id == inbox_uid:
                # TODO: do we need this part?
                # Remove IMAP `Draft` label
                self.conn.remove_flags([uid], ['\Draft'])

                self.conn.delete_messages([uid])
                self.conn.expunge()

                # Delete from `Trash`
                # Needed because otherwise deleting a draft that was sent
                # results in it synced twice - once via the Trash folder and
                # once via the Sent folder.
                self.conn.select_folder(self.folder_names()['trash'])

                all_trash_uids = self.conn.search()
                all_trash_headers = self.conn.fetch(all_trash_uids,
                                                    ['BODY.PEEK[HEADER]'])
                for u, r in all_trash_headers.iteritems():
                    x_inbox_header = HeaderParser().parsestr(
                        r['BODY[HEADER]']).get('X-Inbox-Id')
                    if x_inbox_header == inbox_uid:
                        self.conn.delete_messages([u])
                        self.conn.expunge()
                        return
Exemple #43
0
    def delete_draft(self, inbox_uid):
        """
        Move the message from the "Drafts" folder and into the "Trash" folder.

        Parameters
        ----------
        inbox_uid : str
            The public_id of the draft we want to delete on the remote,
            which is its X-INBOX-ID header too.

        Notes
        -----
        Need the public_id == inbox_uid since that is the only unique
        identifier for the message that both we and the remote know.

        """
        assert inbox_uid

        criteria = ['DRAFT', 'NOT DELETED']
        all_draft_uids = self.conn.search(criteria)
        # It would be nice to just search by X-INBOX-ID header too, but some
        # backends don't support that. So fetch the header for each draft and
        # see if we can find one that matches.
        # TODO(emfree): are there other ways we can narrow the result set a
        # priori (by subject or date, etc.)
        matching_draft_headers = self.conn.fetch(
            all_draft_uids, ['BODY.PEEK[HEADER]'])
        for uid, response in matching_draft_headers.iteritems():
            headers = response['BODY[HEADER]']
            parser = HeaderParser()
            x_inbox_id = parser.parsestr(headers).get('X-Inbox-Id')
            if x_inbox_id == inbox_uid:
                # TODO: do we need this part?
                # Remove IMAP `Draft` label
                self.conn.remove_flags([uid], ['\Draft'])

                self.conn.delete_messages([uid])
                self.conn.expunge()

                # Delete from `Trash`
                # Needed because otherwise deleting a draft that was sent
                # results in it synced twice - once via the Trash folder and
                # once via the Sent folder.
                self.conn.select_folder(self.folder_names()['trash'])

                all_trash_uids = self.conn.search()
                all_trash_headers = self.conn.fetch(all_trash_uids,
                                                    ['BODY.PEEK[HEADER]'])
                for u, r in all_trash_headers.iteritems():
                    x_inbox_header = HeaderParser().parsestr(
                        r['BODY[HEADER]']).get('X-Inbox-Id')
                    if x_inbox_header == inbox_uid:
                        self.conn.delete_messages([u])
                        self.conn.expunge()
                        return
Exemple #44
0
 def parse_request(self, request):
     status = request.splitlines()[0]
     no_status_response = '\n'.join(request.splitlines()[1:])
     
     header_parser = HeaderParser()
     
     mime_message = header_parser.parsestr(no_status_response)
     mime_headers = dict(mime_message.items())
     mime_body = mime_message.get_payload()
     
     return status, mime_headers, mime_body      
Exemple #45
0
 def get_emails(self, count):
     emails = []
     for item in self.latest_n_emails(self, count):
         data = self.mail.fetch(item, '(BODY[HEADER])')
         header_data = data[1][0][1]
         header_data = header_data.decode("utf-8")
         parser = HeaderParser()
         message = parser.parsestr(header_data)
         decoded_header = make_header(decode_header(message['subject']))
         emails.append(str(decoded_header))
     return emails
Exemple #46
0
 def get_subject(self, msg_id=None):
     data = self.mail_connection.uid('FETCH',
         msg_id, '(BODY[HEADER.FIELDS (SUBJECT FROM)])')
     header_data = data[1][0][1]
     parser = HeaderParser()
     header = parser.parsestr(header_data)
     subject = header['Subject']
     text, encoding = email.Header.decode_header(subject)[0]
     if encoding is None:
         return text
     else:
         return text.decode(encoding)
Exemple #47
0
    def content_type(self) -> Union[str, None]:
        """Return "Content-Type" header; strip optional parameters, e.g. "charset"."""
        content_type = self.header('Content-Type')

        if content_type is None:
            return None

        # Parse "type/subtype" out of "type/subtype; param=value; ..."
        header_parser = EmailHeaderParser()
        message = header_parser.parsestr("Content-Type: %s" % content_type)
        content_type = message.get_content_type()
        return content_type
Exemple #48
0
def parse(message):
    msgobj = EmailParser().parsestr(message)
    header_parser = HeaderParser()

    headers = []
    headers_dict = header_parser.parsestr(message)
    for key in headers_dict.keys():
        headers += ['{}: {}'.format(key, headers_dict[key])]
    content = Scrubber(msgobj).scrub()[0]
    return {
        'body': content,
        'headers': '\n'.join(headers),
    }
Exemple #49
0
def build_references_from_mail(mail):
    replies = ""
    references = ""
    parser = HeaderParser()
    headers = parser.parsestr(mail.as_string())
    for h in headers.items():
        # Patch emails created with send-pull-Request script
        # may contain two set of references, so use only the oldest
        if h[0] == 'In-Reply-To':
            replies = "%s" % h[1]
        if h[0] == 'References':
            references = "%s" % h[1]
    return build_references_from_headers(replies, references)
Exemple #50
0
    def get_headers(self, Email):
        headers = None
        try:
            parser      = HeaderParser()
            raw_headers = parser.parsestr(Email.as_string())
            #headers     = dict()
            headers     = ""
            for key,value in raw_headers.items():
#                headers[str(key).lower()] = str(value).lower()
                headers += str(key).lower()+": "+str(value).lower()+"|"
        except Exception as e:
            print "Emailtoolkit: headers error",e
        finally:
            return headers
Exemple #51
0
def all_mail():
  for folder in all_folders():
    mail.select(folder)
    result, data = mail.search(None,'(NOT BODY "BEGIN PGP MESSAGE")')
    ids = data[0]
    id_list = ids.split()
    for imap_id in id_list:
      b_r,b_d = mail.fetch(imap_id,'(RFC822)')    
      h_r,h_d = mail.fetch(imap_id,'(BODY[HEADER.FIELDS (SUBJECT FROM)])')
      mail.store(imap_id, '+FLAGS', '\\Deleted')
      mail.expunge()
      parser = HeaderParser()
      msg = parser.parsestr(h_d[0][1])
      yield (dict(msg),b_d)
Exemple #52
0
def send_message(message, payload):
    """ Sends the AS2 message to the partner """
    try:
        hparser = HeaderParser()
        message_header = hparser.parsestr(message.headers)
        auth = None
        if message.partner.http_auth:
            auth = (message.partner.http_auth_user, message.partner.http_auth_pass)
        verify = True
        if message.partner.https_ca_cert:
            verify = message.partner.https_ca_cert.path
        try:
            response = requests.post(
                message.partner.target_url, auth=auth, verify=verify, headers=dict(message_header.items()), data=payload
            )
            response.raise_for_status()
        except Exception, e:
            ### Send mail here
            as2utils.senderrorreport(
                message,
                _(
                    u'Failure during transmission of message to partner with error "%s".\n\nTo retry transmission run the management command "retryfailedas2comms".'
                    % e
                ),
            )
            message.status = "R"
            models.Log.objects.create(message=message, status="E", text=_(u"Message send failed with error %s" % e))
            return
        models.Log.objects.create(message=message, status="S", text=_(u"AS2 message successfully sent to partner"))
        if message.partner.mdn:
            if message.partner.mdn_mode == "ASYNC":
                models.Log.objects.create(
                    message=message, status="S", text=_(u"Requested ASYNC MDN from partner, waiting for it ........")
                )
                message.status = "P"
                return
            mdnContent = ""
            for key in response.headers:
                mdnContent = mdnContent + "%s: %s\n" % (key, response.headers[key])
            mdnContent = mdnContent + "\n" + response.content
            models.Log.objects.create(message=message, status="S", text=_(u"Synchronous mdn received from partner"))
            pyas2init.logger.debug("Synchronus MDN from message %s received:\n%s" % (message.message_id, mdnContent))
            save_mdn(message, mdnContent)
        else:
            message.status = "S"
            models.Log.objects.create(
                message=message, status="S", text=_(u"No MDN needed, File Transferred successfully to the partner")
            )
            run_postsend(message)
Exemple #53
0
  def getMessages(self):
    self.conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
    self.conn.login(self.account, self.passwd)
    self.conn.select()
    msgIds = [int(x) for x in self.conn.search(None, 'UnSeen')[1][0].split()]
 
    for id in msgIds:
      data = self.conn.fetch(id, '(BODY[HEADER])')
      header_data = data[1][0][1]

      parser = HeaderParser()
      msg = parser.parsestr(header_data)
      if self.tag in msg['Subject'] and \
        self.account[:self.account.find("@")] not in msg["From"]:
        self.mids.append(id)
Exemple #54
0
    def get_date(self, uid):
        parser = HeaderParser()
        
        try:
            typ, header = self.conn.uid('fetch', uid, '(BODY[HEADER.FIELDS (DATE SUBJECT)])')
            headerdic = parser.parsestr(header[0][1])
            pz = email.utils.parsedate_tz(headerdic["Date"])
            stamp = email.utils.mktime_tz(pz)
            date = imaplib.Time2Internaldate(stamp)

            return date

        except:
            self.logger.error("Message date can't be found in the message")
            return None
Exemple #55
0
def imap_access(username,password):

    imap_server=imaplib.IMAP4_SSL(IMAP_SERVER)
    imap_server.login(username,password)
    
    # select INBOX
    imap_server.select('INBOX')
    
    # Search for all new emails in INBOX
    status, email_ids = imap_server.search(None, '(UNSEEN)')
    
    # placeholders
    senders=[]
    code_languages=[]
    codes=[]

    # Retrieve Sender, Subject, code for each new request
    if email_ids != ['']:
        for e_id in email_ids[0].split():

            # Extract sender, code language
            response,data = imap_server.fetch(e_id, '(RFC822)')

            # parse response to extract sender,code_language,code
            header_data = data[0][1]
            parser=HeaderParser()
            msg = parser.parsestr(header_data)

            sender = msg['From']
            code_language= msg['Subject']


            # extract code from the body

            # assuming that the message is single part
            # the code resides in the body of the message

            code =  email.message_from_string(data[0][1])
            code = code.get_payload()
            
            senders.append(sender)
            code_languages.append(code_language)
            codes.append(code)

    # logout
    imap_server.logout()

    return senders,code_languages,codes
Exemple #56
0
    def __init__(self, folder, num, msg):
        self.num = num
        self.folder = folder
        self.msg = email.message_from_string(msg)

        parser = HeaderParser()
        hdr = parser.parsestr(msg)

        self.subject = MailboxMessage.decode_header(hdr['Subject'])
        self.sender = MailboxMessage.decode_header(hdr['From'])

        # Now convert to local date-time
        self.raw_date = hdr['Date']
        date_tuple = email.utils.parsedate_tz(self.raw_date)
        if date_tuple:
            self.local_date = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
 def _fetch_emails_by_ids(self, email_ids, mailbox=None):
     if mailbox and mailbox != self.selected_mailbox:
         self._select_mailbox(mailbox)
     if not self.selected_mailbox:
         raise ValueError('No Mailbox selected')
     if isinstance(email_ids, (list, tuple)):
         email_ids = ','.join(email_ids)
     self.log('fetch %s' % email_ids)
     ret, data = self.connection.fetch(email_ids, DEFAULT_FETCH_PARTS)
     self.log(ret)
     data = (d for d in data if isinstance(d, tuple))
     parser = HeaderParser()
     data = (
         (self.__parse_fetch_response(status), parser.parsestr(header))
         for status, header in data
     )
     return data
    def parse(self):

        try:
            unread = self._imap.uid('search', None,'UnSeen')[1][0].split()

            #For each unread email, fetch header
            for email in unread:
                header_raw = self._imap.uid('fetch', email, '(BODY[HEADER])')[1][0][1]
                parser = HeaderParser()
                header = parser.parsestr(header_raw)
                if header['Subject'].find('Notification of payment received') >= 0 and header['Sender']=='*****@*****.**':
                    body = self._imap.uid('fetch', email, '(BODY[TEXT])')[1][0][1]
                    self._parse_pool.put((0, str(unquote(body.replace('=', '%')))))
                elif header['Subject'].find('new_plugin_purchase_from_toolfarm.com') >= 0 and header['From'].find('@toolfarm.com') >=0:
                    body = self._imap.uid('fetch', email, '(BODY[TEXT])')[1][0][1]
                    self._parse_pool.put((1, str(body)))
        except Exception, e:
            print e
Exemple #59
0
def subjects_inbox():
    """
    get all subject prefix in 'Inbox' of mail @163.com
    :return:
    """
    # mail subjects
    subjects = []

    M = imaplib.IMAP4_SSL(receiver["IMAP"], 993)
    # M.debug = 7
    M.login(receiver["Email"], receiver["Password"])

    # mail folders
    t = M.list()
    # print(t)

    mb = "INBOX"
    rv, data = M.select(mailbox=M._quote(mb))
    if rv == "OK":
        rv, data = M.uid("search", None, "ALL")
        if rv != "OK":
            print("there is no message in {mb}".format(mb=mb))
            return
        uids = data[0]
        for uid in uids.split():
            rv, _data = M.uid("fetch", uid, "(BODY.PEEK[HEADER])")
            if rv != "OK":
                # Fetch volume limit exceed.
                # Please try next day
                print("ERROR getting message {uid}: {rv} {_data}".format(uid=str(uid), rv=rv, _data=_data))
                continue
            header_data = _data[0][1].decode("utf-8")
            parser = HeaderParser()
            msg = parser.parsestr(header_data)
            hdr = email.header.make_header(email.header.decode_header(msg["Subject"]))
            _subject = str(hdr)
            print(("{mb}: " + _subject).format(mb=mb).encode())
            subjects.append(_subject)
        M.close()
    else:
        print("ERROR: unable to open {mb}. ".format(mb=mb) + rv)
    M.logout()

    return subjects