Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: fix types on examples with attachments #77

Merged
merged 5 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading