Skip to content

Commit ea33238

Browse files
committed
feat: inline attachment support
1 parent 7b114dc commit ea33238

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
3+
import resend
4+
5+
if not os.environ["RESEND_API_KEY"]:
6+
raise EnvironmentError("RESEND_API_KEY is missing")
7+
8+
f: bytes = open(
9+
os.path.join(os.path.dirname(__file__), "../resources/resend-wordmark-black.png"),
10+
"rb",
11+
).read()
12+
13+
# Define the file attachment
14+
attachment: resend.Attachment = {
15+
"filename": "resend-wordmark-black.png",
16+
"content": list(f),
17+
"content_type": "image/png",
18+
# This is the content ID that will be used in the HTML to reference the image
19+
"inline_content_id": "my-test-image",
20+
}
21+
22+
# Define the email parameters
23+
params: resend.Emails.SendParams = {
24+
"from": "onboarding@resend.dev",
25+
"to": ["delivered@resend.dev"],
26+
"subject": "Inline attachment test from Resend's python SDK",
27+
"html": '<p>This is an email with an <img width=100 height=40 src="cid:my-test-image" /> embed image</p>',
28+
"attachments": [attachment],
29+
}
30+
31+
email: resend.Email = resend.Emails.send(params)
32+
print("Sent email with attachment")
33+
print(email)

resend/emails/_attachment.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ class Attachment(TypedDict):
2323
"""
2424
Content type for the attachment, if not set will be derived from the filename property
2525
"""
26+
inline_content_id: NotRequired[str]
27+
"""
28+
Content ID for inline attachments used in HTML content with cid: references
29+
"""
24.6 KB
Loading

tests/emails_test.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,50 @@ def test_cancel_scheduled_email(self) -> None:
109109
email_id="49a3999c-0ce1-4ea6-ab68-afcd6dc2e794"
110110
)
111111
assert email["id"] == "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794"
112+
113+
def test_email_send_with_attachment(self) -> None:
114+
self.set_mock_json(
115+
{
116+
"id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794",
117+
}
118+
)
119+
120+
attachment: resend.Attachment = {
121+
"filename": "test.pdf",
122+
"content": [1, 2, 3, 4, 5],
123+
"content_type": "application/pdf",
124+
}
125+
126+
params: resend.Emails.SendParams = {
127+
"to": "to@email.com",
128+
"from": "from@email.com",
129+
"subject": "subject",
130+
"html": "html",
131+
"attachments": [attachment],
132+
}
133+
email: resend.Email = resend.Emails.send(params)
134+
assert email["id"] == "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794"
135+
136+
def test_email_send_with_inline_attachment(self) -> None:
137+
self.set_mock_json(
138+
{
139+
"id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794",
140+
}
141+
)
142+
143+
attachment: resend.Attachment = {
144+
"filename": "image.png",
145+
"content": [1, 2, 3, 4, 5],
146+
"content_type": "image/png",
147+
"inline_content_id": "my-image",
148+
}
149+
150+
params: resend.Emails.SendParams = {
151+
"to": "to@email.com",
152+
"from": "from@email.com",
153+
"subject": "subject",
154+
"html": '<img src="cid:my-image" />',
155+
"attachments": [attachment],
156+
}
157+
email: resend.Email = resend.Emails.send(params)
158+
assert email["id"] == "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794"

0 commit comments

Comments
 (0)