如何将数据从 mongo 数据库传递到 python 中的 html 电子邮件以显示基于 table 的摘要
How to pass data from mongo database to html email in python to show a table based summary
我正在尝试使用 python 创建一个基于 html 的电子邮件。因此,在我的电子邮件内容中,我想创建一个 html table 来显示 mongodb 集合的内容列表,其中包含有关最近 24 小时内添加的不同客户的数据。
如何使用 python 将 mongo 的 json 回复插入到 html 电子邮件中。我在这里使用的 json 响应是数据。
data = {
u'FirstName': u'ABC',
u'LastName': u'XYZ',
u'Company': u'KBS',
u'added_time': datetime.datetime(2016, 12, 12, 12, 12, 20, 207000),
u'ID': 123
}
以下是到目前为止的内容:-
import sys
import json
import smtplib
from pymongo import MongoClient
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender = 'abc@abc.com'
receivers = 'abc@abc.com'
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "TEST"
msg['From'] = sender
msg['To'] = receivers
client = MongoClient('mongodb://localhost:27017/')
db = client.abc
data = []
### Get all the customer added within last 24 hrs
for cust in db.customer.find{"added_time": {"$gt" : dt, "$lt" : datetime.now() }}):
data.append(cust)
print data
# Create the body of the message (a plain-text and an HTML version).
text = "Hi! this is auta geneated mail"
html = """\
<html>
<head></head>
<body>
<table>
</table>
</body>
</html>
"""
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
您应该使用有序字典,这样 json 对象就不会四处移动
from pymongo import MongoClient
from collections import OrderedDict
client = MongoClient('mongodb://localhost:27017/', document_class=OrderedDict)
不要编写自己的 table 解析器,而是使用像 json2html 这样的已经制作好的模块(python 的强大功能;))
库会将字典中的所有字段转换为 table,因此您需要制作一个仅包含必填字段的新字典
json_data = {...}
required = ['a','b','c']
relevant = {k.upper():json_data[k] for k in required}
对于多行,需要一个字典列表
table_json = [{...}, {...}, ...]
我正在尝试使用 python 创建一个基于 html 的电子邮件。因此,在我的电子邮件内容中,我想创建一个 html table 来显示 mongodb 集合的内容列表,其中包含有关最近 24 小时内添加的不同客户的数据。
如何使用 python 将 mongo 的 json 回复插入到 html 电子邮件中。我在这里使用的 json 响应是数据。
data = {
u'FirstName': u'ABC',
u'LastName': u'XYZ',
u'Company': u'KBS',
u'added_time': datetime.datetime(2016, 12, 12, 12, 12, 20, 207000),
u'ID': 123
}
以下是到目前为止的内容:-
import sys
import json
import smtplib
from pymongo import MongoClient
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
sender = 'abc@abc.com'
receivers = 'abc@abc.com'
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "TEST"
msg['From'] = sender
msg['To'] = receivers
client = MongoClient('mongodb://localhost:27017/')
db = client.abc
data = []
### Get all the customer added within last 24 hrs
for cust in db.customer.find{"added_time": {"$gt" : dt, "$lt" : datetime.now() }}):
data.append(cust)
print data
# Create the body of the message (a plain-text and an HTML version).
text = "Hi! this is auta geneated mail"
html = """\
<html>
<head></head>
<body>
<table>
</table>
</body>
</html>
"""
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
您应该使用有序字典,这样 json 对象就不会四处移动
from pymongo import MongoClient
from collections import OrderedDict
client = MongoClient('mongodb://localhost:27017/', document_class=OrderedDict)
不要编写自己的 table 解析器,而是使用像 json2html 这样的已经制作好的模块(python 的强大功能;))
库会将字典中的所有字段转换为 table,因此您需要制作一个仅包含必填字段的新字典
json_data = {...}
required = ['a','b','c']
relevant = {k.upper():json_data[k] for k in required}
对于多行,需要一个字典列表
table_json = [{...}, {...}, ...]