Tag Archives: AttributeError:’list’ object has no attribute’encode’

When sending an email, an error was reported: AttributeError:’list’ object has no attribute’encode’

An error occurred when sending emails using Tencent Enterprise Mailbox: AttributeError:’list’ object has no attribute’encode’

 

Reason: The recipient cannot store data in a list, it needs to be converted to a string, separated by commas

 

Solution:

Convert the recipient list into a string, separated by commas

msg['to'= ','.join(to_list)

 

Complete code:

import smtplib
 from email.mime.text import MIMEText
 from email.header import Header

name = ' [email protected] ' 
pwd = ' xxx ' 
to_list = [ ' [email protected] ' ]

content = ' <html><head><title>test</title></head><body>This is the test email content</body></html> ' 
msg = MIMEText(content, ' html ' , ' utf -8 ' )
msg[ ' form ' ] = Header( ' huyang ' , ' utf-8 ' )
msg[ ' to ' ] = ' , ' .join(to_list) # The focus is on this position
msg[ ' subject ' ] = Header( ' Test mail ' , ' utf-8 ' )

# ---Send 
smtp = smtplib.SMTP_SSL( ' smtp.exmail.qq.com ' , 465 )
smtp.login(name, pwd)
smtp.sendmail(name, to_list, msg.as_string())
print ( ' Send successfully! ' )