我試圖將 2 個檔案附加到一封電子郵件,但它失敗了 - raise TypeError("set_content not valid on multipart") TypeError: set_content not valid on multipart
代碼 :
date_string = f'{datetime.now():%Y-%m-%dT%H_%M_%S%z}'
def send_mail_with_excel(recipient_email, subject, content, date_time):
input_file = "C:\\scripts\\mail_send\\remediated_data_" date_time ".csv"
msg = EmailMessage()
msg['Subject'] = subject
msg['From'] = SENDER_EMAIL
msg['To'] = recipient_email
msg.set_content(content)
with open(input_file, 'rb') as f:
file_data = f.read()
msg.add_attachment(file_data, maintype="application", subtype="csv", filename=input_file)
multiAbort_file = "C:\\scripts\\mail_send\\multi_aborts_data_" date_time ".csv"
if os.path.isfile(multiAbort_file):
with open(multiAbort_file, 'rb') as f:
file_data = f.read()
msg.add_attachment(file_data, maintype="application", subtype="csv", filename=multiAbort_file)
msg.set_content(content "\n Note : there is another list of items that are aborted multiple times in multi_aborts file, please restart these to continue")
with smtplib.SMTP('smtp-mail.outlook.com', 587) as smtp:
smtp.starttls()
smtp.login(SENDER_EMAIL, APP_PASSWORD)
smtp.send_message(msg)
send_mail_with_excel(["[email protected]"], "Remediated List of items" date_string,
"Remediated List of items " date_string, date_string)
陣型有什么問題?
uj5u.com熱心網友回復:
您應該content在致電之前進行全面評估set_content- 不要致電set_content兩次,也不要在添加附件后致電。
因此,將部分代碼更改為如下所示:
multiAbort_file = "C:\\scripts\\mail_send\\multi_aborts_data_" date_time ".csv"
has_abort = os.path.isfile(multiAbort_file)
if has_abort:
content = content "\n Note : there is another list of items that are aborted multiple times in multi_aborts file, please restart these to continue"
msg.set_content(content)
with open(input_file, 'rb') as f:
file_data = f.read()
msg.add_attachment(file_data, maintype="application", subtype="csv", filename=input_file)
if has_abort:
with open(multiAbort_file, 'rb') as f:
file_data = f.read()
msg.add_attachment(file_data, maintype="application", subtype="csv", filename=multiAbort_file)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/362034.html
