-
Notifications
You must be signed in to change notification settings - Fork 3
/
scaleEmail.py
80 lines (63 loc) · 2.5 KB
/
scaleEmail.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
import re
import sys, os
import yaml
def sendEmail (smtpServer, gmailCreds, fAddr, tAddr, subj, body):
# https://docs.python.org/2/library/email-examples.html
import smtplib
from email.mime.text import MIMEText
msg = MIMEText(body)
msg['Subject'] = subj
msg['From'] = fAddr
msg['To'] = tAddr
s = smtplib.SMTP(smtpServer)
# We only need all this if we're authenticating to GMail, not if
# we are using 'localhost' as the SMTP server.
if re.search('gmail',smtpServer):
'''
the file 'gmailCreds.sec' is a text file that has two lines:
<googleID>@gmail.com
<gmail password or application specific password>
with no indenting; only the first two lines are used, but all
of the lines are read, so don't be too crazy with what's in
there.
'''
creds=[]
try:
with open(gmailCreds) as f:
creds = [x.strip('\n') for x in f.readlines()]
except:
errorString = '''I couldn't open the file {0} to read the GMail credentials, so I'm giving up (Logged from {1}/{2})
'''.format(gmailCreds,os.path.abspath(os.path.dirname(sys.argv[0])),sys.argv[0])
logging.error(errorString)
exit (1)
s.starttls()
s.login(creds[0], creds[1])
mailResults = {} # so we can comment out the next line and things still work
mailResults = s.sendmail(fAddr, [tAddr], msg.as_string())
s.quit()
if (mailResults):
return mailResults
else:
return 0
if __name__ == "__main__":
import logging
emailConfigFile = './emailConfig.yaml'
try:
f = open(emailConfigFile)
except:
errorString = '''I couldn't open the file {0} to read the email settings, so I'm giving up (Logged from {1}/{2})
'''.format(emailConfigFile,os.path.abspath(os.path.dirname(sys.argv[0])),sys.argv[0])
logging.error(errorString)
exit (1)
emailConfig = yaml.safe_load(f)
f.close()
subject = "test email from Python"
body = '''
This is the test of my email message. It is pretty boring, but
it is a few lines of text that will help me to see what different clients
do with this formatting.'''
results = sendEmail (emailConfig['smtpServer'], emailConfig['gmailCredsFile'],
emailConfig['fromAddr'], emailConfig['toAddr'],
subject, body)
if (results):
logging.warning ("Delivery Failed, at least in part: %s",results)