简体   繁体   English

AttributeError: 'list' object 在 Python 上没有属性 'encode'

[英]AttributeError: 'list' object has no attribute 'encode' on Python

I am trying to build a program that automatically send email using python with attachment.我正在尝试构建一个程序,使用带附件的 python 自动发送 email。 The recipient list are in the separate.txt file with more than 1 recipient (separated by line).收件人列表位于 separate.txt 文件中,其中包含超过 1 个收件人(以行分隔)。 When I run the code below, it shows error AttributeError: 'list' object has no attribute 'encode'当我运行下面的代码时,它显示错误 AttributeError: 'list' object has no attribute 'encode'

import email, smtplib, ssl, getpass, stdiomask

from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#getting sender email
sender_email = input("Please input email: ")
password = stdiomask.getpass("Input your password: ")
mail_content = "Hi this sent from Python"

#read recipient list
f = open("receiver_list.txt", "r+")
receiver_email = [i.strip() for i in f.readlines()]

message = MIMEMultipart()
message ['From'] = sender_email
message ['To'] = receiver_email
message ['Subject'] = 'Python Email'
message.attach(MIMEText(mail_content, 'plain'))
attach_file_name = 'Final Project - Basic Python.pdf'
attach_file = open(attach_file_name, 'rb')
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload)
payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
message.attach(payload)


#log in server
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    text = message.as_string()
    server.sendmail(sender_email, receiver_email, text)
print('Mail Sent')

email.mime.multipart.MIMEMultipart doesn't accept To attribute as list. email.mime.multipart.MIMEMultipart不接受To属性作为列表。 You must join you recipients as a string, separated by comma.您必须将收件人作为字符串加入,以逗号分隔。

In your example:在你的例子中:

message ['To'] = ', '.join(receiver_email)

This reproduces your error:这重现了您的错误:

error_message = MIMEMultipart()
error_message['To'] = ['myemail@mail.com', 'other@mail.com']
error_message.as_string()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\Nitzam\AppData\Local\Programs\Python\Python38\lib\email\message.py", line 158, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "C:\Users\Nitzam\AppData\Local\Programs\Python\Python38\lib\email\generator.py", line 116, in flatten
    self._write(msg)
  File "C:\Users\Nitzam\AppData\Local\Programs\Python\Python38\lib\email\generator.py", line 195, in _write
    self._write_headers(msg)
  File "C:\Users\Nitzam\AppData\Local\Programs\Python\Python38\lib\email\generator.py", line 222, in _write_headers
    self.write(self.policy.fold(h, v))
  File "C:\Users\Nitzam\AppData\Local\Programs\Python\Python38\lib\email\_policybase.py", line 326, in fold
    return self._fold(name, value, sanitize=True)
  File "C:\Users\Nitzam\AppData\Local\Programs\Python\Python38\lib\email\_policybase.py", line 369, in _fold
    parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))
AttributeError: 'list' object has no attribute 'encode'

While here it is fixed:虽然在这里它是固定的:

message = MIMEMultipart()
message['To'] = ', '.join(['myemail@mail.com', 'other@mail.com'])
message.as_string()
'Content-Type: multipart/mixed; boundary="===============1635559805384717632=="\nMIME-Version: 1.0\nTo: myemail@mail.com, other@mail.com\n\n--===============1635559805384717632==\n\n--===============1635559805384717632==--\n'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 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 Python AttributeError:“ tuple”对象在hashlib.encode中没有属性“ encode” - Python AttributeError: 'tuple' object has no attribute 'encode' in hashlib.encode 谁能解释这个错误? 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' AttributeError: 'list' object 在发送 email 时没有属性 'encode' - AttributeError: 'list' object has no attribute 'encode' when sending an email 如何解决AttributeError:&#39;NoneType&#39;对象在python中没有属性&#39;encode&#39; - How to resolve AttributeError: 'NoneType' object has no attribute 'encode' in python AttributeError:&#39;tuple&#39;对象没有属性&#39;encode&#39;python gui - AttributeError: 'tuple' object has no attribute 'encode' python gui AttributeError: 'tuple' object 没有属性 'encode' - MySQL.connector.Python - AttributeError: 'tuple' object has no attribute 'encode' - MySQL.connector.Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM