Skip to content

Commit

Permalink
examples: fix types on examples with attachments (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
drish authored Apr 22, 2024
1 parent b2357af commit 821f055
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
4 changes: 2 additions & 2 deletions examples/batch_email_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
params: List[resend.Emails.SendParams] = [
{
"sender": "[email protected]",
"to": ["[email protected]"],
"to": ["[email protected]"],
"subject": "hey",
"html": "<strong>hello, world!</strong>",
},
{
"sender": "[email protected]",
"to": ["[email protected]"],
"to": ["[email protected]"],
"subject": "hello",
"html": "<strong>hello, world!</strong>",
},
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

params: resend.Emails.SendParams = {
"sender": "[email protected]",
"to": ["[email protected]"],
"to": ["[email protected]"],
"subject": "hi",
"html": "<strong>hello, world!</strong>",
"reply_to": "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion examples/with_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Define the email parameters
params: resend.Emails.SendParams = {
"sender": "[email protected]",
"to": ["[email protected]"],
"to": ["[email protected]"],
"subject": "hi",
"html": "<strong>hello, world!</strong>",
"attachments": [attachment],
Expand Down
20 changes: 14 additions & 6 deletions examples/with_b64_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@
if not os.environ["RESEND_API_KEY"]:
raise EnvironmentError("RESEND_API_KEY is missing")

# Read file
f = open(
os.path.join(os.path.dirname(__file__), "../resources/invoice.pdf"), "rb"
).read()

b64 = base64.b64encode(f)
b64_str = b64.decode("utf-8")
# Read file bytes as a base64 string
b64: bytes = base64.b64encode(f)
b64_str: str = b64.decode("utf-8")

params = {
"from": "[email protected]",
# Create attachment object from base64 string
b64_attachment: resend.Attachment = {"filename": "invoice.pdf", "content": b64_str}

# Email params
params: resend.Emails.SendParams = {
"sender": "[email protected]",
"to": ["[email protected]"],
"subject": "hello with base64 attachments",
"html": "<strong>hello, world!</strong>",
"attachments": [{"filename": "invoice.pdf", "content": b64_str}],
"attachments": [b64_attachment],
}

# Send email
email = resend.Emails.send(params)
print(email)
print("Email sent with base64 string attachment")
print(f"Email ID: {email.id}")
20 changes: 14 additions & 6 deletions examples/with_html_file_as_b64_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@
if not os.environ["RESEND_API_KEY"]:
raise EnvironmentError("RESEND_API_KEY is missing")

# Read file
f = open(
os.path.join(os.path.dirname(__file__), "../resources/index.html"), "rb"
).read()

b64 = base64.b64encode(f)
b64_str = b64.decode("utf-8")
# Read file bytes as a base64 string
b64: bytes = base64.b64encode(f)
b64_str: str = b64.decode("utf-8")

params = {
"from": "[email protected]",
# Create attachment object from base64 string
b64_attachment: resend.Attachment = {"filename": "file.html", "content": b64_str}

# Email params
params: resend.Emails.SendParams = {
"sender": "[email protected]",
"to": ["[email protected]"],
"subject": "hello with base64 attachments",
"html": "<strong>hello, world!</strong>",
"attachments": [{"filename": "file.html", "content": b64_str}],
"attachments": [b64_attachment],
}

# Send email
email = resend.Emails.send(params)
print(email)
print("Sent email with base64 string attachment")
print("Email ID: ", email.id)
6 changes: 3 additions & 3 deletions resend/emails/_attachment.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from typing import List
from typing import List, Union

from typing_extensions import NotRequired, TypedDict


class Attachment(TypedDict):
content: List[int]
content: Union[List[int], str]
"""
Content of an attached file.
This is a list of integers which is usually translated from a
"bytes" type.
"bytes" type, OR a string
Ie: list(open("file.pdf", "rb").read())
"""
filename: str
Expand Down

0 comments on commit 821f055

Please sign in to comment.