Skip to content

Instantly share code, notes, and snippets.

@SeanPlusPlus
Last active January 31, 2017 21:24
Show Gist options
  • Select an option

  • Save SeanPlusPlus/0bee689fb5e7c053cfba09faba6476e5 to your computer and use it in GitHub Desktop.

Select an option

Save SeanPlusPlus/0bee689fb5e7c053cfba09faba6476e5 to your computer and use it in GitHub Desktop.
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email(message, recipient, subject):
# verify we have the creds
password = os.environ.get('EMAIL_PW', None)
if password is None:
return {'message': 'No Password'}
sender = os.environ.get('EMAIL_SENDER', None)
if sender is None:
return {'message': 'No Sender'}
host = os.environ.get('SMTP_HOST', None)
if host is None:
return {'message': 'No Host'}
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = recipient
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nApproval Requested?\nName: Test:\nhttp://www.python.org"
html = """\
<html>
<head>
</head>
<body>
<h1>Approval Requested</h1>
<p>Name: Test</p>
<hr />
<div class="btn-group btn-group-lg">
<a href="http://python.org" target="_blank" style="font-size: 16px; font-weight: bold; font-family: Helvetica, Arial, sans-serif; color: #ffffff; text-decoration: none; border-radius: 3px; -webkit-border-radius: 3px; -moz-border-radius: 3px; background-color: #5CB85C; border-top: 12px solid #5CB85C; border-bottom: 12px solid #5CB85C; border-right: 18px solid #5CB85C; border-left: 18px solid #5CB85C; display: inline-block; width:130px;">Approve</a>
<a href="http://python.org" target="_blank" style="font-size: 16px; font-weight: bold; font-family: Helvetica, Arial, sans-serif; color: #ffffff; text-decoration: none; border-radius: 3px; -webkit-border-radius: 3px; -moz-border-radius: 3px; background-color: #D9534F; border-top: 12px solid #D9534F; border-bottom: 12px solid #D9534F; border-right: 18px solid #D9534F; border-left: 18px solid #D9534F; display: inline-block; width:130px;">Deny</a>
</div>
</body>
</html>
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP(host, 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(sender, password)
toaddrs = [recipient]
s.sendmail(sender, toaddrs, msg.as_string())
s.close()
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment