Created
September 22, 2017 16:51
-
-
Save mohclips/ab45cf7b9cc3a406fcbea150ed500f98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# demo to | |
# 1. utilse python keystone auth/client | |
# 2. send a multipart MIME html email with embedded image | |
# This is outlook 2007 compatible :) | |
# | |
from keystoneauth1 import loading as ks_loading | |
from keystoneauth1.identity import v3 | |
from keystoneauth1 import session | |
from keystoneclient.v3 import client as keystone_client | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEText import MIMEText | |
from email.MIMEImage import MIMEImage | |
import base64 | |
import argparse | |
import sys | |
import os | |
import requests | |
# | |
# SMTP - required by K5 Email Delivery Service API | |
# | |
MAIL_IDENTITY_ENDPOINT="https://identity.jp-east-1.cloud.global.fujitsu.com/v3" | |
MAILSERVICE_ENDPOINT="https://mail.jp-east-1.cloud.global.fujitsu.com" | |
MAIL_FROM="[email protected]" | |
MAIL_PROJECT_ID="24f8e79d6ca24560863113562b406963" | |
MAIL_TO="[email protected], [email protected]" | |
MAIL_SUBJECT="test email to K5 Email Delivery Service" | |
picture_filename="test_image.png" | |
html_template=""" | |
<html> | |
<body> | |
<h1>Test email for K5 K5 Email Delivery Service API</h1> | |
<img src=cid:{}> | |
<a href="https://k5-doc.jp-east-1.paas.cloud.global.fujitsu.com/doc/en/iaas/document/api-reference/v1/ap/index.html#!_Email delivery service">The K5 Email Service API can be found here</a> | |
</body> | |
</html> | |
""".format(picture_filename) | |
# the image cid needs to match the headers below | |
# i cheat and use the filename :) | |
def mail(email_session): | |
# MIME the related / root section | |
root = MIMEMultipart("related") | |
root['To'] = MAIL_TO | |
root['From'] = MAIL_FROM | |
root['Subject'] = MAIL_SUBJECT | |
root.preamble = 'This is a multi-part message in MIME format.' | |
# MIME the alternative section - this is your HTML | |
multipart = MIMEMultipart("alternative") | |
root.attach(multipart) | |
msgHtml = MIMEText(html_template.encode('utf-8'), 'html', 'UTF-8') | |
multipart.attach(msgHtml) | |
# Read image and MIME encode it | |
# add a header to the image section | |
# and attach to the root section | |
fp = open(picture_filename, 'rb') | |
msg_img = MIMEImage(fp.read()) | |
fp.close() | |
msg_img.add_header('Content-ID', '<{}>'.format(picture_filename)) | |
msg_img.add_header('Content-Disposition', 'inline', filename=picture_filename) | |
root.attach(msg_img) | |
# get the MIME encoded messages as a string | |
email_body = root.as_string() # make sure you get the root of the msg | |
# create requests.post data payload for x-www-form-urlencoded | |
# the K5 Email Delivery Service API requires that the data payload is base64 encoded | |
email_payload={ | |
'Action': 'SendRawEmail', | |
'RawMessage.Data': base64.b64encode(email_body) | |
} | |
# get our email service authorisation token | |
auth_headers=email_session.get_auth_headers() | |
email_token=auth_headers['X-Auth-Token'] | |
# make the API call | |
response=requests.post( | |
url=MAILSERVICE_ENDPOINT, | |
headers={ | |
'X-Auth-Token': email_token, | |
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', | |
'Accept': 'application/xml' | |
}, | |
data=email_payload | |
) | |
if response.status_code in (200,): | |
print "K5 Email Delivery Service Mail sent successfully" | |
else: | |
print "K5 Email Delivery Service Error: " + str(response.status_code) + " : " + str(response.text) | |
if __name__ == "__main__": | |
# here we use the keystone auth client to | |
# support OS_* environment variables | |
# and/or --os-* arguments | |
parser = argparse.ArgumentParser() | |
ks_loading.register_auth_argparse_arguments(parser, sys.argv[1:], default='v3password') | |
ks_loading.register_session_argparse_arguments(parser) | |
email_opts = parser.parse_args() # additonal args loaded in | |
email_session = None | |
# | |
# Get a regional session / token for Japan for the K5 Email Delivery Service | |
# | |
email_opts.os_auth_url=MAIL_IDENTITY_ENDPOINT | |
email_opts.os_project_domain_name=None | |
email_opts.os_project_domain_id=None | |
email_opts.os_project_id=MAIL_PROJECT_ID | |
email_opts.os_project_name=None | |
email_opts.os_region_name=None | |
email_auth = ks_loading.load_auth_from_argparse_arguments(email_opts) | |
email_session = ks_loading.load_session_from_argparse_arguments(email_opts, auth=email_auth) | |
auth_headers=email_session.get_auth_headers() | |
email_token=auth_headers['X-Auth-Token'] | |
print "Email token success", email_token | |
mail(email_session) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment