我正在尝试附加一个pdf并用python发送电子邮件。 低于错误-
类型错误: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
更改为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