Skip to content

Commit

Permalink
examples: fix other examples
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Apr 22, 2024
1 parent 7470c74 commit a4b32b0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
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 a4b32b0

Please sign in to comment.