Skip to content

Commit 27227be

Browse files
authored
Add support for sending raw MIME messages (#243)
This PR adds support for sending raw MIME messages.
1 parent a1e3a73 commit 27227be

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
nylas-python Changelog
22
======================
33

4+
Unreleased
5+
----------------
6+
* Add support for sending raw MIME messages
7+
48
v5.11.0
59
----------------
610
* Add support for calendar colors (for Microsoft calendars)

nylas/client/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
Component,
3434
JobStatus,
3535
Webhook,
36+
Send,
3637
)
3738
from nylas.client.neural_api_models import Neural
3839
from nylas.client.scheduler_restful_model_collection import (
@@ -640,6 +641,11 @@ def _create_resource(self, cls, data, **kwargs):
640641

641642
if cls == File:
642643
response = self._request(HttpMethod.POST, url, cls=cls, files=data)
644+
elif cls == Send and type(data) is not dict:
645+
headers = {"Content-Type": "message/rfc822"}
646+
response = self._request(
647+
HttpMethod.POST, url, cls=cls, headers=headers, data=data
648+
)
643649
else:
644650
converted_data = create_request_body(data, cls.datetime_attrs)
645651
headers = {"Content-Type": "application/json"}

nylas/client/restful_models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,18 @@ def detach(self, file):
514514
if file.id in self.file_ids:
515515
self.file_ids.remove(file.id)
516516

517+
def send_raw(self, mime_message):
518+
"""
519+
Send a raw MIME message
520+
521+
Args:
522+
mime_message (str): The raw MIME message to send
523+
524+
Returns:
525+
Message: The sent message
526+
"""
527+
return self.api._create_resource(Send, mime_message)
528+
517529
def send(self):
518530
if not self.id:
519531
data = self.as_json()

tests/conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,42 @@ def callback(request):
738738
)
739739

740740

741+
@pytest.fixture
742+
def mock_draft_raw_response(mocked_responses, api_url):
743+
body = {
744+
"bcc": [],
745+
"body": "",
746+
"cc": [],
747+
"date": 1438684486,
748+
"events": [],
749+
"files": [],
750+
"folder": None,
751+
"from": [{"email": "[email protected]"}],
752+
"id": "2h111aefv8pzwzfykrn7hercj",
753+
"namespace_id": "384uhp3aj8l7rpmv9s2y2rukn",
754+
"object": "draft",
755+
"reply_to": [],
756+
"reply_to_message_id": None,
757+
"snippet": "",
758+
"starred": False,
759+
"subject": "Stay polish, stay hungary",
760+
"thread_id": "clm33kapdxkposgltof845v9s",
761+
"to": [{"email": "[email protected]", "name": "Helena Handbasket"}],
762+
"unread": False,
763+
"version": 0,
764+
}
765+
766+
def callback(request):
767+
return 200, {}, json.dumps(body)
768+
769+
mocked_responses.add_callback(
770+
responses.POST,
771+
api_url + "/send",
772+
callback=callback,
773+
content_type="application/json",
774+
)
775+
776+
741777
@pytest.fixture
742778
def mock_draft_send_unsaved_response(mocked_responses, api_url):
743779
def callback(request):

tests/test_drafts.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ def test_send_draft_with_tracking(mocked_responses, api_client):
8686
assert send_payload["tracking"] == tracking
8787

8888

89+
@pytest.mark.usefixtures("mock_draft_raw_response")
90+
def test_send_draft_raw_mime(mocked_responses, api_client):
91+
raw_mime = """MIME-Version: 1.0
92+
Content-Type: text/plain; charset=UTF-8
93+
Subject: With Love, From Nylas
94+
From: You <[email protected]>
95+
To: My Nylas Friend <[email protected]>
96+
97+
This email was sent via raw MIME using the Nylas email API. Visit https://nylas.com for details.
98+
"""
99+
draft = api_client.drafts.create()
100+
draft.send_raw(raw_mime)
101+
102+
send_payload = mocked_responses.calls[-1].request
103+
assert type(send_payload.body) == str
104+
assert send_payload.body == raw_mime
105+
assert send_payload.path_url == "/send"
106+
assert send_payload.method == "POST"
107+
assert send_payload.headers["Content-Type"] == "message/rfc822"
108+
109+
89110
@pytest.mark.usefixtures("mock_files")
90111
def test_draft_attachment(api_client):
91112
draft = api_client.drafts.create()

0 commit comments

Comments
 (0)