-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
47 lines (35 loc) · 1.46 KB
/
utils.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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import settings
import os
class Email:
@classmethod
def send(cls):
print('Sending code snippet in Email...')
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = settings.EMAIL_SUBJECT
msg['to'] = settings.RECEIVER_EMAIL
with open(os.path.join('code_snippet', 'gtm.txt'), 'r') as f:
code_snippet = f.read()
f.close()
# Record the MIME type text/plain
text = MIMEText(code_snippet, 'plain')
msg.attach(text)
# Send the message via SMTP server.
try:
server = smtplib.SMTP(settings.SMTP_HOST, settings.SMTP_PORT)
except smtplib.socket.gaierror as error:
raise Exception('Invalid HOST or PORT for SMTP configuration: Original Message: %s' % (error))
server.starttls()
try:
server.login(settings.SMTP_EMAIL, settings.SMTP_PASSWORD)
except smtplib.SMTPAuthenticationError as error:
raise Exception('Invalid username or password for SMTP configuration: Original Message: %s' % (error))
server.quit()
# returns nothing if sent successfully.
sent = server.sendmail(settings.SENDER_EMAIL, settings.RECEIVER_EMAIL, msg.as_string())
if sent:
print(sent)
server.quit()