我没有收到附件

问题描述

我正在通过Python中的smtplib发送一封电子邮件,附件中带有xlsx文件。我可以在Gmail网站中打开附件,但在Thunderbird中不会显示文件

我需要使用Thunderbird,因为它是我正在工作的客户端的首选工具。

import smtplib,ssl
from email import encoders
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase


def enviar_email(email):
    smtp_server = 'smtp.gmail.com'
    port = 465
    sender = 'proex.crim@gmail.com'
    password = '**********'
    receiver = email
    context = ssl.create_default_context()
    message = MIMEMultipart('alternative')
    message['Subject'] = 'Pesquisa finalizada'
    message['From'] = sender
    message['To'] = receiver
    filename = f'C:\\Users\\evand\\OneDrive\\Desktop\\projeto\\core\\funcoes\\{email}.xlsx'



    with open(filename,'rb') as attachment:
        part_a = MIMEBase('application','octet-stram')
        part_a.set_payload(attachment.read())
        filename = 'Resultado da Pesquisa.xlsx'

    encoders.encode_base64(part_a)

    text = f"""\
        Olá,segue em anexo o resultado da pesquisa solicitada no nosso site. 
        Enviamos para você um arquivo com os processos correspondentes aos CNPJs pesquisados.
    
    
    """

    html = """\
        <html>
            <body>
            <p> Olá,segue em anexo o resultado da pesquisa solicitada no nosso site. <br>
            </p>
            <p> Enviamos para você um arquivo com os processos correspondentes aos CNPJs pesquisados.</p>
        </body>
    </html>
    
    """

    part1 = MIMEText(text,'plain')
    part2 = MIMEText(html,'html')

    part_a.add_header(
        'Content-disposition',f'attachment; filename= {filename}',)

    message.attach(part1)
    message.attach(part2)
    message.attach(part_a)

    context = ssl.create_default_context()

    with smtplib.SMTP_SSL(smtp_server,port,context=context) as server:
        server.login(sender,password)
        server.sendmail(sender,receiver,message.as_string())

enter image description here

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)