-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailSender.py
131 lines (112 loc) · 5.32 KB
/
EmailSender.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
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
import smtplib, imaplib, ssl
from getpass import getpass
#from email import encoders
from email.message import EmailMessage
class EmailSender:
"""this class sends a template to multiple receivers"""
smtp_server = "smtp.gmail.com"
imap_server = "imap.gmail.com"
port = 587
class TextArgs:
"""this class is used as a way to personalize the email"""
def __init__(self, args):
"""create a new argument list
@param args: a list of arguments for the text template
"""
self.args = args
def __init__(self, subject, text, textArgs, *, emailaddress=None, password=None, doNotDeleteSentMails=False, test=False):
"""constructor is used to do all the work of sending the email template
--- CAUTION: use a junk gmail account for this, to not lose important emails ---
Gmail has to be set up that it allows less secure apps and allow access via IMAP and SMTP
the list of receiving emails shall be provided with the textArgs directory.
Example: textArg[[email protected]] = TextArg("test", 123)
This way the receiver is tied to his personal email message.
After the emails have been sent, they are deleted from the server using imap. Be careful, as this function simply
searches for the subject. This could affect other emails, too.
@param subject: the subject of the email
@param text: a text template. This is formatted using the function 'format' and the given textArgs
@param textArgs: a directory (key is the receivers email) of arguments to format the text
@param test: set to true to use the localhost smtp daemon
@param password: can be used to provide the password (if not provided, the function will ask for it
@param emailaddress: the emailaddress to use, will be asked if not provided
@param doNotDeleteSentMails: set this to true if you want to keep the sent emails in the sentbox of your email account
"""
if (None == emailaddress):
# ask for gmail address and password
print("type your gmail address (only works with gmail) and password")
print("email address: ", end="")
emailaddress = input()
if (None == password) & (not test):
password = getpass()
smtp_server = EmailSender.smtp_server
smtp_port = EmailSender.port
# for testing: use localhost
if test:
smtp_server = "localhost"
smtp_port = 1025
# send the emails
with smtplib.SMTP(smtp_server, smtp_port) as server:
try:
if not test:
# Create a secure SSL context
context = ssl.create_default_context()
#server.ehlo()
server.starttls()
#server.starttls(context=context) # and secure the connection
#server.ehlo()
server.login(emailaddress, password)
message = EmailMessage()
message['from'] = emailaddress
message['subject'] = subject
for receiver in textArgs.keys():
del message['to']
message['to'] = receiver
message.clear_content()
message.set_content(text.format(*textArgs[receiver].args))
server.sendmail(emailaddress, receiver, message.as_string())
except Exception as e:
print(e)
finally:
server.quit()
# delete the sent emails right after
if (not doNotDeleteSentMails) & (not test):
with imaplib.IMAP4_SSL(EmailSender.imap_server) as imap:
try:
imap.login(emailaddress, password)
print(imap.select('[Gmail]/Gesendet', readonly=False))
typ, data = imap.search(None, 'SUBJECT \"' + subject + '\"')
for num in data[0].split():
imap.store(num, "+Flags", "\\Deleted")
imap.expunge()
except Exception as e:
print(e)
finally:
imap.close()
imap.logout()
def test():
"""For this test, the build in smtp daemon shall be used, run:
python -m smtpd -c DebuggingServer -n localhost:1025
in a console / shell
"""
text = "This is a test for {0}\n" +\
"Hello {0} {1},\n" +\
"You are won ${2},{3:02d}!\n" +\
"\n" +\
"Yours sincerely\n" +\
"Newman\n"
textArgs = {}
emailReceiver = ("[email protected]",
import random
import time
import re
random.seed(time.time())
for receiver in emailReceiver:
firstName = str.capitalize(re.split("[.@]", receiver)[0])
lastName = str.capitalize(re.split("[.@]", receiver)[1])
textArgs[receiver] = EmailSender.TextArgs((firstName, lastName, random.randint(0,1), random.randint(0,99)))
EmailSender("You Win!", text, textArgs, test=True, emailaddress="[email protected]")
if __name__ == '__main__':
test()