Tôi đã tìm thấy rất nhiều ví dụ trên mạng về cách đính kèm tệp cục bộ vào email. Những gì tôi muốn làm là đính kèm một tập tin như đối tượng vào một email. Lý do tại sao bạn hỏi? vì vậy tôi không phải xử lý việc dọn dẹp các tập tin. Dưới đây là mã của tôi và lỗi của tôi. Sau nhiều googling tôi vẫn chưa được quản lý để có được nó để làm việc, bất kỳ trợ giúp sẽ được đánh giá cao :)đính kèm tệp như đối tượng vào email python 3
def email_sup_teams(team_name, contact_list, file_attachemnt):
message_list = []
for jobs in file_attachemnt:
for k, v in jobs.items():
message_list.append(v + ',')
attachment_text = "\n".join(message_list)
print(type(attachment_text))
msg = MIMEText(' Failed jobs list. Please see attachment')
msg['Subject'] = 'Not run Jobs for ' + team_name
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
f = io.StringIO(attachment_text)
attachment = MIMEText(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename='test_attach')
msg.attach(attachment)
s = smtplib.SMTP('smlsmtp')
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.quit()
print('\n' + team_name + ' Email Sent')
lỗi:
<class 'str'>
Traceback (most recent call last):
File "queue_cleaner_main.py", line 85, in <module>
sys.exit(main())
File "queue_cleaner_main.py", line 82, in main
queue_cleaner_functions.email_sup_teams(t, team_members_emails, attachment_file_of_jobs)
File "D:\oppssup\old_job\queue_cleaner_functions.py", line 179, in email_sup_teams
msg.attach(attachment)
File "C:\Python34\lib\email\mime\nonmultipart.py", line 22, in attach
'Cannot attach additional subparts to non-multipart/*')
email.errors.MultipartConversionError: Cannot attach additional subparts to non-multipart/*