-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJMail.py
executable file
·84 lines (58 loc) · 2.25 KB
/
JMail.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#Import what you need#
import imaplib
import getpass
import csv
import sys
#Get UserName
user=raw_input('GMail Username: ')
#Get Pass
passw = getpass.getpass()
#Get JoveFolder Name
imappath=raw_input('What''s your IMAP server address? ')
jovefld=raw_input('What is the name of your JoveReg folder in GMail? ')
curdir=sys.path[0]
csvout=curdir + '/JoveReg.csv'
#Open GMail Connections
gm=imaplib.IMAP4_SSL(imappath)
gm.login(user,passw)
gm.select(jovefld,readonly=True)
#Instantiate Body List
bodies=list()
textlist=list()
textlist.append('First Name')
textlist.append('Last Name')
textlist.append('Email')
textlist.append('Company')
textlist.append('Research Areas')
textlist.append('Department')
textlist.append('Title/Position')
textlist.append('User Name')
textlist.append('User ID')
#Define extraction function
out=csv.writer(file(csvout,'wb'),dialect='excel')
out.writerow(textlist)
#Iterate over emails
emltoti=len(gm.search(None,'ALL')[1][0].split())
emltot=gm.search(None,'ALL')[1][0].split()[emltoti-1]
for eml in gm.search(None,'ALL')[1][0].split():
try:
body=gm.fetch(eml,'(UID BODY[TEXT])')[1][0][1]
body=body.replace('<BR/>','')
body=body.replace('<blockquote>',' ')
textbody=body.split()
textlist=list()
textlist.append(''.join(textbody[textbody.index('First')+2:textbody.index('Last')]))
textlist.append(''.join(textbody[textbody.index('Last')+2:textbody.index('Email:')]))
textlist.append(''.join(textbody[textbody.index('Email:')+1:textbody.index('Institution:')]))
textlist.append(''.join(textbody[textbody.index('Institution:')+1:textbody.index('Areas:')-1]))
textlist.append(''.join(textbody[textbody.index('Areas:')+1:textbody.index('Department:')]))
textlist.append(''.join(textbody[textbody.index('Department:')+1:textbody.index('Position:')]) )
textlist.append(''.join(textbody[textbody.index('Position:')+1:textbody.index('Username:')]))
textlist.append(''.join(textbody[textbody.index('Username:')+1:textbody.index('UserID:')]))
textlist.append(''.join(textbody[textbody.index('UserID:')+1]))
out.writerow(textlist)
print('Currently on email #' + eml + ' of ' + emltot)
except:
continue
del(out)
#Here be a test comment, yargggh