简体   繁体   English

如何修复 AttributeError: 'list' 对象没有属性 'encode'

[英]How to fix AttributeError: 'list' object has no attribute 'encode'

I'm trying to send a mail with an html attachment which contains a table from a Pandas dataframe and some plotted images in Python 3.7.我正在尝试发送带有 html 附件的邮件,其中包含来自 Pandas 数据帧的表格和 Python 3.7 中的一些绘制图像。 When the data in the provided dataframe is not null and therefore there are plotted images to send, i wont run into the error.当提供的数据帧中的数据不为空并且因此有绘制的图像要发送时,我不会遇到错误。 I also collect occuring errors in a list and add them to the final string which i want to sent with the mail.我还在列表中收集发生的错误,并将它们添加到我想与邮件一起发送的最终字符串中。

def mailMe():

    # Create the container email message.
    msg = EmailMessage()
    msg['Subject'] = 'Mail'
    msg['From'] = sender
    msg['To'] = receiver

    #style overview table
    htmlTable = (
        df.style
        .set_table_styles(styles)
        .applymap(color_negative_red)
        .set_caption('Auswertung')
        .render()
    )

    #attach 100 dpi images
    if not len(pngfiles100)==0:
        for file in pngfiles100:
            try:
                with open(r'.\\temp\\'+file, 'rb') as fp:
                    img_data = fp.read()

                msg.add_attachment(img_data, maintype='image',
                                            subtype=imghdr.what(None, img_data),
                                            filename=file)
            except:
                continue

            fp.close()        

    #Error messages
    htmlTable+='\n'+'<p style="font-family:Arial;"><b>Errors:</b></p>'
    if not len(errors)==0:
        for i in errors:
            htmlTable+='\n'+'<p style="font-family:Arial;">'+i+'</p>'


    pngFolder=r'.\\temp\\' 
    htmlTable+=""" 
    <table>
    <tbody>
    """

    i=0
    for col in df.columns:
        tempDf=pd.DataFrame(data=df[col])
        tmpHtml = (
            tempDf.style
            .set_table_styles(styles)
            .applymap(color_negative_red)
            .render()
            )
        if not len(pngfiles50)==0:
            #embed 50 dpi images in mail
            htmlTable+=u'\n'+'<tr><td>'+tmpHtml+'</td>'+'<td><img src="cid:image'+str(i+1)+'"></td></tr>'+u'\n'
            try:
                fp=open(pngFolder+pngfiles50[i],'rb')
                msgImage = MIMEImage(fp.read())
                fp.close()
                msgImage.add_header('Content-ID','<image'+str(i+1)+'>')
                msg.attach(msgImage)
            except:
                continue
            i+=1

    htmlTable+="""
    </tbody>
    </table>
    """

    # Send the email via our own SMTP server.
    with smtplib.SMTP(host) as s:
        htmlTable = MIMEText(htmlTable,"html")
        msg.attach(htmlTable)
        s.send_message(msg)

Traceback:追溯:

Traceback (most recent call last):
  File "c:\Users\mmai\.vscode\extensions\ms-python.python-2019.8.30787\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\mmai\.vscode\extensions\ms-python.python-2019.8.30787\pythonFiles\lib\python\ptvsd\__main__.py", line 432, in main
    run()
  File "c:\Users\mmai\.vscode\extensions\ms-python.python-2019.8.30787\pythonFiles\lib\python\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Users\mmai\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\mmai\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\mmai\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\mmai\Documents\Python\Mail.py", line 1043, in <module>
    OEM()
  File "c:\Users\mmai\Documents\Python\Mail.py", line 168, in __init__
    self.mailMe()
  File "c:\Users\mmai\Documents\Python\Mail.py", line 1035, in mailMe
    s.send_message(msg)
  File "C:\Users\mmai\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 964, in send_message
    g.flatten(msg_copy, linesep='\r\n')
  File "C:\Users\mmai\AppData\Local\Programs\Python\Python37-32\lib\email\generator.py", line 116, in flatten
    self._write(msg)
  File "C:\Users\mmai\AppData\Local\Programs\Python\Python37-32\lib\email\generator.py", line 181, in _write
    self._dispatch(msg)
  File "C:\Users\mmai\AppData\Local\Programs\Python\Python37-32\lib\email\generator.py", line 214, in _dispatch
    meth(msg)
  File "C:\Users\mmai\AppData\Local\Programs\Python\Python37-32\lib\email\generator.py", line 427, in _handle_text
    if _has_surrogates(msg._payload) and not self.policy.cte_type=='7bit':
  File "C:\Users\mmai\AppData\Local\Programs\Python\Python37-32\lib\email\utils.py", line 57, in _has_surrogates
    s.encode()
AttributeError: 'list' object has no attribute 'encode'

It seems there is a problem with s.send_message(msg) but i can't wrap my head around it. s.send_message(msg) 似乎有问题,但我无法理解它。 Sender and receiver are both a string and not a list.发送方和接收方都是字符串而不是列表。

Solved it by adding msg.add_attachment(htmlTable)通过添加msg.add_attachment(htmlTable)解决它

这可能会帮助您:

"".join(s).encode()  

if receiver is a list , then use如果接收者是一个列表,则使用

for i in range(len(receiver)):
            s.sendmail(message['From'], receiver[i], message.as_string())

暂无
暂无

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

相关问题 如何修复 AttributeError:“NoneType”object 使用 smtplib 没有属性“encode” - How to fix AttributeError: "NoneType" object has no attribute "encode" using smtplib 如何修复 AttributeError: &#39;bytes&#39; object has no attribute &#39;encode&#39;? - How do I fix AttributeError: 'bytes' object has no attribute 'encode'? AttributeError: 'list' object 在 Python 上没有属性 'encode' - AttributeError: 'list' object has no attribute 'encode' on Python AttributeError:&#39;list&#39;对象没有属性&#39;encode&#39; - AttributeError: 'list' object has no attribute 'encode' 如何修复“列表”object 在 Python/Django 中没有属性“get”(AttributeError) - How to fix 'list' object has no attribute 'get' (AttributeError) in Python/Django 如何修复 AttributeError: 'NoneType' object 在 setting.py 中没有属性 'encode' Django - How to fix AttributeError: 'NoneType' object has no attribute 'encode' in setting.py Django 谁能解释这个错误? AttributeError:“列表”对象没有属性“编码” - Can anyone explain this error? AttributeError: 'list' object has no attribute 'encode' AttributeError: 'list' object 在发送 email 时没有属性 'encode' - AttributeError: 'list' object has no attribute 'encode' when sending an email 如何修复:AttributeError:Class对象没有属性 - How to fix: AttributeError:Class object has no attribute AttributeError: 'list' object 没有属性 'encode'。 我如何通过 Excel 发送 Email? Python - AttributeError: 'list' object has no attribute 'encode'. How do i send Email via Excel? Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM