-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimap_fetch_email_3.py
More file actions
executable file
·48 lines (39 loc) · 1.79 KB
/
Copy pathimap_fetch_email_3.py
File metadata and controls
executable file
·48 lines (39 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python3
##
# Fetches emails from a server using imap-ssl
# Python 3.5.2 version
#
##
# the cvt script was adjusted slightly
import imaplib, datetime, ssl, logging
from cvt_pull_opp_mail import *
class Fetcher(object):
def __init__(self):
self.server = ""
self.port = 993 # standard IMAP4-over-SSL port
self.user = ""
self.password = ""
def getemails(self):
# context is only in python3.3+, defaults are: PROTOCOL_TLS, OP_NO_SSLv2, OP_NO_SSLv3 (most secure version)
context = ssl.create_default_context()
try:
mailman = imaplib.IMAP4_SSL(self.server, port=self.port, ssl_context=context)
mailman.login(self.user, self.password)
except imaplib.IMAP4.error as e:
logging.error("Imaplib connection error: {}".format(e))
# only retrieves unread mail from inbox
mailman.select(mailbox='INBOX')
resp, data = mailman.search(None, '(UNSEEN)')
for index in data[0].split():
r, d = mailman.fetch(index, '(RFC822)')
msg = d[0][1].decode("utf-8", "ignore") + "\n"
try:
# tests the decoding attachment script gets called correctly, using sample email in MIME format
transformer = Transformer()
print(transformer.perform(msg))
except IOError as e:
logging.error("Error writing to file: {}".format(e))
mailman.close()
mailman.logout()
fetcher = Fetcher()
fetcher.getemails()