-
Notifications
You must be signed in to change notification settings - Fork 1
/
email_script.py
114 lines (108 loc) · 4.09 KB
/
email_script.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
# Utility for sending emails using pre-made template to companies to ask for permission for their products on our store
# Built-in Imports
import smtplib
from string import Template
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Personal imports
from information import Info
import SystemShortcuts as SyS
# Main method used
def send_emails(company_info):
# Set up the email client
s = smtplib.SMTP(host=Info.EHOST, port=Info.EPORT)
# Start it
s.starttls()
# Log in using the AOL account
s.login(Info.LOGIN, Info.EPWD)
# Set up the template file content var
tfc = None
# Read the template file and save the content
with open(Info.ETEMPLATE, 'r', encoding='utf-8') as tf:
tfc = tf.read()
# Create the template from that content
t = Template(tfc)
# For each brand given as input
for c in company_info:
# Create the message to be send
msg = MIMEMultipart()
# Use the information for this brand to create the body of the message
body = t.substitute(COMPANY_NAME=c)
# Set the "From" address
msg['From'] = Info.LOGIN
# Set the "To" address
msg['To'] = company_info[c]
# Set the subject of the email
msg['Subject'] = 'Online Store Item Usage'
# Attach the body of the message
msg.attach(MIMEText(body, 'plain'))
# Python used Rest!
SyS.slp(1.5)
# Send the message (in string form) using all of the above information
s.sendmail(msg['From'], msg['To'], msg.as_string())
# Python used Rest!
SyS.slp(1.5)
# Delete the message currently being used to reset
del msg
# Quit the email server
s.quit()
# Debugging ensure functionality
print('Emailing is done')
class EmailSending():
def __init__(self, *data):
try:
self.company_info = data[0]
except:
self.company_info = None
# Main method used
def send_emails(self, *company_info):
# Set up the email client
s = smtplib.SMTP(host=Info.EHOST, port=Info.EPORT)
# Start it
s.starttls()
# Log in using the AOL account
s.login(Info.LOGIN, Info.EPWD)
# Set up the template file content var
tfc = None
# Read the template file and save the content
with open(Info.ETEMPLATE, 'r', encoding='utf-8') as tf:
tfc = tf.read()
# Create the template from that content
t = Template(tfc)
# Try to set the company info from the parameters given
try:
self.company_info = company_info[0]
except:
pass
# Ensure that there is company information
assert self.company_info is not None
# For each brand given as input
for c in self.company_info:
# Create the message to be send
msg = MIMEMultipart()
# Use the information for this brand to create the body of the message
body = t.substitute(COMPANY_NAME=c,
DEVELOPER_NAME=Info.DEVELOPER_NAME,
DEVELOPER_POSITION=Info.DEVELOPER_POSITION,
THIS_COMPANY=Info.THIS_COMPANY,
COMPANY_LOCATION=Info.COMPANY_LOCATION)
# Set the "From" address
msg['From'] = Info.LOGIN
# Set the "To" address
msg['To'] = self.company_info[c]
# Set the subject of the email
msg['Subject'] = 'Online Store Item Usage'
# Attach the body of the message
msg.attach(MIMEText(body, 'plain'))
# Python used Rest!
SyS.slp(1.5)
# Send the message (in string form) using all of the above information
s.sendmail(msg['From'], msg['To'], msg.as_string())
# Python used Rest!
SyS.slp(1.5)
# Delete the message currently being used to reset
del msg
# Quit the email server
s.quit()
# Debugging ensure functionality
print('Emailing is done')