-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.py
53 lines (46 loc) · 1.6 KB
/
sender.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
import errno
import time
from smtpclient import SmtpMailClient
from create_mail import Mail
MAXIMUM_NUMBER_OF_ATTEMPTS=2
A_COUPLE_OF_SECONDS=2
class Sender():
def __init__(self, hostname, port, randomFrom, recipientList, randomSubject, randomDate, hasHtml, hasText, bodyPlainText, bodyHtmlText):
self.hostname=hostname
self.port=port
self.randomFrom=randomFrom
self.recipientList=recipientList
self.randomSubject=randomSubject
self.hasText=hasText
self.hasHtml=hasHtml
self.bodyPlainText=bodyPlainText
self.bodyHtmlText=bodyHtmlText
self.randomDate=randomDate
def run(self):
#create message
m=Mail()
m.setFrom(self.randomFrom)
m.addRecipient(self.recipientList)
m.setSubject(self.randomSubject)
if self.hasText:
m.setTextBody(self.bodyPlainText)
else:
m.setHtmlBody(self.bodyHtmlText)
m.setMessageID()
m.setDate(self.randomDate)
msg=m.create()
smtpclient=SmtpMailClient(self.hostname, self.port)
#send message
for attempt in range(MAXIMUM_NUMBER_OF_ATTEMPTS):
try:
smtpclient.send(m.mailfrom, m.to, msg)
except EnvironmentError as exc:
if exc.errno == errno.ECONNREFUSED:
time.sleep(A_COUPLE_OF_SECONDS)
else:
raise
else:
break
else:
raise RuntimeError("maximum number of unsuccessful attempts reached")
#smtpMailsink.stop()