Skip to content

Commit 8bb1e60

Browse files
committed
Add headers to email attachments
Example usage: def find_attachment_by_cid(cid: str) -> Attachment | None: for att in message.attachments: for header in att.headers: if header['name'] == 'Content-ID' and header['value'] == f'<{cid}>': return att Signed-off-by: Blaz Primc <[email protected]>
1 parent 7e1dcb9 commit 8bb1e60

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

simplegmail/attachment.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import base64 # for base64.urlsafe_b64decode
99
import os # for os.path.exists
10-
from typing import Optional
10+
from typing import Optional, List, Dict
1111

1212
class Attachment(object):
1313
"""
@@ -21,6 +21,7 @@ class should not be manually instantiated.
2121
att_id: The id of the attachment.
2222
filename: The filename associated with the attachment.
2323
filetype: The mime type of the file.
24+
headers: The headers of the attachment.
2425
data: The raw data of the file. Default None.
2526
2627
Attributes:
@@ -30,6 +31,7 @@ class should not be manually instantiated.
3031
id (str): The id of the attachment.
3132
filename (str): The filename associated with the attachment.
3233
filetype (str): The mime type of the file.
34+
headers: The headers of the attachment.
3335
data (bytes): The raw data of the file.
3436
3537
"""
@@ -42,6 +44,7 @@ def __init__(
4244
att_id: str,
4345
filename: str,
4446
filetype: str,
47+
headers: Optional[List[Dict]],
4548
data: Optional[bytes] = None
4649
) -> None:
4750
self._service = service
@@ -50,6 +53,7 @@ def __init__(
5053
self.id = att_id
5154
self.filename = filename
5255
self.filetype = filetype
56+
self.headers = headers or []
5357
self.data = data
5458

5559
def download(self) -> None:

simplegmail/gmail.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ def _build_message_from_ref(
826826
elif part['part_type'] == 'attachment':
827827
attm = Attachment(self.service, user_id, msg_id,
828828
part['attachment_id'], part['filename'],
829-
part['filetype'], part['data'])
829+
part['filetype'], part['headers'], part['data'])
830830
attms.append(attm)
831831

832832
return Message(
@@ -892,6 +892,7 @@ def _evaluate_message_payload(
892892
'filetype': payload['mimeType'],
893893
'filename': filename,
894894
'attachment_id': att_id,
895+
'headers': payload['headers'],
895896
'data': None
896897
}
897898

0 commit comments

Comments
 (0)