feat: resend example (#1416)

This commit is contained in:
Charles Packer
2024-05-25 17:22:58 -07:00
committed by GitHub
parent 648f781560
commit 487fd47b10
3 changed files with 145 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
import json
import os
import requests
def send_email(self, description: str):
"""
Sends an email to a predefined user. The email contains a message, which is defined by the description parameter.
Args:
description (str): Email contents. All unicode (including emojis) are supported.
Returns:
None
Example:
>>> send_email("hello")
# Output: None. This will send an email to the you are talking to with the message "hello".
"""
RESEND_API_KEY = os.getenv("RESEND_API_KEY")
RESEND_TARGET_EMAIL_ADDRESS = os.getenv("RESEND_TARGET_EMAIL_ADDRESS")
if RESEND_API_KEY is None:
raise Exception("User did not set the environment variable RESEND_API_KEY")
if RESEND_TARGET_EMAIL_ADDRESS is None:
raise Exception("User did not set the environment variable RESEND_TARGET_EMAIL_ADDRESS")
url = "https://api.resend.com/emails"
headers = {"Authorization": f"Bearer {RESEND_API_KEY}", "Content-Type": "application/json"}
data = {
"from": "onboarding@resend.dev",
"to": RESEND_TARGET_EMAIL_ADDRESS,
"subject": "MemGPT message:",
"html": f"<strong>{description}</strong>",
}
try:
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.text)
except requests.HTTPError as e:
raise Exception(f"send_email failed with an HTTP error: {str(e)}")
except Exception as e:
raise Exception(f"send_email failed with an error: {str(e)}")