-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp_send.py
48 lines (43 loc) · 1.35 KB
/
smtp_send.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import smtplib
import time
import sys
# User editable settings
user_auth = '[email protected]'
pword = 'my_secret_password'
server = 'mail.myserver.tld'
fromaddr = '[email protected]' # In case it's different from user_auth
toaddr = '[email protected]'
subject = 'This is the subject'
mail_body = '''
This is the email body.
'''
# Compose the message
msg = ('From: %s\r\nTo: %s\r\n' % (fromaddr, toaddr))
msg += 'Subject: %s\r\n' % (subject)
msg += 'Date: ' + time.strftime("%a, %d %b %Y %X -0300") + '\r\n\r\n'
msg += mail_body
# Try sending the mail, and catch some common errors
try:
try:
print 'Sending message from %s to %s' % (fromaddr, toaddr)
smtp_conn = smtplib.SMTP(server)
smtp_conn.set_debuglevel(0)
smtp_conn.ehlo()
try:
# TODO: try other authentication methods, use SSL/TLS
smtp_conn.login(user_auth, pword)
smtp_conn.sendmail(user_auth, toaddr, msg)
except smtplib.SMTPAuthenticationError:
print 'Authentication error'
sys.exit(1)
print 'Message succesfully sent from %s to %s' % (fromaddr, toaddr)
except smtplib.SMTPException:
print 'Error connecting'
sys.exit(1)
except KeyboardInterrupt:
print 'Canceling'
sys.exit(2)
finally:
smtp_conn.quit()