-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.py
More file actions
196 lines (132 loc) · 4.36 KB
/
mail.py
File metadata and controls
196 lines (132 loc) · 4.36 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
""" This file contains all the necessary functions to send,
search or delete emails
@author: Ahmed Tidiane BALDE
"""
import imaplib
import smtplib
import imapclient
# To fix the size error
imaplib._MAXLINE = 10000000
class UnknownValueException(Exception):
def __init__(self, value):
""" Initialize the exception
:param value: value that causes the exception
"""
self.value = value
def __str__(self):
""" Print the value when the exception occurs """
return repr("Unknown value : {}".format(self.value))
def getDomain(email, protocol="smtp"):
""" Get the server domain name giving the protocol and an email
:param protocol: smtp or imap
:param email: login an e-mail
:returns:
:rtype:
"""
protocol = protocol.lower()
if protocol not in ("smtp", "imap"):
raise UnknownValueException(protocol)
domain = email.split('@')[1].split('.')[0]
if domain == "gmail":
return 587, protocol + '.' + domain + '.com'
elif domain in ("outlook", "hotmail"):
return 587, protocol + '-mail.' + domain + '.com'
elif domain == "yahoo":
return 587, protocol + '.mail.' + domain + '.com'
elif domain == "att":
return 465, protocol + '.mail.' + domain + '.net'
elif domain == "comcast":
return 587, protocol + '.' + domain + '.net'
elif domain == "verizon":
return 465, protocol + '.' + domain + '.net'
else:
raise UnknownValueException(domain)
def connectImap(email, mdp):
""" Connect to an IMAP server
:param email: login
:param mdp: password
:param domain: domain name of the imap server
:returns: an imapclient that connects into the server
:rtype: IMAPClient Object
"""
port, domain = getDomain(email, "imap")
print(domain)
imapObj = imapclient.IMAPClient(domain, ssl=True)
imapObj.login(email, mdp)
return imapObj
def connectSMTP(email, mdp):
"""FIXME! briefly describe function
:param email: login
:param mdp: password
:param port: 587 for TLS and 465 for SSL
:param domain: domain name of the imap server
:returns: the smtp object
:rtype: SMTP Object
"""
port, domain = getDomain(email)
print(domain)
smtpObj = None
if port == 587:
smtpObj = smtplib.SMTP(domain, port)
smtpObj.ehlo()
# Enable TLS encryption if you are using 587 port
smtpObj.starttls()
elif port == 465:
# Encryption is automatically set up
smtpObj = smtplib.SMTP_SSL(domain, port)
smtpObj.ehlo()
else:
raise UnknownValueException(port)
smtpObj.login(email, mdp)
return smtpObj
def logout(obj, protocol='smtp'):
protocol = protocol.lower()
if protocol == 'smtp':
obj.quit()
elif protocol == 'imap':
obj.logout()
else:
raise UnknownValueException(protocol)
def sendmail(smtpObj, phrom, to, subject=None, body=None):
""" Send email from "phrom" to "to"
:param smtpObj: Object that connects into the mail
:param phrom: from
:param to: to
:param subject: Subject of the email
:param body: body of the email
:returns: send an email
:rtype: void
"""
smtpObj.sendmail(phrom, to, 'Subject: {}\n{}'.format(subject, body))
def listfolders(imapObj):
""" List the folders of an account
:param imapObj: Object that connects into the mail
:returns: prints the list of folders of the account
:rtype: void
"""
return imapObj.list_folders()
def deletefolder(imapObj, folder):
""" Delete a folder
:param imapObj: Object that connects into the mail
:param folder: the folder to delete
:rtype: void
:returns: delete emails
"""
imapObj.delete_folder(folder)
def searchmails(imapObj, folder='INBOX', **kwargs):
""" Search emails given infinite args
:param imapObj: Object that connects into the mail
:param folder: folder from which to search emails
"""
imapObj.select_folder(folder)
requete = []
for key in kwargs:
requete += ['{}'.format(key), '{}'.format(kwargs[key])]
uids = imapObj.search(requete)
return uids
def deletemails(imapObj, uids):
""" Delete an email from phrom
:param imapObj: Object that connects into the mail
:param uids: list of uids from which to delete emails
"""
imapObj.delete_messages(uids)