Skip to content

Day29 发送带附件邮件的示例代码缺少 email.attach(attachment) #1215

Description

@feather222

在 Day21-30/29.Python发送邮件和短信.md 中,「发送带附件的邮件」一节的示例代码存在一个小问题:
代码虽然创建了 attachment 对象并设置了相关头部信息,但没有调用 email.attach(attachment) 将附件添加到邮件主体中。这会导致实际发送的邮件不包含附件。

Image

问题代码位置:
Python

with open(f'resources/王大锤离职证明.docx', 'rb') as file:
attachment = MIMEText(file.read(), 'base64', 'utf-8')
attachment['content-type'] = 'application/octet-stream'
filename = quote('王大锤离职证明.docx')
attachment['content-disposition'] = f'attachment; filename="{filename}"'

此处缺少 email.attach(attachment)

建议修复:
在 with 代码块结束后(或内部末尾)添加:
Python

email.attach(attachment)
感谢作者和维护团队的辛勤付出,这个项目帮助了很多初学者!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions