Setting File Type for Emailed Attachments

A script for sending email with attachments is working, but all attachments, if any, are arriving as .eml files rather than as jpg, pdf, txt, etc. They do open correctly, but I would prefer that the bear their actual name and type. Although I’ve made minor changes for Python 3, it did work for me on Python 2.7.

Also, I keep coming across references that set_payload has been deprecated, but I can’t see what to use instead.

Pasted Graphic

# Attach any files
files = '''/Users/Mine/Desktop/RKw.jpeg\n/Users/Mine/Desktop/PG_2022.pdf'''.splitlines()
for file in files:
	attachment = open(file, "rb")
	part = MIMEBase("application", "octet-stream")
	part.set_payload((attachment).read())
	encoders.encode_base64(part)
	part.add_header("Content-Disposition", f"attachment; filename= {file.split('/')[-1]}")
	msg.add_attachment(part)

Looking at a very old script I ported to Python3, it’s mostly the same as what you’re doing except replace the last line with msg.attach(part) and see if that works.

That’s what I used in my Python 2.7 version, but on 3.9 it gets errors that I corrected with msg.add_attachment(part)