Home > Software design >  Python list attribute error without a list
Python list attribute error without a list

Time:06-26

receiver_email = open(subscribers.txt, 'r').readlines()
for i in receiver_email:
    current_mail = i.removesuffix("\n")
    print(type(current_mail))
    print(type(smtp_data[2]))
    print(type(message))
    smtp.sendmail(smtp_data[2], current_mail, message.as_string())

this previous code throws out an attribute error 'list' object has no attribute 'encode' on last line the print statements yield out the following:

<class 'str'>
<class 'str'>
<class 'email.mime.multipart.MIMEMultipart'>

Any ideas on why this isn't working?

edit: here is an example of the contents of the txt file

[email protected]
[email protected]
[email protected]

fulltraceback:

Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2022.8.0\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2022.8.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
    run()
  File "c:\Users\Administrator\.vscode\extensions\ms-python.python-2022.8.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 269, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\Administrator\Desktop\smtp.py", line 62, in <module>
    smtp.sendmail(smtp_data[2], current_mail, message.as_string())
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\message.py", line 158, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\generator.py", line 116, in flatten
    self._write(msg)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\generator.py", line 199, in _write
    self._write_headers(msg)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\generator.py", line 226, in _write_headers
    self.write(self.policy.fold(h, v))
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\_policybase.py", line 326, in fold
    return self._fold(name, value, sanitize=True)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\email\_policybase.py", line 369, in _fold
    parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))
AttributeError: 'list' object has no attribute 'encode'

CodePudding user response:

See below code - you can write it as UTF-8 in current_mail.encode('utf-8')

enter image description here

CodePudding user response:

It turned out that the message.as_string messes up when I am using a list of emails for some reason, I changed it from attaching the email context to a mimetext to directly setting it to mimetext and it seemed to do the trick.

Huge thanks to Barmar

  • Related