MailService — Aquilia Documentation
Comprehensive guide and documentation for MailService in the Aquilia framework. View API reference, examples, and implementation patterns.
Mail / MailService MailService API The MailService orchestrates email compilation, DKIM signing, rate limiting, and provider dispatch. Learn how to build messages, attach files, set custom headers, and handle errors. EmailMessage API Usages Construct plain-text or HTML-alternative emails using EmailMessage and EmailMultiAlternatives: from aquilia.mail import EmailMessage, EmailMultiAlternatives, Attachment # 1. Simple Plain Text Email msg = EmailMessage( subject="Invoice #2041", body="Your invoice is attached.", from_email="billing@myapp.com", to=["client@example.com"], cc=["accountant@myapp.com"], priority=80, # High priority (default is 50) headers= ) # Add attachment msg.attach(Attachment(filename="invoice.pdf", content=b"...pdf_bytes...", content_type="application/pdf")) await msg.asend() # 2. HTML Alternative Email html_msg = EmailMultiAlternatives( subject="Monthly Newsletter", body="Read the newsletter online at https://myapp.com/newsletter", from_email="newsletter@myapp.com", to=["user@example.com"] ) # Attach the HTML body alternative html_msg.attach_alternative(" Our Monthly Updates Here is the news... ", content_type="text/html") await html_msg.asend() The MailEnvelope Dataclass When messages are processed, they are converted into an immutable MailEnvelope that represents the delivery unit of work: from dataclasses import dataclass, field from datetime import datetime from typing import Any @dataclass class MailEnvelope: id: str # Unique UUID tracking string created_at: datetime # Creation timestamp # Queue Priority priority: int = 50 # Priority rating (0-100) # Addressing from_email: str = "" # Normalized sender address to: list[str] = field(default_factory=list) cc: list[str] = field(default_factory=list) bcc: list[str] = field(default_factory=list) reply_to: str | None = None # Content subject: str = "" # Pre-interpolated subject line body_text: str = "" # Plain text representation body_html: str | None = None # HTML alternative body headers: dict[str, str] = field(default_factory=dict) # Attachments attachments: list[Attachment] = field(default_factory=list) # Idempotency & Verification idempotency_key: str | None = None digest: str = "" # SHA-256 checksum hash of the envelope Mail Fault Handling The mail subsystem raises structured exceptions based on the failure domain: , , , , ].map((f, i) => ( ))} from aquilia.mail import TemplateMessage from aquilia.faults import MailSendFault, MailValidationFault try: msg = TemplateMessage( template="welcome.aqt", subject="Welcome!", to=["invalid-email-format"] ) await msg.asend() except MailValidationFault as e: print(f"Validation failed: ") except MailSendFault as e: print(f"Network delivery failed: ") Overview Providers )
Go to Homepage