From 9b1526a0162707c6c8c7f28800021b3e136e20c8 Mon Sep 17 00:00:00 2001 From: Advay Pal Date: Wed, 29 Mar 2023 03:37:56 -0700 Subject: [PATCH 1/9] Add relevant headers for replying to threads --- src/ezgmail/__init__.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ezgmail/__init__.py b/src/ezgmail/__init__.py index 125d2c2..a51886d 100644 --- a/src/ezgmail/__init__.py +++ b/src/ezgmail/__init__.py @@ -413,7 +413,7 @@ def reply(self, body, attachments=None, cc=None, bcc=None, mimeSubtype="plain"): # 1. The Subject headers match # 2. The References and In-Reply-To headers follow the RFC 2822 standard. - send(self.sender, self.subject, body, attachments=attachments, cc=cc, bcc=bcc, mimeSubtype=mimeSubtype, _threadId=self.threadId) + send(self.sender, self.subject, body, attachments=attachments, cc=cc, bcc=bcc, mimeSubtype=mimeSubtype, _threadId=self.threadId, ) def replyAll(self, body, attachments=None, cc=None, bcc=None, mimeSubtype="plain"): """Like the send() function, but replies to the last message in this thread.""" @@ -476,7 +476,7 @@ def init(userId="me", tokenFile="token.json", credentialsFile="credentials.json" return False -def _createMessage(sender, recipient, subject, body, cc=None, bcc=None, mimeSubtype="plain", _threadId=None): +def _createMessage(sender, recipient, subject, body, cc=None, bcc=None, mimeSubtype="plain", _threadId=None, in_reply_to = None, references = None): """Creates a MIMEText object and returns it as a base64 encoded string in a ``{'raw': b64_MIMEText_object} `` dictionary, suitable for use by ``_sendMessage()`` and the ``users.messages.send()`` Gmail API. @@ -495,10 +495,16 @@ def _createMessage(sender, recipient, subject, body, cc=None, bcc=None, mimeSubt message["cc"] = cc if bcc is not None: message["bcc"] = bcc + if inReplyTo is not None: + message["in-reply-to"] = in_reply_to + if references is not None: + message["references"] = references rawMessage = {"raw": base64.urlsafe_b64encode(message.as_bytes()).decode("ascii")} if _threadId is not None: rawMessage['threadId'] = _threadId + if inReplyTo is not None: + rawMessage['In-Reply-To' return rawMessage From 73fa6e50d160df647ead971e769a3d8a08e183dd Mon Sep 17 00:00:00 2001 From: Advay Pal Date: Wed, 29 Mar 2023 03:45:18 -0700 Subject: [PATCH 2/9] Fix typo --- src/ezgmail/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ezgmail/__init__.py b/src/ezgmail/__init__.py index a51886d..837450d 100644 --- a/src/ezgmail/__init__.py +++ b/src/ezgmail/__init__.py @@ -503,8 +503,6 @@ def _createMessage(sender, recipient, subject, body, cc=None, bcc=None, mimeSubt rawMessage = {"raw": base64.urlsafe_b64encode(message.as_bytes()).decode("ascii")} if _threadId is not None: rawMessage['threadId'] = _threadId - if inReplyTo is not None: - rawMessage['In-Reply-To' return rawMessage From 8986f9fdddb7190718d9137aade524ed6872af3b Mon Sep 17 00:00:00 2001 From: Advay Pal Date: Wed, 29 Mar 2023 03:50:27 -0700 Subject: [PATCH 3/9] Fix typo + add more references --- src/ezgmail/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ezgmail/__init__.py b/src/ezgmail/__init__.py index 837450d..4e84d93 100644 --- a/src/ezgmail/__init__.py +++ b/src/ezgmail/__init__.py @@ -402,7 +402,7 @@ def trash(self): """Move this message to the Trash folder. It will be automatically removed in 30 days.""" _trash(self) # The global _trash() function implements this feature. - def reply(self, body, attachments=None, cc=None, bcc=None, mimeSubtype="plain"): + def reply(self, body, attachments=None, cc=None, bcc=None, mimeSubtype="plain", in_reply_to=None, references=None): """Like the send() function, but replies to the last message in this thread.""" # NOTE: Since the ``sender`` argument is ignored by Gmail anyway, I'm not including in this method the @@ -413,7 +413,7 @@ def reply(self, body, attachments=None, cc=None, bcc=None, mimeSubtype="plain"): # 1. The Subject headers match # 2. The References and In-Reply-To headers follow the RFC 2822 standard. - send(self.sender, self.subject, body, attachments=attachments, cc=cc, bcc=bcc, mimeSubtype=mimeSubtype, _threadId=self.threadId, ) + send(self.sender, self.subject, body, attachments=attachments, cc=cc, bcc=bcc, mimeSubtype=mimeSubtype, _threadId=self.threadId, in_reply_to=in_reply_to, references=references) def replyAll(self, body, attachments=None, cc=None, bcc=None, mimeSubtype="plain"): """Like the send() function, but replies to the last message in this thread.""" @@ -476,7 +476,7 @@ def init(userId="me", tokenFile="token.json", credentialsFile="credentials.json" return False -def _createMessage(sender, recipient, subject, body, cc=None, bcc=None, mimeSubtype="plain", _threadId=None, in_reply_to = None, references = None): +def _createMessage(sender, recipient, subject, body, cc=None, bcc=None, mimeSubtype="plain", _threadId=None, in_reply_to=None, references=None): """Creates a MIMEText object and returns it as a base64 encoded string in a ``{'raw': b64_MIMEText_object} `` dictionary, suitable for use by ``_sendMessage()`` and the ``users.messages.send()`` Gmail API. @@ -495,7 +495,7 @@ def _createMessage(sender, recipient, subject, body, cc=None, bcc=None, mimeSubt message["cc"] = cc if bcc is not None: message["bcc"] = bcc - if inReplyTo is not None: + if in_reply_to is not None: message["in-reply-to"] = in_reply_to if references is not None: message["references"] = references From 6add0baf184991b7b79e6359fd72572583b017f3 Mon Sep 17 00:00:00 2001 From: Advay Pal Date: Wed, 29 Mar 2023 03:52:14 -0700 Subject: [PATCH 4/9] Add print statement --- src/ezgmail/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ezgmail/__init__.py b/src/ezgmail/__init__.py index 4e84d93..3e6197c 100644 --- a/src/ezgmail/__init__.py +++ b/src/ezgmail/__init__.py @@ -477,6 +477,7 @@ def init(userId="me", tokenFile="token.json", credentialsFile="credentials.json" def _createMessage(sender, recipient, subject, body, cc=None, bcc=None, mimeSubtype="plain", _threadId=None, in_reply_to=None, references=None): + print("CREATING MESSAGE YO") """Creates a MIMEText object and returns it as a base64 encoded string in a ``{'raw': b64_MIMEText_object} `` dictionary, suitable for use by ``_sendMessage()`` and the ``users.messages.send()`` Gmail API. From 4d1fc09f495656653841addae75c59bf65971e80 Mon Sep 17 00:00:00 2001 From: Advay Pal Date: Wed, 29 Mar 2023 03:54:40 -0700 Subject: [PATCH 5/9] Add to send signature --- src/ezgmail/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ezgmail/__init__.py b/src/ezgmail/__init__.py index 3e6197c..ed50d00 100644 --- a/src/ezgmail/__init__.py +++ b/src/ezgmail/__init__.py @@ -586,7 +586,7 @@ def _sendMessage(message, userId="me"): return message -def send(recipient, subject, body, attachments=None, sender=None, cc=None, bcc=None, mimeSubtype="plain", _threadId=None): +def send(recipient, subject, body, attachments=None, sender=None, cc=None, bcc=None, mimeSubtype="plain", _threadId=None, in_reply_to=None, references=None): """Sends an email from the configured Gmail account. Note that the ``sender`` argument seems to be ignored by Gmail, which uses the account's actual email address. @@ -605,9 +605,9 @@ def send(recipient, subject, body, attachments=None, sender=None, cc=None, bcc=N sender = EMAIL_ADDRESS if attachments is None: - msg = _createMessage(sender, recipient, subject, body, cc, bcc, mimeSubtype, _threadId=_threadId) + msg = _createMessage(sender, recipient, subject, body, cc, bcc, mimeSubtype, _threadId=_threadId, in_reply_to=in_reply_to, references=references) else: - msg = _createMessageWithAttachments(sender, recipient, subject, body, attachments, cc, bcc, mimeSubtype, _threadId=_threadId) + msg = _createMessageWithAttachments(sender, recipient, subject, body, attachments, cc, bcc, mimeSubtype, _threadId=_threadId, in_reply_to=in_reply_to, references=references) _sendMessage(msg) From 88454770375cc8580bb2a81f834c9dd97fe62b82 Mon Sep 17 00:00:00 2001 From: Advay Pal Date: Wed, 29 Mar 2023 03:57:33 -0700 Subject: [PATCH 6/9] Add debug statements --- src/ezgmail/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ezgmail/__init__.py b/src/ezgmail/__init__.py index ed50d00..5c2b67f 100644 --- a/src/ezgmail/__init__.py +++ b/src/ezgmail/__init__.py @@ -477,7 +477,7 @@ def init(userId="me", tokenFile="token.json", credentialsFile="credentials.json" def _createMessage(sender, recipient, subject, body, cc=None, bcc=None, mimeSubtype="plain", _threadId=None, in_reply_to=None, references=None): - print("CREATING MESSAGE YO") + print(f"CREATING MESSAGE YO {in_reply_to} {references}") """Creates a MIMEText object and returns it as a base64 encoded string in a ``{'raw': b64_MIMEText_object} `` dictionary, suitable for use by ``_sendMessage()`` and the ``users.messages.send()`` Gmail API. @@ -586,7 +586,7 @@ def _sendMessage(message, userId="me"): return message -def send(recipient, subject, body, attachments=None, sender=None, cc=None, bcc=None, mimeSubtype="plain", _threadId=None, in_reply_to=None, references=None): +def send(recipient, subjectS, body, attachments=None, sender=None, cc=None, bcc=None, mimeSubtype="plain", _threadId=None, in_reply_to=None, references=None): """Sends an email from the configured Gmail account. Note that the ``sender`` argument seems to be ignored by Gmail, which uses the account's actual email address. From 3bc0339dc20f63f0ed0e6c874af32294cf49f819 Mon Sep 17 00:00:00 2001 From: Advay Pal Date: Wed, 29 Mar 2023 04:00:04 -0700 Subject: [PATCH 7/9] Fix typo --- src/ezgmail/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ezgmail/__init__.py b/src/ezgmail/__init__.py index 5c2b67f..f637a75 100644 --- a/src/ezgmail/__init__.py +++ b/src/ezgmail/__init__.py @@ -586,7 +586,7 @@ def _sendMessage(message, userId="me"): return message -def send(recipient, subjectS, body, attachments=None, sender=None, cc=None, bcc=None, mimeSubtype="plain", _threadId=None, in_reply_to=None, references=None): +def send(recipient, subject, body, attachments=None, sender=None, cc=None, bcc=None, mimeSubtype="plain", _threadId=None, in_reply_to=None, references=None): """Sends an email from the configured Gmail account. Note that the ``sender`` argument seems to be ignored by Gmail, which uses the account's actual email address. From 7a12c55b514ef06071512b8067d0e8fcb471d718 Mon Sep 17 00:00:00 2001 From: Advay Pal Date: Wed, 29 Mar 2023 04:30:46 -0700 Subject: [PATCH 8/9] Get more data from email --- src/ezgmail/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ezgmail/__init__.py b/src/ezgmail/__init__.py index f637a75..5ef5cae 100644 --- a/src/ezgmail/__init__.py +++ b/src/ezgmail/__init__.py @@ -227,6 +227,10 @@ def __init__(self, messageObj): if header["name"].upper() == "CONTENT-TYPE": emailEncoding = _parseContentTypeHeaderForEncoding(header["value"]) + if header["name"].upper() == "MESSAGE-ID": + self.message_id = header["value"] + if header["name"].upper() == "REFERENCES": + self.references = header["value"] # Find the plaintext email part, get the encoding, and use it to get the email body. if "parts" in messageObj["payload"].keys(): From 66f0151b2d3a1c7563722648952854da679d6df9 Mon Sep 17 00:00:00 2001 From: Advay Pal Date: Wed, 29 Mar 2023 04:36:20 -0700 Subject: [PATCH 9/9] Remove debug statements --- src/ezgmail/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ezgmail/__init__.py b/src/ezgmail/__init__.py index 5ef5cae..614d49a 100644 --- a/src/ezgmail/__init__.py +++ b/src/ezgmail/__init__.py @@ -481,7 +481,6 @@ def init(userId="me", tokenFile="token.json", credentialsFile="credentials.json" def _createMessage(sender, recipient, subject, body, cc=None, bcc=None, mimeSubtype="plain", _threadId=None, in_reply_to=None, references=None): - print(f"CREATING MESSAGE YO {in_reply_to} {references}") """Creates a MIMEText object and returns it as a base64 encoded string in a ``{'raw': b64_MIMEText_object} `` dictionary, suitable for use by ``_sendMessage()`` and the ``users.messages.send()`` Gmail API.