import osfrom ark import Ark# Initialize the clientclient = Ark(api_key=os.environ.get("ARK_API_KEY"))# Send an emailemail = client.emails.send( from_="Security <security@myapp.com>", to=["user@example.com"], subject="Reset your password", html="<h1>Password Reset</h1><p>Click the link below to reset your password.</p>", text="Password Reset\n\nClick the link below to reset your password.")print(f"Email ID: {email.data.id}")print(f"Status: {email.data.status}")
The from_ parameter uses an underscore because from is a reserved keyword in Python.
Returns:SendEmailResponse with data.id, data.status, etc.
send_batch
Send multiple emails in a single request.
Copy
result = client.emails.send_batch( emails=[ { "from_": "hello@yourdomain.com", "to": ["user1@example.com"], "subject": "Hello User 1", "html": "<p>Hello!</p>" }, { "from_": "hello@yourdomain.com", "to": ["user2@example.com"], "subject": "Hello User 2", "html": "<p>Hello!</p>" } ])for email in result.data: print(f"{email.id}: {email.status}")
send_raw
Send a raw MIME message. The raw_message parameter must be base64-encoded.
Copy
import base64raw_message = """From: hello@yourdomain.comTo: user@example.comSubject: Raw emailContent-Type: text/plainThis is a raw MIME message."""email = client.emails.send_raw( from_="hello@yourdomain.com", to=["user@example.com"], raw_message=base64.b64encode(raw_message.encode()).decode())
The raw_message must be base64-encoded. Pass your RFC 2822 MIME message through base64.b64encode(message.encode()).decode() before sending.
list
List emails with filtering and pagination.
Copy
# List recent emailsemails = client.emails.list( page=1, per_page=25, status="sent", # Optional filter tag="welcome", # Optional filter)for email in emails.data: print(f"{email.id}: {email.subject} - {email.status}")# Pagination infoprint(f"Page {emails.page} of {emails.total_pages}")
deliveries = client.emails.get_deliveries("msg_abc123xyz")for delivery in deliveries.data: print(f"Attempt at {delivery.timestamp}: {delivery.status}") if delivery.error: print(f" Error: {delivery.error}")
retry
Retry a failed email.
Copy
result = client.emails.retry("msg_abc123xyz")print(f"Retry scheduled: {result.data.id}")
domain = client.domains.create(name="mail.yourdomain.com")print(f"Domain ID: {domain.data.id}")print("DNS Records to configure:")for record in domain.data.dns_records: print(f" {record.type} {record.name} -> {record.value}")
list
List all domains.
Copy
domains = client.domains.list()for domain in domains.data: status = "Verified" if domain.verified else "Pending" print(f"{domain.name}: {status}")
result = client.domains.verify("dom_abc123")if result.data.verified: print("Domain verified successfully!")else: print("DNS records not found. Please check your configuration.")
tracking = client.tracking.create( domain="track.yourdomain.com")print("Add this CNAME record:")print(f" {tracking.data.cname_target}")
list
List tracking domains.
Copy
tracking_domains = client.tracking.list()for t in tracking_domains.data: status = "Verified" if t.verified else "Pending" print(f"{t.domain}: {status}")
verify
Verify tracking domain DNS.
Copy
result = client.tracking.verify("trk_abc123")print(f"Verified: {result.data.verified}")
import uuididempotency_key = str(uuid.uuid4())# This request can be safely retried with the same keyemail = client.emails.send( from_="hello@yourdomain.com", to=["user@example.com"], subject="Order Confirmation", html="<p>Your order is confirmed.</p>", idempotency_key=idempotency_key)
The SDK models allow access to undocumented API fields:
Copy
email = client.emails.retrieve("msg_abc123")# Access known fieldsprint(email.data.subject)# Access any additional fields returned by the APIprint(email.data.model_extra) # Dict of extra fields