-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmtp_lib.py
89 lines (80 loc) · 3.66 KB
/
smtp_lib.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
# Send email to one or more recipients
# sender, recipients, subject and body are strings
# Separate multiple recipients with space, comma or semicolon
# Return tuple (status, msg) where status 0 for success, 1 for error
# dsmall 8 Apr 2012, 24 Jun 2012
def sendmail(sender, recipients, subject, body):
import smtplib
import time
from email.mime.text import MIMEText
##### SET UP SMTP SERVER #####
# Fill in details of the SMTP server you will be using
# If your ISP doesn't provide an SMTP service, you can sign up with gmail
# These settings are correct for gmail SMTP but you will need to provide
# a LOCAL_HOST name for your Rascal and your gmail LOGIN and PASSWORD
HOST = 'smtp.gmail.com' # Outgoing mail server
PORT = 587 # Usually 25 or 587
LOCAL_HOST = 'rascalNN' # Name of your Rascal
TIMEOUT = 30.0 # Seconds
# Fill in this section if the SMTP server requires TLS (gmail does)
USE_TLS = True # True or False
LOGIN = '[email protected]'
PASSWORD = 'YourGmailPassword'
##### END OF SET UP #####
# Replace comma or semicolon with a space, then split into a list
toaddrs = recipients.replace(',', ' ').replace(';', ' ').split()
print '## sendmail ##', sender, toaddrs, subject
msg = MIMEText(body)
msg['From'] = sender
msg['To'] = ', '.join(toaddrs)
msg['Subject'] = subject
msg['Date'] = time.strftime("%a, %d %b %Y %H:%M:%S %z", time.localtime())
try:
server = smtplib.SMTP(HOST, PORT, LOCAL_HOST, TIMEOUT)
try:
if USE_TLS:
server.starttls()
server.ehlo()
server.login(LOGIN, PASSWORD)
resdict = server.sendmail(sender, toaddrs, msg.as_string())
if len(resdict) == 0:
result = (0, 'Email sent')
else:
sendmail_log(resdict)
result = (1, 'Can\'t send to some recipients, see log')
except smtplib.SMTPRecipientsRefused as resdict:
sendmail_log (resdict)
result = (1, 'Can\'t send to any recipients, see log')
except smtplib.SMTPHeloError:
result = (1, 'The server didn\'t reply properly to the HELO greeting')
except smtplib.SMTPSenderRefused:
result = (1, 'The server didn\'t accept the sender')
except smtplib.SMTPAuthenticationError:
result = (1, 'The server didn\'t accept the login/password')
except smtplib.SMTPDataError:
result = (1, 'The server replied with an unexpected error code')
except:
result = (1, 'Unexpected error')
server.quit()
except smtplib.SMTPConnectError:
result = (1, 'Could not connect to server')
except:
result = (1, 'Unexpected error on connect')
return result
def sendmail_log(resdict):
print '## sendmail ## Can\'t send to', resdict
def sender():
# Delete the next line, uncomment the following line and fill in your Rascal name and email address
sender = ''
# sender='rascalNN <[email protected]>'
return sender
def help():
help = ''
##### Delete this section to remove the help message #####
help = 'Before using this page, please edit smtp_lib.py and \
enter details of the SMTP server you will be using to send email. To get rid of this message and \
automatically fill in the Sender email address, edit procedures help and sender in smtp_lib.py. \
After making these changes, remember to click the Reload pytronics button to ensure \
that the server is running the latest version of the code.'
##### Delete up to here #####
return help