Skip to content

Commit

Permalink
Add headers to email attachments
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bprimc committed May 4, 2023
1 parent 7e1dcb9 commit 8bb1e60
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 5 additions & 1 deletion simplegmail/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import base64 # for base64.urlsafe_b64decode
import os # for os.path.exists
from typing import Optional
from typing import Optional, List, Dict

class Attachment(object):
"""
Expand All @@ -21,6 +21,7 @@ class should not be manually instantiated.
att_id: The id of the attachment.
filename: The filename associated with the attachment.
filetype: The mime type of the file.
headers: The headers of the attachment.
data: The raw data of the file. Default None.
Attributes:
Expand All @@ -30,6 +31,7 @@ class should not be manually instantiated.
id (str): The id of the attachment.
filename (str): The filename associated with the attachment.
filetype (str): The mime type of the file.
headers: The headers of the attachment.
data (bytes): The raw data of the file.
"""
Expand All @@ -42,6 +44,7 @@ def __init__(
att_id: str,
filename: str,
filetype: str,
headers: Optional[List[Dict]],
data: Optional[bytes] = None
) -> None:
self._service = service
Expand All @@ -50,6 +53,7 @@ def __init__(
self.id = att_id
self.filename = filename
self.filetype = filetype
self.headers = headers or []
self.data = data

def download(self) -> None:
Expand Down
3 changes: 2 additions & 1 deletion simplegmail/gmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ def _build_message_from_ref(
elif part['part_type'] == 'attachment':
attm = Attachment(self.service, user_id, msg_id,
part['attachment_id'], part['filename'],
part['filetype'], part['data'])
part['filetype'], part['headers'], part['data'])
attms.append(attm)

return Message(
Expand Down Expand Up @@ -892,6 +892,7 @@ def _evaluate_message_payload(
'filetype': payload['mimeType'],
'filename': filename,
'attachment_id': att_id,
'headers': payload['headers'],
'data': None
}

Expand Down

0 comments on commit 8bb1e60

Please sign in to comment.