[英]AttributeError: 'list' has no attribute 'encode' when sending Excel attachment via Python
My code is as below.我的代码如下。 The "attachment" variable is returning a static file name for testing "excel/test.xlsx" “附件”变量返回用于测试“excel/test.xlsx”的 static 文件名
def send_email(snapshot_id, email):
logging.info("Sending_email for snapshot_id %d to user %s", snapshot_id, email)
attachment = get_attachment(snapshot_id, email)
logging.info("Sending attachment %s", attachment)
message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender
message["To"] = email
message['Date'] = formatdate(localtime=True)
# write the plain text part
text = """\
This is an automated email from the ARIEL reporting database, detailing key changes to Regulatory Tracking data for products, markets and categories you have subscribed to.
"""
# write the HTML part
html = """\
<html>
<body>
<p>Hi,<br>
Check out the changes!!</p>
<p> Feel free to <strong>let us</strong> know what content would be useful for you!</p>
</body>
</html>
"""
# convert both parts to MIMEText objects and add them to the MIMEMultipart message
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)
part3 = MIMEBase('application', "octet-stream")
part3.set_payload(open(attachment, "rb").read())
encoders.encode_base64(part3)
part3.add_header('Content-Disposition', 'attachment; filename="' + attachment + '"')
message.attach(part3)
# send your email
with smtplib.SMTP(smtp_server, port) as server:
server.sendmail(
sender, email, message.as_string()
)
I am getting this error message:我收到此错误消息:
AttributeError: 'list' object has no attribute 'encode'
Exception occurred
Traceback (most recent call last):
File "C:/ariel_deltas/main.py", line 263, in <module>
send_emails(snapshot_id)
File "C:\ariel_deltas\emails.py", line 25, in send_emails
send_email(snapshot_id, emails)
File "C:\ariel_deltas\emails.py", line 73, in send_email
sender, email, message.as_string()
File "C:\ProgramData\Anaconda3\envs\ariel\lib\email\message.py", line 158, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:\ProgramData\Anaconda3\envs\ariel\lib\email\generator.py", line 116, in flatten
self._write(msg)
File "C:\ProgramData\Anaconda3\envs\ariel\lib\email\generator.py", line 195, in _write
self._write_headers(msg)
File "C:\ProgramData\Anaconda3\envs\ariel\lib\email\generator.py", line 222, in _write_headers
self.write(self.policy.fold(h, v))
File "C:\ProgramData\Anaconda3\envs\ariel\lib\email\_policybase.py", line 326, in fold
return self._fold(name, value, sanitize=True)
File "C:\ProgramData\Anaconda3\envs\ariel\lib\email\_policybase.py", line 369, in _fold
parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))
Check the line检查线路
message["To"] = email
You may be setting the To
header to a list of strings instead of a string.您可能将To
header 设置为字符串列表而不是字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.