简体   繁体   English

AttributeError:通过 Python 发送 Excel 附件时,“列表”没有属性“编码”

[英]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.

相关问题 AttributeError: 'list' object 在发送 email 时没有属性 'encode' - AttributeError: 'list' object has no attribute 'encode' when sending an email AttributeError: 'list' object 在 Python 上没有属性 'encode' - AttributeError: 'list' object has no attribute 'encode' on Python AttributeError:&#39;list&#39;对象没有属性&#39;encode&#39; - AttributeError: 'list' object has no attribute 'encode' Python,AttributeError:&#39;float&#39;对象没有属性&#39;encode&#39; - Python, AttributeError: 'float' object has no attribute 'encode' AttributeError: &#39;tuple&#39; 对象没有属性 &#39;encode&#39; python - AttributeError: 'tuple' object has no attribute 'encode' python AttributeError:通过 setup.py 脚本安装 Python 包时,“list”对象没有属性“split” - AttributeError: 'list' object has no attribute 'split' when installing a Python package via a setup.py script Python AttributeError:“ tuple”对象在hashlib.encode中没有属性“ encode” - Python AttributeError: 'tuple' object has no attribute 'encode' in hashlib.encode Python AttributeError:列表中的“ dict”对象时,“ dict”对象没有属性“ startswith” - Python AttributeError: 'dict' object has no attribute 'startswith' when 'dict' in a list 谁能解释这个错误? AttributeError:“列表”对象没有属性“编码” - Can anyone explain this error? AttributeError: 'list' object has no attribute 'encode' 如何修复 AttributeError: &#39;list&#39; 对象没有属性 &#39;encode&#39; - How to fix AttributeError: 'list' object has no attribute 'encode'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM