python - 使用 smtplib 通过电子邮件发送带有缩进格式的字典列表作为字符串?

标签 python smtplib

我正在尝试将带有缩进格式的词典列表发送到我的电子邮件。目前,这是我根据我的代码收到的电子邮件:

[{
        'post_id': '3524',
        'text': '🥕\n.\n▪️10/3 9 PM\n 9:00 ~ 11:45 Special host\n \n.\n▪️\n ~\n.\n▪️10/5 8PM More\n',
        'time': datetime.datetime(2019, 6, 13, 18, 31, 36),
        'image': ‘’,
        'link': 'https://bit.ly/'
    }, { {
            'post_id': '3524',
            'text': '🥕\n.\n▪️10/3 9 PM\n 9:00 ~ 11:45 Special host\n \n.\n▪️\n ~\n.\n▪️10/5 8PM More\n',
            'time': datetime.datetime(2019, 6, 13, 18, 31, 36),
            'image': ‘’,
            'link': None
        }
        ]

我的代码将电子邮件作为单行发送,其中“\n”显示为字母且未设置制表符格式。

我需要保留表情符号、特殊字符(不同语言),同时包括换行符。我该如何实现这一目标?

当前代码:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


print(type(posts))  # <class 'list'>
print(posts)  # [{'post_id': '3524', 'text': '🥕\n.\n▪️10/3 9 PM\n 9:00 ~ 11:45 Special host\n \n.\n▪️\n ~\n.\n▪️10/5 8PM More\n', 'time': datetime.datetime(2019, 6, 13, 18, 31, 36), 'image': ‘’, 'link': 'https://bit.ly/'}, {{'post_id': '3524', 'text': '🥕\n.\n▪️10/3 9 PM\n 9:00 ~ 11:45 Special host\n \n.\n▪️\n ~\n.\n▪️10/5 8PM More\n', 'time': datetime.datetime(2019, 6, 13, 18, 31, 36), 'image': ‘’, 'link': None}]

posts = str(posts)  # otherwise error: msg.attach(MIMEText(message_content)) File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/email/mime/text.py", line 34, in __init__ _text.encode('us-ascii') AttributeError: 'list' object has no attribute 'encode'

recipients = ["recipient_id@yahoo.com"]
sender = "sender_id@gmail.com"
subject = "report reminder"
body = posts

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ", ".join(recipients)
msg.attach(MIMEText(body, 'plain'))

# sending
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls()
session.login(sender, 'my password')
send_it = session.sendmail(sender, recipients, msg.as_string())
session.quit()

为了澄清,我想将整个字典列表作为字符串、括号和所有格式(事件选项卡/换行符)发送。

最佳答案

如果您想在电子邮件中使用格式化的 JSON,则需要在正文数据中使用 json.dumps()。为此,您需要在函数中提供 indent 参数。查看代码

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import json

posts = [{"latlng":[77.7355421,12.985924],"name":"International Tech Park Bangalore 2.5 km","type":"work","icon":"suitcase"},{"latlng":[77.7515038,12.9829723],"name":"H M Tech Park 2.3 km","type":"work","icon":"suitcase"},{"latlng":[77.721544,12.981423],"name":"Prestige Featherlite Techapark 4.7km","type":"work","icon":"suitcase"}]


recipients = ["reciever@demo.com"]
sender = "sender@demo.com"
subject = "report reminder"
body = json.dumps(posts, indent=4) #changed code line

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ", ".join(recipients)
msg.attach(MIMEText(body, 'plain'))

# sending
session = smtplib.SMTP('smtp.office365.com', 587)
session.starttls()
session.login(sender, 'your password')
send_it = session.sendmail(sender, recipients, msg.as_string())
session.quit()

电子邮件内容如下:

[
    {
        "latlng": [
            77.7355421,
            12.985924
        ],
        "name": "International Tech Park Bangalore 2.5 km",
        "type": "work",
        "icon": "suitcase"
    },
    {
        "latlng": [
            77.7515038,
            12.9829723
        ],
        "name": "H M Tech Park 2.3 km",
        "type": "work",
        "icon": "suitcase"
    },
    {
        "latlng": [
            77.721544,
            12.981423
        ],
        "name": "Prestige Featherlite Techapark 4.7km",
        "type": "work",
        "icon": "suitcase"
    }
]

关于python - 使用 smtplib 通过电子邮件发送带有缩进格式的字典列表作为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58223773/

相关文章:

python - 如何将电子邮件主题从 "?UTF-8?...?="转换为可读字符串?

python - Django:存储用户偏好

python - 理解 Python 中的全局变量

python - 在 Dockerfile 中激活 python virtualenv

python - 使用 Django Stack 预配置的 Amazon EC2 AMI

python - 使用 python smtplib 发送 'from' 电子邮件的别名(欺骗)

python - 在 python 中的电子邮件的发件人字段中添加发件人姓名

python - 如何通过代理发送带有 smtplib 模块的电子邮件?

python - SMTPAuthenticationError 5.7.14 请通过网络浏览器登录\n5.7.14

python - 根据我是逐条语句还是作为一个 block 运行代码,我怎么可能得到不同的结果?