使用Predix的邮件通知服务
作者:唐翊国,开发者生态资深经理,GE数字集团
23年工作经验,长期在杜邦、欧文斯科宁、庄信万丰、通用电气医疗等从事制造业信息化工作,规划、实施了大量MES、SAP ERP、LIMS、BPM等项目,积累了丰富的制造业数字化转型经验。
如果您还没有Predix试用帐号,请访问
https://supportcentral.ge.com/esurvey/GE_survey/takeSurvey.html?form_id=18446744073709715720
申请。请务必准确提供您的信息,我们会以邮件方式通知您注册结果。
如果您使用Windows操作系统,请参考http://blog.csdn.net/predixcn/article/details/53967673系列文章设置您的开发环境。
如果您使用Linux操作系统,请参考http://blog.csdn.net/predixcn/article/details/54093234系列文章设置您的开发环境。
在文章http://blog.csdn.net/predixcn/article/details/74639346 中我们已经创建了一个邮件通知服务,本文将示例如何使用Python语言来使用邮件通知服务的REST API。
由于通知服务使用了UAA来做用户认证和授权管理,我们必须要在代码中先获得UAA 访问令牌(access token),请参考文章
http://blog.csdn.net/predixcn/article/details/70213325 来学习如何获得一个访问令牌(access token)。
Predix的官方文档https://docs.predix.io/en-US/content/service/operations/notification/using-configuration-services
介绍了邮件通知服务的各个REST API,本文将主要介绍如何配置邮件服务器和发送文本型邮件。
1、配置邮件服务器
注:本文中是以配置google的免费SMTP服务来作为参考的。
对应的入口是:
https://ev-notification-service.run.aws-usw02-pr.ice.predix.io/v1/tenants/724967f8-9822-483b-adbf-4796f0ebaf9b/configurations/
其中的“724967f8-9822-483b-adbf-4796f0ebaf9b”是文章http://blog.csdn.net/predixcn/article/details/74639346中环境变量里的tenantUuid。
使用POST协议,请求头为:
Authorization: bearer <token fromtrusted issuer>
Content-Type: application/json
请求数据部分是
[
{
"protocol": "smtp",
"host": "smtp.gmail.com",
"port": "587",
"smtpAuth": "true",
"smtpStarttlsEnable": "true",
"mailFrom": "yourmail@gmail.com",
"mailUsername": "yourmail@gmail.com",
"mailPassword": "password",
"mailReturnPath": null
}
]
解释完请求的细节,我们来学习如何在Python中实现它。
本文使用的Python 是2.7.13版本,编辑一个configuresmtp.py文件:
import requests
#Get UAA access token
data ={"grant_type":"client_credentials","client_id":"yourclientid","client_secret":"secret","response_type":"token"}
uaa ="https://ba67c2f3-3af7-436a-9c40-d86da25600b7.predix-uaa.run.aws-usw02-pr.ice.predix.io/oauth/token"
response = requests.post(uaa, data=data)
access_token =response.json()["access_token"]
#Create Configuration
url = "https://ev-notification-service.run.aws-usw02-pr.ice.predix.io/v1/tenants/724967f8-9822-483b-adbf-4796f0ebaf9b/configurations/"
payload = """[{
"protocol": "smtp",
"host": "smtp.gmail.com",
"port": "587",
"smtpAuth": "true",
"smtpStarttlsEnable": "true",
"mailFrom": "yourmail@gmail.com",
"mailUsername": "yourmail@gmail.com",
"mailPassword": "password",
"mailReturnPath": null
}]"""
headers = {"Authorization":"Bearer " + access_token,"content-type":"application/json"}
r = requests.post(url, data=payload,headers=headers)
print r.json()["payload"]
在命令行里运行,
python configuresmtp.py
应该会得到类似这样的结果:
{
"payload": [
{
"id": 486,
"uuid": "83255967-ebaf-4b26-b218-6f6bfdecf3a0",
"tenantUuid":"724967f8-9822-483b-adbf-4796f0ebaf9b",
"protocol": "smtp",
"host": "smtp.gmail.com",
"port": 587,
"smtpAuth": "true",
"smtpStarttlsEnable": "true",
"mailFrom": "yourmail@gmail.com",
"mailUsername": "yourmail@gmail.com",
"mailPassword": "password",
"timestamp": 1499405790123,
"lastUpdated": 1499405790128,
"mailReturnPath": null
}
],
"uuid": "a8acfd42-02ca-424e-a459-a9c6a3139412",
"status": 1000,
"message": "Configuration(s) added successfully andsensitive information is encrypted for security reasons.",
"timestamp": 1499311964526
}
2、发送文本类型的邮件
注:本文中是以配置google的免费SMTP服务来作为参考的。
对应的入口是:
https://ev-notification-service.run.aws-usw02-pr.ice.predix.io/v1/tenants/724967f8-9822-483b-adbf-4796f0ebaf9b/email?configuration=83255967-ebaf-4b26-b218-6f6bfdecf3a0
其中的“724967f8-9822-483b-adbf-4796f0ebaf9b”是文章http://blog.csdn.net/predixcn/article/details/74639346中环境变量里的tenantUuid,“83255967-ebaf-4b26-b218-6f6bfdecf3a0”是第一步里创建的SMTP服务器的uuid。
使用POST协议,请求头为:
Authorization: bearer <token fromtrusted issuer>
Content-Type: application/json
请求数据部分是
{
"body":"Content of the email message",
"subject":"Subject of the email",
"fromEmail":"yourmail@gmail.com",
"fromName" :"****",
"important" :false,
"recipients":
[
{
"email":"recipient@recipient.com",
"recipientName":"****",
"type":"to"
}
],
"attachments":null
}
解释完请求的细节,我们来学习如何在Python中实现它。
本文使用的Python 是2.7.13版本,编辑一个sendmail.py文件:
import requests
import json
#Get UAA access token
data ={"grant_type":"client_credentials","client_id":"yourclientid","client_secret":"secret","response_type":"token"}
uaa ="https://ba67c2f3-3af7-436a-9c40-d86da25600b7.predix-uaa.run.aws-usw02-pr.ice.predix.io/oauth/token"
response = requests.post(uaa, data=data)
access_token =response.json()["access_token"]
#Send email
url ="https://ev-notification-service.run.aws-usw02-pr.ice.predix.io/v1/tenants/724967f8-9822-483b-adbf-4796f0ebaf9b/email?configuration=83255967-ebaf-4b26-b218-6f6bfdecf3a0"
payload = """{
"body":"this is a test from google SMTP server",
"subject":"test from predix email notification -20170707",
"fromEmail":"yourmail@gmail.com",
"fromName" :"****",
"important" :false,
"recipients":
[
{
"email":"*****",
"recipientName":"****",
"type":"to"
}
],
"attachments":null
}"""
headers = {"Authorization":"Bearer " + access_token,"content-type":"application/json"}
r = requests.post(url, data=payload,headers=headers)
print r.json()
在命令行里运行,
python sendmail.py
应该会得到类似这样的结果:
{
"payload": {
"errors": [],
"notificationReferenceUuid":"c263f6c1-9018-4cf3-b59a-451caa35e6b7"
},
"uuid": "bc8a3b04-4ff0-4dcd-854a-0b5965a940f3",
"status": 1013,
"message": "Email message is queued",
"timestamp": 1499312487101
}
同时在邮箱里可以看到发来的邮件:
在使用中您有任何问题,请访问我们的论坛http://bbs.csdn.net/forums/GEPredix
GE数字集团的技术专家们会在线回答您的问题。
也请访问我们在CSDN的Predix专区http://predix.csdn.net/ 了解更多Predix的内容和相关活动。