python - Python通过电子邮件发送pdf得到意外的关键字参数'main_type'
问题描述
我正在尝试附加 pdf 并在 python 中发送电子邮件。低于错误 -
TypeError:set_bytes_content() 得到了一个意外的关键字参数“main_type”。
新手在这里,不知道我做错了什么?
import os
import smtplib
from email.message import EmailMessage
import PyPDF2
EMAIL_ADDRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
msg = EmailMessage()
msg['Subject'] = 'EDs Diet Plan'
msg['From'] = EMAIL_ADDRESS
msg['To'] = 'test@gmail.com'
msg.set_content('Please find attached your customized diet plan and the general list of foods')
file = 'LFV Diet.pdf'
with open(file, 'rb') as f:
file_data = f.read()
#file_type =
file_name = f.name
msg.add_attachment(file_data, main_type='application', subtype='octet-stream', file_name=file_name)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
解决方案
您有一些拼写错误 - 只需更改main_type
为maintype
和file_name
to filename
。
前:
msg.add_attachment(file_data, main_type='application', subtype='octet-stream', file_name=file_name)
后:
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file_name)
一些很好的例子在这里 - https://docs.python.org/3/library/email.examples.html
推荐阅读
- javascript - JavaScript:所有函数都是闭包吗?
- python - 是否可以使用 python 在 apache kafka 中重用消费者
- python - pandas - 从 pd.Series 写一列比行行长的 csv
- excel - VBA 如何执行输入验证?
- snowflake-cloud-data-platform - 我可以让 Snowflake 从所有 CSV 列和第一行作为标题创建阶段吗?
- javascript - Mongodb .find() 结果相反
- reactjs - 如何控制地图中的多个输入对象值
- azure - 无法访问安装在 Azure ML 中的 python 包
- bash - 如何从用户那里获取符号来格式化模式
- r - 使用 randomForest 但说不能处理超过 53 个类别错误