0
votes

I am trying to build a program that automatically send email using python with attachment. The recipient list are in the separate .txt file with more than 1 recipient (separated by line). When I run the code below, it shows error 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')
1
please post the complete error along with the line number it is hitting the errorKrishna Chaurasia

1 Answers

0
votes

email.mime.multipart.MIMEMultipart doesn't accept To attribute as list. 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'] = ['[email protected]', '[email protected]']
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(['[email protected]', '[email protected]'])
message.as_string()
'Content-Type: multipart/mixed; boundary="===============1635559805384717632=="\nMIME-Version: 1.0\nTo: [email protected], [email protected]\n\n--===============1635559805384717632==\n\n--===============1635559805384717632==--\n'