Home > Software design >  Sending email with zip file in Python
Sending email with zip file in Python

Time:06-02

Script is working, i received zip file. But when i am trying to open zip file (WinRaR and 7-zip) i get error - archive is either in unknown format or damaged. Maybe problem related with encoding ???

from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
import smtplib


def send_mail():
    sender = '*****@mail.ru'
    password = '*************'

    smtp_obj = smtplib.SMTP('smtp.mail.ru', 587)
    smtp_obj.starttls()
    
    msg = MIMEMultipart()
    msg['Subject'] = 'Data from Experiment'
    msg['From'] = sender
    msg['To'] = sender
    
    with open(r'C:\Users\Admin\Desktop\Python\Python   SQL\Send_email\arch\arch.zip',encoding='CP866') as file:
    # Attach the file with filename to the email
        msg.attach(MIMEApplication(file.read(), Name='arch.zip'))

    

    # Login to the server
    smtp_obj.login(sender, password)

    # Convert the message to a string and send it
    smtp_obj.sendmail(sender, sender, msg.as_string())
    #smtp_obj.quit()

send_mail()

CodePudding user response:

Do not pass an encoding when opening a zip file. That's binary data and should be treated as such! open(..., 'b')

  • Related