def _extract_filename(self, filename):
    result = filename

    if decode_header(filename)[0][1] is not None:
      result = decode_header(filename)[0][0].decode(decode_header(filename)[0][1])

    return result
Пример #2
0
def _decode_header(header):
    """Decode RFC-2047-encoded headers to Unicode strings

    >>> from email.header import Header
    >>> _decode_header('abc')
    'abc'
    >>> _decode_header('=?utf-8?b?zpbOtc+Nz4I=?= <*****@*****.**>')
    'Ζεύς <*****@*****.**>'
    >>> _decode_header(Header('Ζεύς <*****@*****.**>', 'utf-8'))
    'Ζεύς <*****@*****.**>'
    """
    if isinstance(header, _Header):
        return str(header)
    chunks = []
    _LOG.critical(_email_header.decode_header(header))
    for chunk,charset in _email_header.decode_header(header):
        if charset is None:
            if isinstance(chunk, bytes):
                chunk = str(chunk, 'ascii')
            chunks.append(chunk)
        else:
            chunks.append(str(chunk, charset))
    if _sys.version_info < (3, 3):  # Python 3.2 and older
        return ' '.join(chunks)  # http://bugs.python.org/issue1079
    return ''.join(chunks)
Пример #3
0
def save_attachments(mid,mb):
    
    msg = mb.get_message(mid)
    #print msg 
    if msg.is_multipart():
        #print msg
        for part in msg.get_payload():
            if part.get_content_type() != 'application/pdf':
                continue
            file_name = part.get_filename()
            sender_name = msg['from'].split()[0]
            #print file_name

            try:
                encoding = decode_header(file_name)
            except UnicodeEncodeError:
                file_name = string_util.strip_diacriticals(file_name)
                encoding = decode_header(file_name)
                #print "fail"
            bytes = encoding[0][0]
            enc = encoding[0][1]
            #print encoding[0]
            if(enc==None):
                enc = 'utf-8'
            f = bytes.decode(enc)
            #notify('Saving' % part.get_filename())
            directory = creat_userDir(prefs['dir'],sender_name)
            with open(directory +'/'+f, 'wb') as f:
                f.write(part.get_payload(decode=True))
Пример #4
0
    def _parseMessage(self, fp):
        """
        Extract fromaddr and toaddrs from the X-Actually-{To,From} headers.
        Returns message string which has those headers stripped.
        """
        parser = Parser()
        message = parser.parse(fp)

        fromaddr = message['X-Actually-From']
        if fromaddr is not None:
            decoded_fromaddr = header.decode_header(fromaddr)
            assert len(decoded_fromaddr) == 1, 'From header has multiple parts.'
            encoded_fromaddr, charset = decoded_fromaddr[0]
            if charset is not None:
                fromaddr = encoded_fromaddr.decode(charset)
        else:
            fromaddr = ''
        del message['X-Actually-From']

        toaddrs = message['X-Actually-To']
        if toaddrs is not None:
            decoded_toaddrs = header.decode_header(toaddrs)
            assert len(decoded_toaddrs) == 1, 'To header has multiple parts.'
            encoded_toaddrs, charset = decoded_toaddrs[0]
            if charset is not None:
                toaddrs = encoded_toaddrs.decode(charset)
            toaddrs = tuple(a.strip() for a in toaddrs.split(','))
        else:
            toaddrs = ()
        del message['X-Actually-To']

        return fromaddr, toaddrs, message
Пример #5
0
def test():
    print '********************************************************************************'
    print '* Performing checks'
    print '********************************************************************************'
    StartTime = datetime.datetime(2017,8,9)
    for FutureDays in range(2) :
        for FutureHours in range(26) :
            print "-------------------- Testing iteration --------------------"
            # Current date, for testing
            DeltaTime = datetime.timedelta(days=FutureDays,hours=FutureHours)
            CurrentDate = StartTime + DeltaTime

            #SQL Dataset:
            SQLCon = sqlite3.connect('../Database/Testing/SumSemData20170808.db')
        
            ## Getting announcements and check ins
            SQLCur = SQLCon.cursor()
            EmailsToSend = MakeAllMessages(CurrentDate,SQLCur)
            SQLCur.close()

            # Making sure they are sent to the test address:
            for Email in EmailsToSend:
                Email['msg'].replace_header('To',Header(u'*****@*****.**','utf-8'))  


            ## Sending emails
            # Ensuring emails are sent to the testing address
            if len(EmailsToSend) > 0 :
                for Email in EmailsToSend:
                    assert decode_header(Email['msg']['To'])[0][0] == '*****@*****.**' 
                    assert len(decode_header(Email['msg']['To'])) == 1
            SendEmails(EmailsToSend,SQLCon)
Пример #6
0
  def __init__(self, msg, date=None):
    self.msg = msg
    self.subject, self.subject_encoding = decode_header(msg["subject"])[0]

    if self.subject_encoding:
      self.subject = unicode(self.subject, self.subject_encoding)
      self.subject = self.subject.encode("ascii", "ignore") # to be nice to file systems

    # decode email address and sender

    nameRes = self.getNameProg.search(msg["from"])

    if nameRes:
      self.fromName, self.fromName_encoding = decode_header(nameRes.group(1))[0]

      if self.fromName_encoding:
        self.fromName = unicode(self.fromName, self.fromName_encoding)
        self.fromName = self.fromName.encode("ascii", "ignore")

      self.fromAddr = nameRes.group(2)
    else:
      self.fromName = None 
      self.fromAddr = self.emailProg.search(msg["from"]).group(0)

    if date:
      self.timestamp = float(date.strftime("%s.%f"))
      self.datestr = date.strftime("%Y-%m-%d %H:%M:%S")
    else:
      # omg deprecate this
      self.initDate()

    self.toAddr = self.emailProg.search(msg["to"]).group(0)

    self.hasGif = None
    self.lines = None
Пример #7
0
def mail (conf) :
	mail_conf = conf["mail"]
	parser = Parser()
	if mail_conf["ssl"] :
		obj = imaplib.IMAP4_SSL(mail_conf["server"], int(mail_conf["port"]))
	else :
		obj = imaplib.IMAP4(mail_conf["server"], int(mail_conf["port"]))
	
	obj.login(mail_conf["username"], mail_conf["passwd"])
	obj.select(readonly=True)
	data = obj.search(None, 'UnSeen')
	header = ""
	for num in data[1][0].split() :
		msg = parser.parsestr(obj.fetch(num, '(BODY[HEADER.FIELDS (SUBJECT FROM)])')[1][0][1])
		sender_tuple =  re.sub(r'<(.+)>', '', msg["from"])
		sender = ""
		for i in decode_header(sender_tuple) :
			if i[1] is not None :
				sender += i[0].decode(i[1])
			else : 
				sender += i[0]
		subject_tuple =  msg["subject"]
		subject = ""
		for j in decode_header(subject_tuple) :
			if j[1] is not None :
				subject += j[0].decode(i[1])
			else : 
				subject += j[0]
		header += "Message de " + sender + " : " + subject.replace("RE:", "").replace("Re:", "").replace("Fwd:", "").replace("FWD:", "").replace("Tr:", "").replace("TR:", "") + ". "
	return len(data[1][0].split()), header
Пример #8
0
 def get_imap(self, server, port, user, password):
     messages = []
     
     try:
         m = imaplib.IMAP4_SSL(server, port)
         m.login(user, password)
         m.select('inbox', readonly=True)
     
         result, data = m.uid('search', None, "(UNSEEN)")
         #print (len(data))
         if result == 'OK':
             for latemail in data[0].split():
                 result, data = m.uid('fetch', latemail, '(RFC822)')
                 if result == 'OK':
                     email_message = email.message_from_bytes(data[0][1])
                     dechead, enc = decode_header(email_message['Subject'])[0]
                     if isinstance(dechead, bytes):
                         dechead = dechead.decode(encoding='UTF-8')
                     #print(dechead)
                     decsend, enc = decode_header(email_message['From'])[0]
                     if isinstance(decsend, bytes):
                         decsend = decsend.decode(encoding='UTF-8')
                     #print(decsend)
                     #print(type(email_message['Subject']))
                     messages.append( (dechead, decsend) )
     
         m.close()
         m.logout()
         
     except:
         messages.append( ("Error accessing " + server, "Check account settings") )
     
     return messages
Пример #9
0
 def getMessage(self, uid):
     if(not self.server.loggedIn):
         self.server.login()
     self.server.imap_server.select(self.mailbox)
     
     status, data = self.server.imap_server.uid('fetch',uid, 'RFC822')
     messagePlainText = ''
     messageHTML = ''
     for response_part in data:
         if isinstance(response_part, tuple):
             msg = email.message_from_string(response_part[1])
             for part in msg.walk():
                 if str(part.get_content_type()) == 'text/plain':
                     messagePlainText = messagePlainText + str(part.get_payload(decode=
                                                                                True))
                 if str(part.get_content_type()) == 'text/html':
                     messageHTML = messageHTML + str(part.get_payload())
             
     #create new message object
     message = gmail_message.gmail_message()
     
     if(messageHTML != '' ):
         message.Body = messageHTML
     else:
         message.Body = messagePlainText
     if('Subject' in msg):
         message.Subject = decode_header(msg['Subject'])[0][0]
     message.From = decode_header(msg['From'])[0][0]
     
     message.uid = uid
     message.mailbox = self.mailbox
     message.date = decode_header(msg['Date'])[0][0]
     
     return message
Пример #10
0
 def getMoreConversations(self, serial, count):
     with self._store.writeLock as transaction:
         self.manager.updated.emit(serial, State.InProgress, "step 1")
         thrids = store.ConversationsDefaultDict(self._store.snapshot.conversations)
         while len(thrids.added) < count:
             n = count - len(thrids.added)
             for item in self._fetch_uids(n):
                 thrids[item['X-GM-THRID']].message_ids.append(item['UID'])
                 thrids[item['X-GM-THRID']].message_ids.sort()
                 n -= 1
             if n > 0:
                 break
         self.manager.updated.emit(serial, State.InProgress, "step 2")
         # TODO: sort thrids.added by date
         for i, thrid in enumerate(thrids.added):
             uids = self._search_thrid(thrid)
             for uid, raw_headers in self._fetch_headers((uids[0],uids[-1]), ('SUBJECT','FROM','DATE')):
                 headers = self.email_parser.parsestr(raw_headers)
                 message = transaction.messages[uid]
                 message.subject = unicode(make_header(decode_header(headers['subject']))).replace('\r\n ',' ')
                 message.sender = unicode(make_header(decode_header(headers['from'])))
                 message.timestamp = mktime_tz(parsedate_tz(headers['date']))
             transaction.thrids[thrid].message_ids = uids
             transaction.thrids[thrid].subject = transaction.messages[uids[0]].subject
             transaction.thrids[thrid].date = transaction.messages[uids[-1]].timestamp
             self.manager.progress.emit(serial, float(i+1) / len(thrids.added))
             transaction.commit(block=True)
         self.manager.updated.emit(serial, State.Successful, "done")
Пример #11
0
def checknew(imap, messages):
    for i in messages:
        result, data = imap.fetch(str(i), '(BODY.PEEK[HEADER])')
        for response in data:
            if isinstance(response, tuple):
                header_data = response[1]
                try: # email only works on strings, not bytes
                    header_data = header_data.decode('utf-8')
                except:
                    pass
                header = parser.parsestr(header_data)
                subject = decode_header(header['Subject'])[0][0]
                sender = decode_header(header['From'])
                msg_id = decode_header(header['Message-Id'])[0][0]
                msg_id = msg_id.encode('utf-8')
                msg_id_hash = sha512(msg_id).hexdigest()
                try:
                    sender_email = sender[0][0].split('<')[1].split('>')[0]
                except:
                    sender_email = sender[0][0] # what'ev
                if not check_notified(msg_id_hash):
                    subprocess.call(["notify-send", sender_email, subject])
                    write_hash(msg_id_hash)
                else:
                    return
Пример #12
0
    def test_0070_render_email_unicode(self):
        '''
        Render email with unicode in Subject, From and To.
        '''
        with Transaction().start(DB_NAME, USER, CONTEXT):
            self.setup_defaults()
            app = self.get_app()

            sender = u'Cédric Krier <*****@*****.**>'

            with app.test_request_context('/'):
                email_message = render_email(
                    sender,
                    u'*****@*****.**',
                    u'Dummy subject øf email',
                    text_template='from-local.html',
                    cc=u'*****@*****.**'
                )
                self.assertEqual(
                    decode_header(email_message['From'])[0],
                    (sender.encode('UTF-8'), 'utf-8')
                )
                self.assertEqual(
                    decode_header(email_message['Subject'])[0],
                    (u'Dummy subject øf email'.encode('UTF-8'), 'utf-8')
                )
                self.assertEqual(
                    decode_header(email_message['To'])[0],
                    (u'*****@*****.**'.encode('UTF-8'), None)
                )
Пример #13
0
 def process(self,mailbox):
     self.mailbox = mailbox
     self.messages = list()
     
     if(not self.server.loggedIn):
         self.server.login()
     
     result, message = self.server.imap_server.select(mailbox,readonly=1)
     if result != 'OK':
         raise Exception, message
     typ, data = self.server.imap_server.search(None, '(UNDELETED)')
     fetch_list = string.split(data[0])[-8:]# limit to N most recent messages in mailbox, this is where pagination should be implemented
     fetch_list = ','.join(fetch_list)
     
     if(fetch_list):
         f = self.server.imap_server.fetch(fetch_list, '(UID FLAGS BODY.PEEK[HEADER.FIELDS (FROM SUBJECT DATE)])')
         for fm in f[1]:
             if(len(fm)>1):
                 metadata = self.parseMetadata(fm[0]) #metadata is contained 
                 headers = self.parseHeaders(fm[1])
                 message = gmail_message.gmail_message()
                 message.id = metadata['id']
                 message.uid = metadata['uid']
                 message.flags = metadata['flags']
                 message.mailbox = mailbox   #UID depends on mailbox location so,
                                                             #we need to know which owns the message
                 message.date = decode_header(headers['Date'])[0][0]
                 message.From = decode_header(headers['From'])[0][0]
                 if( 'Subject' in headers ):                        
                     message.Subject = decode_header(headers['Subject'])[0][0]
                 self.messages.append(message)
     self.messages.reverse()
Пример #14
0
def reademail(user, pwd) :
	pop_conn = poplib.POP3(host, timeout=5)
	pop_conn.user(user)  
	pop_conn.pass_(pwd)  
	 
	#Get messages from server:
	topn = 3 
	mailcount = len(pop_conn.list()[1])
	startindex = 1
	if mailcount > topn :
		startindex = mailcount - topn + 1
	messages = [pop_conn.top(i, 0) for i in range(startindex, mailcount+1)]  
	  
	# Concat message pieces:  
	messages = ["\n".join(mssg[1]) for mssg in messages]  
	  
	#Parse message intom an email object:  
	messages = [parser.Parser().parsestr(mssg) for mssg in messages]  
	for message in messages:  
		subject = header.decode_header(message['Subject'])[0][0];
		fromstr = header.decode_header(message['From']);
		date = header.decode_header(message['Date']);
		print "Subject:",subject, "From", fromstr, "Date", date, user+":"+pwd
		#print "From", fromstr, "Date", date, user+":"+pwd
		#if subject.find("預訂申請") >= 0 :
		#	print "GOTIT:", date, user+":"+pwd 

	'''
	# delete all emails
	for i in range(2, mailcount+1) :
		pop_conn.dele(i)
	'''

	pop_conn.quit()  
Пример #15
0
    def test_send_email_html(self, smtp):
        action = std.SendEmailAction(
            from_addr=self.from_addr,
            to_addrs=self.to_addrs,
            smtp_server=self.smtp_server,
            smtp_password=None,
            subject=self.subject,
            body=self.body,
            html_body=self.html_body
        )

        action.run(self.ctx)

        smtp.assert_called_once_with(self.smtp_server)

        sendmail = smtp.return_value.sendmail

        self.assertTrue(sendmail.called, "should call sendmail")
        self.assertEqual(
            self.from_addr, sendmail.call_args[1]['from_addr'])
        self.assertEqual(
            self.to_addrs, sendmail.call_args[1]['to_addrs'])

        message = parser.Parser().parsestr(sendmail.call_args[1]['msg'])

        self.assertEqual(self.from_addr, message['from'])
        self.assertEqual(self.to_addrs_str, message['to'])
        if six.PY3:
            self.assertEqual(
                self.subject,
                decode_header(message['subject'])[0][0].decode('utf-8')
            )
        else:
            self.assertEqual(
                self.subject.decode('utf-8'),
                decode_header(message['subject'])[0][0].decode('utf-8')
            )
        body_payload = message.get_payload(0).get_payload()
        if six.PY3:
            self.assertEqual(
                self.body,
                base64.b64decode(body_payload).decode('utf-8')
            )
        else:
            self.assertEqual(
                self.body.decode('utf-8'),
                base64.b64decode(body_payload).decode('utf-8')
            )
        html_body_payload = message.get_payload(1).get_payload()
        if six.PY3:
            self.assertEqual(
                self.html_body,
                base64.b64decode(html_body_payload).decode('utf-8')
            )
        else:
            self.assertEqual(
                self.html_body.decode('utf-8'),
                base64.b64decode(html_body_payload).decode('utf-8')
            )
Пример #16
0
def extract_part_filename(part):
    filename = part.get_filename()
    if filename is not None:
        if decode_header(filename)[0][1] is not None:
            filename = str(decode_header(filename)[0][0]).decode(decode_header(filename)[0][1])
        return filename
    else:
        return None
Пример #17
0
    def test_0005_render_email(self):
        """
        Render email
        """
        Mail = POOL.get('mail.mail')

        with Transaction().start(DB_NAME, USER, CONTEXT):
            # Stubbing ``Mail.jinja_loader_func`` as it needs an actual
            # template from filesystem.
            stub(Mail.jinja_loader_func, template_loader)

            with self.assertRaises(Exception):
                # Try rendering mail without passing text
                # or html template
                Mail.render_email(
                    from_email="*****@*****.**",
                    to='*****@*****.**',
                    subject='Dummy subject of email',
                    cc=u'*****@*****.**'
                )

            email_message = Mail.render_email(
                from_email="*****@*****.**",
                to='*****@*****.**',
                subject='Dummy subject of email',
                text_template='mail/base.html',
                cc=u'*****@*****.**'
            )

            self.assertEqual(
                decode_header(email_message['From'])[0],
                ("*****@*****.**", None)
            )

            self.assertEqual(
                decode_header(email_message['Subject'])[0],
                ('Dummy subject of email', None)
            )

            self.assertFalse(email_message.is_multipart())
            self.assertEqual(
                email_message.get_payload(decode=True),
                "|block 1 from base\n|block 2 from base",
            )

            # Email to can take comma separated email addresses.
            email_message = Mail.render_email(
                from_email="*****@*****.**",
                to='[email protected], [email protected]',
                subject='Dummy subject of email',
                text_template='mail/base.html',
                cc=u'*****@*****.**'
            )
            self.assertEqual(
                decode_header(email_message['To'])[0],
                ('[email protected], [email protected]', None)
            )
Пример #18
0
def getSingleTask():
    global log
    M = getConnection()
    # lock na M
    # TODO: nalezy popatrzec sie na polecenia
    # TODO: nalezy popatrzec sie na liste problemow
    M.select(QUEUE_MAILBOX)
    msg, msgid = None, None
    type, data = M.search(None, 'UNSEEN')
    msgs = data[0].split()
    if len(msgs) > 0:
        msgid = msgs[0]
        type, data = M.fetch(msgid, '(RFC822)')
        M.store(msgid, '+FLAGS', '\Seen')
        for rp in data:
            if isinstance(rp, tuple):
                msg = email.message_from_string(rp[1])
    if msg is None: raise TaskListEmpty()
    
    fromaddr = msg.get('From')
    subject = decode_header(msg.get('Subject'))[0][0].strip().translate(None, '.\\/')
    userid = fromaddr[fromaddr.find('<') + 1:fromaddr.find('>')]
    
    if len(userid) < 5: raise IncorrectUserid()
    if not problemExists(subject):
        if subject == CMD_TASKLIST: pass
        elif subject.startswith(CMD_TASKLIST): serveTaskDescriptionCmd(userid, subject)
        elif subject == CMD_RESULTS: serveResultsCmd(userid, subject)
        elif subject == CMD_RANKING: serveRankingCmd()
        elif subject == CMD_ADDTASK: serveAddTaskCmd()
        else:
            log.error("Could not found instruction for subject '%s'" % subject)
            raise ProblemNotFoundError()
        log.debug("Remove message %s" % str(msgid))
        M.select(QUEUE_MAILBOX)
        M.store(msgid, '+FLAGS', '\Deleted')
        M.expunge()
        raise CommandProcessed()
    
    log.debug("New message retrieved (Subj '%s', From '%s')", subject, userid)
    # TODO: sprawdzic jeszcze rozmiar
    attname = None
    att = None
    for part in msg.walk():
        attname = decode_header(part.get_filename())[0][0]
        att = part.get_payload(decode=1)
    if len(att) > FILE_SIZE_LIMIT: raise FileTooLongError()
    suffix = attname[attname.rfind('.'):]
    filename = mktemp(suffix=suffix, dir=TESTING_DIR)
    with open(filename, 'w') as f:
        f.write(att)
    
    task = Task(subject, userid, filename)
    task.msgid = msgid
    log.debug("New task created for problem '%s', user '%s' (file %s)",
              subject, userid, filename)
    return task
Пример #19
0
def showSubject(msg):
    try:
        decode = "utf-8"
        if decode_header(msg["Subject"])[0][1] == "gbk":
            print decode_header(msg["Subject"])[0][0].decode("gbk").encode("utf-8")
        print unicode(decode_header(msg["Subject"])[0][0], decode)
    except:
        print ""
        pass
Пример #20
0
 def retrieveMail(self):
   try:
     if debug:
       print 'checking mail...'
     pop = poplib.POP3_SSL(pop_host, pop_port)
     pop.user(pop_user)
     pop.pass_(pop_pass)
     stat = pop.stat()
     if stat[0] > 0:
       for n in range(stat[0]):
         msgnum = n+1
         response, lines, bytes = pop.top(msgnum, max_lines)
         clean_lines = []
         count = 0
         for i in range(len(lines)-1):
           if lines[count+1].replace('\t', ' ').startswith(' '):
             clean_lines.append(lines[count] + lines[count+1])
           elif not lines[count].startswith(' '):
             clean_lines.append(lines[count])
           count += 1
         subject_line = filter(subject_line_re.match, clean_lines)
         subject_line = subject_line[0].split('Subject: ', 1)[1]
         subject_line, subject_encoding = decode_header(subject_line)[0]
         if subject_encoding is not None:
           subject_line = subject_line.decode(subject_encoding).encode('utf-8')
         message_sender = filter(from_re.match, clean_lines)
         name_provided = re.search(r'From: (.+) <.+>', message_sender[0])
         name_not_provided = re.search(r'From: (.+)@.+', message_sender[0])
         if name_provided:
           if debug:
             print 'name provided'
           sender_safe_name = name_provided.group(1)
         elif name_not_provided:
           if debug:
             print 'name not provided'
           sender_safe_name = name_not_provided.group(1)
         sender_safe_name, sender_encoding = decode_header(sender_safe_name)[0]
         if sender_encoding is not None:
           sender_safe_name = sender_safe_name.decode(sender_encoding).encode('utf-8')
         if re.search('To:.+toolserver-announce@', ' '.join(clean_lines)):
           prefix = ''
         elif re.search('To:.+toolserver-l@', ' '.join(clean_lines)):
           prefix = ''
         else:
           if not debug:
             pop.dele(msgnum)
           continue
         final_mail_line = '%s * %s' % (sender_safe_name.strip('"'),
                                        re.sub(r'\t', ' ', re.sub(r'\s{2,}', ' ', subject_line)))
         self.msg(primary_channel, final_mail_line)
         time.sleep(0.5)
         if not debug:
           pop.dele(msgnum)
     pop.quit()
   except:
     pass
   return
Пример #21
0
 def _subject(self):
     print "decode_header", decode_header(self._mail['subject'])
     charset = decode_header(self._mail['subject'])[0][1]
     if charset:
         return unicode(' '.join([part[0] for 
                        part in decode_header(self._mail['subject'])]),charset)
     else:
         return ' '.join([part[0] for 
                        part in decode_header(self._mail['subject'])])
Пример #22
0
	def poll(self, host, mailbox, user, passwd, room, ssl=True):
		"""Poll an IMAP mailbox"""
		log.info("Polling {0}@{1}".format(user, host))

		if 'seen' not in self.shelf.keys():
			seen = []
		else:
			seen = self.shelf['seen']
		if ssl:
			M = imaplib.IMAP4_SSL(host)
		else:
			M = imaplib.IMAP4(host)

		log.debug("IMAP LOGIN")
		code,message = M.login(user, passwd)
		log.debug("{0}: {1}".format(code, message))

		log.debug("IMAP SELECT: {0}".format(mailbox))
		M.select(mailbox)
		log.debug("{0}: {1}".format(code, message))
		log.debug("IMAP SEARCH")
		if self._highest_uid is None:
			search = '(SENTSINCE {})'.format((datetime.datetime.now() + datetime.timedelta(weeks=-1)).strftime('%d-%b-%Y'))
		else:
			search = '(UID {}:*)'.format(self._highest_uid)
		typ, data = M.search(None, search)
		log.debug("{0}: {1}".format(typ, data))

		for num in data[0].split():
			typ, data = M.fetch(num, '(RFC822.HEADER)')
			raw_mail = data[0][1]
			# raw_message is a bytestring which must be decoded to make it usable
			mail = email.message_from_string(raw_mail.decode("utf-8", "ignore"))
			if mail.get('Message-ID') not in seen:
				log.debug("New message: {0}".format(mail.get('Message-ID')))
				seen.append(mail.get('Message-ID'))

				message = 'New email arrived'
				for hdrname in ['From','To','Cc','Subject']:
					value = mail.get(hdrname) or None
					if value:
						if PY2:
							pairs = decode_header(value)
							hdrvalue = ' '.join([ unicode(t[0], t[1] or 'ASCII') for t in pairs ])
							message += "\n\t{}: {}".format(hdrname, hdrvalue)
						else:
							hi = make_header(decode_header(value))
							message += "\n\t{}: {}".format(hdrname, str(hi))

				self.send(room, message, message_type='groupchat')
			else:
				log.debug("Seen message: {0}".format(mail.get('Message-ID')))
			self._highest_uid = num.decode('ascii')
		M.close()
		M.logout()
		self.shelf['seen'] = seen
		self.shelf.sync()
Пример #23
0
def DisplayMessage(Msg):
        print "------------------------------------------------------------"
        print "Message: "
        print Msg.items()
        print 'Subject: ',decode_header(Msg['Subject'])
        print 'From: ',decode_header(Msg['From'])
        print 'To: ',decode_header(Msg['To'])
        print Msg.get_payload(decode=True)
        print "------------------------------------------------------------"
Пример #24
0
def unpack_msg(input_file, output_dir, cfg):
    target_exts = cfg.targets
    with open(input_file, "rb") as fp:
        msg = email.message_from_file(fp)
    only_input_filename = os.path.split(input_file)[1]
    counter = 0
    for part in msg.walk():
        # multipart/* are just containers
        try:
            if part.get_content_maintype() == "multipart":
                continue
            # Applications should really sanitize the given filename so that an
            # email message can't be used to overwrite important files
            m_filename = part.get_filename()
            filename = m_filename
            if filename:
                if filename.startswith("=?"):
                    decoded = decode_header(filename)
                    filename = decoded[0][0].decode(decoded[0][1].upper())
                else:
                    filename = escape_chars(filename)
                filename = only_input_filename.decode("utf-8") + "_" + filename
                ext = os.path.splitext(filename)[1].lower()
            else:
                ext = mimetypes.guess_extension(part.get_content_type())
                if not ext:
                    # Use a generic bag-of-bits extension
                    ext = ".bin"
                filename = u"%s_part-%03d%s" % (only_input_filename, counter, ext)
            filename = filename.encode(fs_enc)
            if ext in cfg.targets:
                with open(os.path.join(output_dir, filename), "wb") as of:
                    of.write(part.get_payload(decode=True))
            elif ext in arch_exts and not fnmatch(filename, cfg.exclude):
                with TempDir(dir=cfg.tempdir) as temp:
                    archpath = os.path.join(temp, filename)
                    with open(archpath, "wb") as of:
                        of.write(part.get_payload(decode=True))
                    for f in unpack_arch(archpath, temp, cfg):
                        ext = os.path.splitext(f)[1].lower()
                        if ext in cfg.targets and not fnmatch(f, cfg.exclude):
                            path_from = os.path.join(temp, f)
                            path_to = os.path.join(output_dir, filename + "_" + f)
                            shutil.copy(path_from, path_to)

            counter += 1
        except UnicodeDecodeError as e:
            print "oops:"
            print input_file
            raise
            print "encoded: ", type(m_filename), m_filename.encode("string_escape")
            if m_filename and m_filename.startswith("=?"):
                decoded = decode_header(m_filename)
                print "tuple: ", type(decoded), decoded
    if cfg.remove == True:
        os.remove(input_file)
Пример #25
0
def showSubject(msg):
	try:
		subdecode = decode_header(msg["Subject"])[0][1]
		if not subdecode==None:
			print  decode_header(msg["Subject"])[0][0].decode(decode_header(msg["Subject"])[0][1]).encode("utf-8")
		else:
			print unicode(decode_header(msg["Subject"])[0][0],"utf-8")	
		print ""
	except:
		print ""	
		pass
Пример #26
0
def decode(*args):
    quoted_printable_string = args[0]
    if len(args) == 2:
        encoding = args[1]
        if not encoding:
            encoding = 'latin1'
    else:
        return ' '.join([decode(*bit) for bit in decode_header(
            quoted_printable_string)])
    return unicodedata.normalize('NFC', unicode(decode_header(
        quoted_printable_string)[0][0], encoding=encoding, errors='ignore'))
Пример #27
0
    def msg_to_dict(self, msg):
        subject = self._header_convert(header.decode_header(msg['subject']))
        fro = self._header_convert(header.decode_header(msg['from']))
        to = self._header_convert(header.decode_header(msg['to']))

        return {
            'id': int(msg.id or 0),
            'subject': subject,
            'from': fro,
            'to': to,
            'body': self.get_msg_payload(msg),
        }
Пример #28
0
 def _decode(self, text):
     'Decode text into utf-8'
     try:
         packs = decode_header(text)
     except HeaderParseError:
         try:
             packs = decode_header(text.replace('?==?', '?= =?'))
         except HeaderParseError:
             log.warn(self.format_error('Could not decode header', text))
             packs = [(text, 'utf-8')]
     string = ''.join(part.decode(encoding or 'utf-8', 'ignore') for part, encoding in packs)
     return PATTERN_WHITESPACE.sub(' ', string.strip())
Пример #29
0
    def get_email(self, from_address, subject_pattern=None):
        """
        Get the latest unread email
        """
        timeout = time.time() + self.timeout
        try:
            conn = IMAP4_SSL(self.imap_host, self.imap_port)
            conn.login(self.user, self.password)

            while True:
                # Note: If you want to search unread emails, you should login after new emails are arrived
                conn.list()
                conn.select('inbox')
                #typ, data = conn.search(None, 'ALL')
                #typ, data = conn.search(None, '(UNSEEN HEADER Subject "%s")' % subject)
                #typ, data = conn.search(None, '(ALL HEADER FROM "%s")' % from_address)
                # Search unread ones
                typ, data = conn.search(None, '(UNSEEN HEADER FROM "%s")' % from_address)
                ids = data[0].split()
                print "ids=%s" % ids
                # Search from backwards
                for id in ids[::-1]:
                    typ, data = conn.fetch(id, '(RFC822)')
                    raw_email = data[0][1]
                    msg = email.message_from_string(raw_email)
                    msg_subject = decode_header(msg.get('Subject'))[0][0]
                    msg_encoding = decode_header(msg.get('Subject'))[0][1] or self.email_default_encoding
                    if subject_pattern and re.match(subject_pattern, msg_subject.decode(msg_encoding)) is None:
                        continue
                    # TODO: Cannot use when maintype is 'multipart'
                    #print  msg
                    return {
                        'from_address': msg.get('From'),
                        'to_addresses': msg.get('To'),
                        'cc_addresses': msg.get('CC'),
                        'bcc_addresses': msg.get('BCC'),
                        'date': msg.get('Date'),
                        'subject': msg_subject.decode(msg_encoding),
                        'body': msg.get_payload().decode(msg_encoding),
                        # TODO: Attached file
                    }

                '''
                if time.time() > timeout:
                    raise Exception("Timeout!")
                time.sleep(5)
                '''
        except:
            raise
        finally:
            conn.close()
            conn.logout()
Пример #30
0
	def getMailList(self, directory = None, searchParams = None):
		print "DIR", directory
		print "SEARCH", searchParams
		messages = []
		
		search = '(ALL)'
		if searchParams:
			search = []
			for item in searchParams:
				#if not
				search.append(item + ' "' + searchParams[item] + '"')
			
			search = "(" + ' '.join(search) + ")"
			
		
		if not directory:
			directory = "INBOX"
			
		
			
		result = self.imapHandle.select(str(directory))
		
		status, data = self.imapHandle.search(None, search)
		for num in data[0].split():
			status, data = self.imapHandle.fetch(num, '(BODY[HEADER.FIELDS (SUBJECT FROM DATE CONTENT-TYPE MESSAGE-ID)])')
			if status == 'OK':
				msg = email.message_from_string(data[0][1])
				charset = msg.get_content_charset()
				try:
					if charset:
						subject =  unicode(decode_header(msg.get_all('Subject')[0])[0][0], charset, "replace")
					else:
						subject = decode_header(msg.get_all('Subject')[0])[0][0]
				except TypeError,e :
					subject = ""
				
				try:
					sender = msg.get_all('From')[0]
				except TypeError,e :
					sender = ""
				
				dateTuple = email.utils.parsedate_tz(msg.get_all('Date')[0])
				date = datetime.datetime.fromtimestamp(email.utils.mktime_tz(dateTuple))
				
				messages.append({
					'mail_id': msg.get('Message-ID'),
					'folder': str(directory),
					'subject': subject,
					'sender': sender,
					'date': date,
					'folder': directory
				})
Пример #31
0
import poplib
Пример #32
0
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

LOG = INFO_LOG(__name__)
EXCEPTION = EXCEPTION_LOG(__name__)
WARN = WARN_LOG(__name__)
DEBUG = DEBUG_LOG(__name__)

MIN_HOURS = 6.995  #record hours

decode = lambda header: u''.join(
    val.decode('utf-8' if not encoding else encoding)
    for val, encoding in decode_header(header)).strip()

Q_ENCODING_REGEXP = re.compile(r'(\=\?[^\?]+\?[QB]\?[^\?]+\?\=)')


def decode_subject(val):
    for value in Q_ENCODING_REGEXP.findall(val):
        val = val.replace(value, decode(value))
    return val.strip()


def get_msg_payload(msg):
    encoding = msg.get('Content-Transfer-Encoding')
    payload = msg.get_payload()
    if type(payload) == list:
        a_msg = payload[0]  # first is plaintext, second - html
Пример #33
0
notification_timeout = 12000
unread_mail_icon = r"/usr/share/icons/oxygen/32x32/status/mail-unread-new.png"

# Getting the path of all the boxes
fd = open(expanduser(r"~/.mutt/mailboxes"), 'r')
# [10:-1] to remove the initial '^mailboxes '
boxes = filter(None, (b.rstrip().replace('+', '')
                      for b in fd.readline()[10:-1].split('"')))
fd.close()

pynotify.init(r'email_notifier.py')

# Handling a new mail
icon = pixbuf_new_from_file(unread_mail_icon)
dec_header = lambda h: ' '.join(
    unicode(s, e if bool(e) else 'ascii') for s, e in decode_header(h))


def newfile(event):
    fd = open(event.pathname, 'r')
    mail = MaildirMessage(message=fd)
    From = "From: " + dec_header(mail['From'])
    Subject = "Subject: " + dec_header(mail['Subject'])
    n = pynotify.Notification(
        "New mail in " + '/'.join(event.path.split('/')[-3:-1]),
        From + "\n" + Subject)
    fd.close()
    n.set_icon_from_pixbuf(icon)
    n.set_timeout(notification_timeout)
    n.show()