首页 > 解决方案 > Python Mail MIMEMultipart (.setpayload) 在日志中打印 \n 而不是进入下一行

问题描述

我正在尝试通过 MIMEMultipart 发送邮件,包括带有设置有效负载的电子邮件正文以捕获 HTML/图像和错误日志,除了错误日志没有进入下一行,当错误具有来自源 lambda 的默认值'\n' ,正因为如此,它看起来一点也不友好。我试图
在我的源 lambda 中替换 '\n ,但这次日志正在打印
而不是进入下一行。任何建议都将受到高度赞赏。

这是我的代码的一小段:我在下面的代码中添加了#event,它来自我的第一个 lambda 作为有效负载。

import json
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import *
from email.encoders import *
from email.message import EmailMessage
from mimetypes import *
import smtplib, email

messag = MIMEMultipart()
messag.add_header('Content-Type','text/html')
#event payload coming from first lambda
event = {'Source': 'Model', 'ModelName': 'Test  checks', 'Target': ['email'], 'EmailSubject': 'Alert  Notification - Test  checks failed', 'EmailBody': ' The lambda has failed for Test  checks<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>Error_Msg</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Traceback (most recent call last):\\n  File "/opt/python/pandas/core/indexes/base.py", line 2891, in get_loc\\n    return self._engine.get_loc(casted_key)\\n  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc\\n  File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc\\n  File "pandas/_libs/hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item\\n  File "pandas/_libs/hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item\\nKeyError: \'dealguid\'\\n\\nThe above exception was the direct cause of the following exception:\\n\\nTraceback (most recent call last):\\n  File "/var/task/lambda_function.py", line 569, in lambda_handler\\n    dqm_results_wfp,temp_deep_dive_results,alert_message,ap_comp_columns,payload_id = perform_data_quality_checks(json_str,engine)\\n  File "/var/task/lambda_function.py", line 409, in perform_data_quality_checks\\n    payload_id = list(result[\'dealguid\'])[0]\\n  File "/opt/python/pandas/core/frame.py", line 2902, in __getitem__\\n    indexer = self.columns.get_loc(key)\\n  File "/opt/python/pandas/core/indexes/base.py", line 2893, in get_loc\\n    raise KeyError(key) from err\\nKeyError: \'dealguid\'\\n</td>\n    </tr>\n  </tbody>\n</table> please refer to corresponding lambda function Test  checks in Dev to debug the issue. ', 'AdditionalRec': ['exampleid.com'}

messag.set_payload(event['EmailBody'])
messag['To'] = "exampleid.com"
   
messag['Subject'] = 'Test mail'
server = smtplib.SMTP('example.com', '25')
server.sendmail('exampleid.com' , 'exampleid.com', messag.as_string())

我收到邮件的输出:

Traceback (most recent call last):\n File "/var/task/lambda_function.py", line 397, in traverse_json\n dqm_json_df = pd.json_normalize(json.loads(json_string))\n File "/opt/python/pandas/io/json/_normalize.py", line 270, in _json_normalize\n if any([isinstance(x, dict) for x in y.values()] for y in data):\n File "/opt/python/pandas/io/json/_normalize.py", line 270, in <genexpr>\n if any([isinstance(x, dict) for x in y.values()] for y in data):\nAttributeError: 'str' object has no attribute 'values'\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File "/var/task/lambda_function.py", line 569, in lambda_handler\n dqm_results_wfp,temp_deep_dive_results,alert_message,ap_comp_columns,payload_id = perform_data_quality_checks(json_str,engine)\n File "/var/task/lambda_function.py", line 408, in perform_data_quality_checks\n result = traverse_json(json_payload)\n File "/var/task/lambda_function.py", line 399, in traverse_json\n dqm_json_df = pd.json_normalize(json_string)\n File "/opt/python/pandas/io/json/_normalize.py", line 270, in _json_normalize\n if any([isinstance(x, dict) for x in y.values()] for y in data):\n File "/opt/python/pandas/io/json/_normalize.py", line 270, in <genexpr>\n if any([isinstance(x, dict) for x in y.values()] for y in data):\nAttributeError: 'str' object has no attribute 'values'\n

标签: pythonaws-lambda

解决方案


您将正文格式化为 HTML 并添加text/html.

那么使用格式化是没有意义的\n,你应该坚持使用 html 元素并将\\n你的 boby替换为break : <br/>


推荐阅读