# Setting File Type for Emailed Attachments

**URL:** <https://discuss.python.org/t/setting-file-type-for-emailed-attachments/46443>\
**Category:** Python Help\
**Created:** [February 20, 2024, 3:40pm UTC](https://discuss.python.org/t/setting-file-type-for-emailed-attachments/46443 "2024-02-20T15:40:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakomesco](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@Jakomesco](https://discuss.python.org/u/Jakomesco)\
**Post date:** [February 20, 2024, 3:40pm UTC](https://discuss.python.org/t/setting-file-type-for-emailed-attachments/46443/1 "2024-02-20T15:40:01Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/5/6/56d327e80721e2ed994544ae4fd5c4d51f8c3dc4.png)

```python
# 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)

```

---

<div class="post-metadata">

**Author:** ![moosemaimer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/moosemaimer/32/15091_2.png) [@moosemaimer](https://discuss.python.org/u/moosemaimer)\
**Post date:** [February 20, 2024, 8:05pm UTC](https://discuss.python.org/t/setting-file-type-for-emailed-attachments/46443/2 "2024-02-20T20:05:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jakomesco](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@Jakomesco](https://discuss.python.org/u/Jakomesco)\
**Post date:** [February 21, 2024, 11:36pm UTC](https://discuss.python.org/t/setting-file-type-for-emailed-attachments/46443/3 "2024-02-21T23:36:21Z")

</div>

> [@moosemaimer](#):
>
> `msg.attach(part)`

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)
