Email Sending¶
The Konigle SDK provides email sending functionality for transactional and marketing emails. Emails are sent through configured channels and can include attachments.
Sending Basic Emails¶
Simple Email Without Attachments¶
import konigle
from konigle.models.comm import Email
client = konigle.Client(api_key="your-api-key")
# Send a basic email
email_data = Email(
to_email=["recipient@example.com"],
subject="Welcome to Our Platform",
body_html="<h1>Welcome!</h1><p>Thanks for signing up with us.</p>",
body_text="Welcome! Thanks for signing up with us.",
channel="welcome-emails"
)
response = client.emails.send(email_data)
print(f"Email sent successfully: {response.status}")
print(f"Message ID: {response.message_id}")
Email with Custom From Address and Reply-To¶
# Send email with custom headers
email_data = Email(
from_email="marketing@company.com", # Override account default
to_email=["customer@example.com"],
subject="Special Offer Just for You",
body_html="<h2>Exclusive Deal</h2><p>Check out our latest offers!</p>",
body_text="Exclusive Deal - Check out our latest offers!",
reply_to_email="support@company.com",
channel="marketing-emails",
category="promotions" # For unsubscribe management
)
response = client.emails.send(email_data)
print(f"Email sent: {response.status}")
Note that the from_email must be verified email identity or must belong to a
verified domain identity in the account.
Email with Custom Headers¶
You can include custom headers in your emails. All custom header keys must
start with X-:
# Send email with custom headers
email_data = Email(
to_email=["customer@example.com"],
subject="Order Confirmation",
body_html="<h1>Order Confirmed</h1><p>Your order has been received.</p>",
body_text="Order Confirmed - Your order has been received.",
channel="transactional",
headers={
"X-Order-ID": "12345",
"X-Customer-Type": "premium",
"X-Campaign-ID": "summer-sale-2024"
}
)
response = client.emails.send(email_data)
print(f"Email sent with custom headers: {response.status}")
Custom headers are useful for: - Tracking campaigns and marketing initiatives - Adding metadata for email processing pipelines - Integrating with third-party email analytics tools - Custom routing or filtering in email systems
Important: All custom header keys must start with X- prefix. Headers that
don't follow this convention will be rejected during validation.
Multiple Recipients¶
# Send to multiple recipients
email_data = Email(
to_email=[
"user1@example.com",
"user2@example.com",
"user3@example.com"
],
subject="Team Announcement",
body_html="<h1>Important Update</h1><p>Please review the latest changes.</p>",
body_text="Important Update - Please review the latest changes.",
channel="announcements"
)
response = client.emails.send(email_data)
print(f"Email sent to {len(email_data.to_email)} recipients")
Sending Emails with Attachments¶
Email with File Attachments¶
# Send email with file attachments
email_data = Email(
to_email=["client@example.com"],
subject="Monthly Report",
body_html="<h1>Monthly Report</h1><p>Please find the attached report.</p>",
body_text="Monthly Report - Please find the attached report.",
channel="reports",
attachments=[
"/path/to/monthly_report.pdf",
"/path/to/charts.xlsx"
]
)
response = client.emails.send(email_data)
print(f"Email with attachments sent: {response.status}")
Email with Binary Data Attachments¶
from io import BytesIO
# Send email with binary attachments
pdf_content = b"%PDF-1.4 content here..."
image_content = b"image binary data..."
# Create BytesIO objects
pdf_file = BytesIO(pdf_content)
pdf_file.name = "report.pdf"
image_file = BytesIO(image_content)
image_file.name = "chart.png"
email_data = Email(
to_email=["recipient@example.com"],
subject="Files Attached",
body_html="<h1>Documents</h1><p>Attached files for your review.</p>",
body_text="Documents - Attached files for your review.",
channel="documents",
attachments=[pdf_file, image_file]
)
response = client.emails.send(email_data)
print(f"Email with binary attachments sent: {response.status}")
Email with Mixed Attachment Types¶
# Send email with various attachment types
with open("/path/to/document.pdf", "rb") as pdf_file:
email_data = Email(
to_email=["manager@example.com"],
subject="Project Deliverables",
body_html="<h1>Project Complete</h1><p>All deliverables attached.</p>",
body_text="Project Complete - All deliverables attached.",
channel="project-updates",
attachments=[
"/path/to/summary.txt", # File path
pdf_file, # File object
("custom_name.json", b'{"status": "complete"}') # Tuple format
]
)
response = client.emails.send(email_data)
print(f"Project email sent: {response.status}")
Email Templates Integration¶
Save Email as Template¶
# Send email and save as template for future use
email_data = Email(
to_email=["new_user@example.com"],
subject="Welcome to {{company_name}}",
body_html="<h1>Welcome {{user_name}}!</h1><p>Thanks for joining us.</p>",
body_text="Welcome {{user_name}}! Thanks for joining us.",
channel="onboarding",
save_as_template=True # Save this email as a template
)
response = client.emails.send(email_data)
print(f"Email sent: {response.status}")
if response.template_id:
print(f"Saved as template: {response.template_id}")
Note that email templates do not support any context veriables apart from
unsubscribe_link. The unsubscribe_link variable is only available if the
email is being sent to single recipient.
Async Email Sending¶
import asyncio
import konigle
async def send_emails():
async with konigle.AsyncClient(api_key="your-api-key") as client:
# Send basic email
email_data = Email(
to_email=["async@example.com"],
subject="Async Email",
body_html="<h1>Sent Asynchronously</h1>",
channel="async-channel"
)
response = await client.emails.send(email_data)
print(f"Async email sent: {response.status}")
# Send email with attachments
email_with_files = Email(
to_email=["files@example.com"],
subject="Files via Async",
body_html="<h1>Async Files</h1>",
channel="async-files",
attachments=["/path/to/file.pdf"]
)
response = await client.emails.send(email_with_files)
print(f"Async email with files sent: {response.status}")
asyncio.run(send_emails())
CLI Commands¶
The Konigle CLI provides email sending functionality with full attachment support.
Send Basic Email¶
# Send simple email
konigle comm email send \
--to "recipient@example.com" \
--subject "Test Email" \
--body-html "<h1>Hello</h1><p>This is a test.</p>" \
--body-text "Hello - This is a test." \
--channel "test-channel"
Send Email with Multiple Recipients¶
# Send to multiple recipients
konigle comm email send \
--to "user1@example.com" \
--to "user2@example.com" \
--to "user3@example.com" \
--subject "Team Update" \
--body-html "<h1>Important News</h1>" \
--channel "announcements"
Send Email with Attachments¶
# Send email with file attachments
konigle comm email send \
--to "client@example.com" \
--subject "Monthly Report" \
--body-html "<h1>Report Attached</h1>" \
--channel "reports" \
--attachment "/path/to/report.pdf" \
--attachment "/path/to/summary.xlsx"
Send Email with Custom Headers¶
# Send email with custom headers
konigle comm email send \
--to "customer@example.com" \
--subject "Order Confirmation" \
--body-html "<h1>Order Confirmed</h1>" \
--channel "transactional" \
--header "X-Order-ID:12345" \
--header "X-Customer-Type:premium" \
--header "X-Campaign-ID:summer-sale-2024"
Note: Custom header keys must start with X- prefix. Use the format
--header "Key:Value" for each header.