A short python script that uses the Sendgrid API to send a basic email.

# /// script
# dependencies = [
# "sendgrid",
# ]
# ///
# https://docs.astral.sh/uv/guides/scripts/#declaring-script-dependencies
# https://docs.astral.sh/uv/#scripts
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
# Replace with your actual sender and recipient emails
# Make sure your 'from_email' is a verified sender in your SendGrid account.
FROM_EMAIL = 'adhoc-test@domain.com.au'
TO_EMAIL = 'marquin@marquinsmith.com'
API_KEY_1 = 'SG.your-api-key-here'
def send_simple_email():
"""Sends a simple plain-text email using SendGrid."""
try:
# Get the API key from environment variable
sendgrid_api_key = API_KEY_1
if not sendgrid_api_key:
raise ValueError("SENDGRID_API_KEY environment variable not set.")
message = Mail(
from_email=FROM_EMAIL,
to_emails=TO_EMAIL,
subject='Sending with SendGrid is Fun!',
plain_text_content='And easy to do anywhere, even with Python.')
sg = SendGridAPIClient(sendgrid_api_key)
response = sg.send(message)
print(f"Email sent successfully! Status Code: {response.status_code}")
print(f"Response Body: {response.body}")
print(f"Response Headers: {response.headers}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == '__main__':
send_simple_email()