Python电子邮件,如何删除/刷新“收件人”消息中的字段

发布于 2025-01-08 16:04:17 字数 556 浏览 3 评论 0原文

我正在尝试编写一个 python 程序来将电子邮件发送到多个地址。

每次消息都是相同的,除了收件人的地址之外。 我以为我可以在循环中更改 msg['To'],

for email in emaillist
   ...
   msg['To'] = email
   ...
   ...

但实际上每次将 'email' 附加到 msg['To'] 中。

msg.as_string() 就像“收件人:[电子邮件受保护]\nTo :[email protected]'

如何替换 msg['To'] 字段而不是附加字段?

谢谢

I'm trying to write a python rogram to send email to multiple addresses.

Each time the message is the same except for the recipient's address.
I thought i could just change the msg['To'] in the loop,

for email in emaillist
   ...
   msg['To'] = email
   ...
   ...

but actually each time 'email' is appended to the msg['To'].

msg.as_string() is like 'To: [email protected]\nTo:[email protected]'

How can I replace the msg['To'] field rather than appending?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2025-01-15 16:04:17

您需要运行 del msg['To'] 来清除。 = 运算符是添加而不是替换。有关详细信息,请参阅 __setitem__文档

You need to run del msg['To'] to clear. The = operator adds rather than replaces. See the documentation for __setitem__ for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文