Skip to content

Instantly share code, notes, and snippets.

@diegozr1
Created December 17, 2018 18:26
Show Gist options
  • Save diegozr1/f51fb22a025ee820e2a3d02142baf846 to your computer and use it in GitHub Desktop.
Save diegozr1/f51fb22a025ee820e2a3d02142baf846 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# send.py -s “Subject” -b “Body” -r “recipient”
import sys
#from smtplib import SMTP_SSL as SMTP
from smtplib import SMTP
#from email.MIMEText import MIMEText
from email.mime.text import MIMEText
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(‘-s’, action=’store’, dest=’subject_value’,
help=’Store a subject value’)
parser.add_argument(‘-b’, action=’store’, dest=’body_value’,
help=’Store a body value’)
parser.add_argument(‘-r’, action=’store’, dest=’recipient_value’,
help=’Store a recipient value’)
result = parser.parse_args()
print result.recipient_value
def alert():
smtp_server = ‘email-smtp.us-east-1.amazonaws.com’
#smtp_port = 587
smtp_port = 25
user = ‘AWS ACCESS KEY’
password = ‘AWS SECRET KEY’
sender = ‘yourdomain.com’
destination = [result.recipient_value]
text_subtype = ‘html’
content = result.body_value
subject = result.subject_value
try:
msg = MIMEText(content, text_subtype)
msg[‘Subject’]= subject
msg[‘From’] = sender
msg[‘To’] = ‘,’.join(destination)
conn = SMTP(smtp_server, smtp_port)
conn.set_debuglevel(1)
conn.ehlo()
conn.starttls()
conn.ehlo()
conn.login(user, password)
try:
if conn.sendmail(sender, destination, msg.as_string()):
print “Successful!”
finally:
conn.close()
except Exception, exc:
sys.exit(“Mail failed; %s” % str(exc))
alert()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment