This guide demonstrates how to send a basic email using the Resend API. You’ll learn how to set up your environment, configure your API key, and send an email.
resend-email-example.py
Copy
import osfrom dotenv import load_dotenvimport resend# Load environment variables from .env fileload_dotenv()# Get API key and email details from environment variablesresend.api_key = os.getenv('RESEND_API_KEY')email_from = os.getenv('RESEND_EMAIL_FROM')email_to = os.getenv('RESEND_EMAIL_TO')# Define email contentsubject = "Hello World"email_body = "<strong>It works!</strong>"try: # Send the email using Resend API response = resend.Emails.send({ "from": email_from, "to": email_to, "subject": subject, "html": email_body }) # Print and return the response print(f"Email sent to {email_to}. Response: {response}") responseexcept Exception as e: # Print and return the error message print(f"Failed to send email. Error: {e}") {"status": "error", "message": str(e)}
You have successfully sent an email using the Resend API! This guide provided a basic example to get you started. You can now expand on this by customizing the email content and handling different email sending scenarios.