腳本正在運行,我收到了 zip 檔案。但是當我嘗試打開 zip 檔案(WinRaR 和 7-zip)時,我收到錯誤 - 存檔格式未知或已損壞。也許與編碼有關的問題???
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()
uj5u.com熱心網友回復:
打開 zip 檔案時不要傳遞編碼。那是二進制資料,應該這樣對待!open(..., 'b')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486451.html
