-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemailslofar.py
More file actions
106 lines (85 loc) · 3.49 KB
/
emailslofar.py
File metadata and controls
106 lines (85 loc) · 3.49 KB
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
#pysovo VOEvent Tools
#Tim Staley 2012
#Based on various web snippets.
import simplejson as simplejson
import os
import sys
import getpass
import smtplib
import quick_keys as keys
import base64
# import pysovo as ps
default_email_config_file = "~as24v07/.email_acc"
known_users={'user': 'enter_b64encoded_email',}
def prompt_for_config( config_filename = default_email_config_file ):
default_smtp_server = "smtp.googlemail.com"
default_smtp_port = 587
# ps.utils.ensure_dir(config_filename)
outputfile=open(config_filename, 'w')
account = {}
print "Please enter the smtp server address:"
print "(Default = {dserve})".format(dserve=default_smtp_server)
account[keys.email_account.smtp_server]= raw_input(">")
if account[keys.email_account.smtp_server]=="":
account[keys.email_account.smtp_server] = default_smtp_server
print "Please enter the smtp server port:"
print "(Default = {dport})".format(dport=default_smtp_port)
account[keys.email_account.smtp_port]= raw_input(">")
if account[keys.email_account.smtp_port]=="":
account[keys.email_account.smtp_port] = default_smtp_port
print "Please enter the smtp username: (e.g. someone@gmail.com)"
account[keys.email_account.username]= raw_input(">")
print "Now please enter your password:"
account[keys.email_account.password]= getpass.getpass()
print "You entered:"
print "Server", account[keys.email_account.smtp_server]
print "Port", account[keys.email_account.smtp_port]
print "User", account[keys.email_account.username]
print "Pass", "(Not shown)"
outputfile.write(simplejson.dumps(account))
outputfile.close()
print ""
print "Account settings saved to:", config_filename
chmod_command = "chmod go-rwx {file}".format(file=config_filename)
os.system(chmod_command)
return config_filename
def load_account_settings_from_file( config_filename = default_email_config_file):
# print "Loading email acc from ", config_filename
try:
with open(config_filename, 'r') as config_file:
account = simplejson.loads(config_file.read())
except Exception:
print "Error: Could not load email account from "+ config_filename
raise
return account
def send_email( account,
recipient_addresses,
subject,
body_text,
verbose=False
):
if verbose:
print "Loaded account, starting SMTP session"
# recipient_addresses = raw_input()
# recipient_addresses = ps.utils.listify(recipient_addresses)
smtpserver = smtplib.SMTP(account[keys.email_account.smtp_server],
account[keys.email_account.smtp_port])
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(account[keys.email_account.username],
base64.b64decode(account[keys.email_account.password]))
sender = account[keys.email_account.username]
recipients_str=str(recipient_addresses)
if verbose:
print "Logged in, emailing", recipients_str
header = "".join( ['To: ',recipients_str,'\n',
'From: ',sender,'\n',
'Subject: ', subject,'\n'])
msg = "".join( [header,'\n',
body_text,'\n\n'])
smtpserver.sendmail(sender, recipient_addresses, msg)
if verbose:
print 'Message sent'
smtpserver.close()
pass