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