Email¶
The email adapter provides integration with email services for sending transactional and notification emails.
Ports¶
Abstract port interface defining the email adapter contract.
archipy.adapters.email.ports.EmailPort ¶
Interface for email sending operations.
This interface defines the contract for email adapters, ensuring a consistent approach to sending emails across different implementations. It provides a comprehensive set of features including support for:
- Multiple recipients (To, CC, BCC)
- HTML and plain text content
- File and in-memory attachments
- Template-based email rendering
Implementing classes should handle the details of connecting to an email service, managing connections, and ensuring reliable delivery.
Examples:
>>> from archipy.adapters.email.ports import EmailPort
>>>
>>> class CustomEmailAdapter(EmailPort):
... def __init__(self, config):
... self.config = config
...
... def send_email(
... self,
... to_email,
... subject,
... body,
... cc=None,
... bcc=None,
... attachments=None,
... html=False,
... template=None,
... template_vars=None,
... ):
... # Implementation details...
... pass
Source code in archipy/adapters/email/ports.py
archipy.adapters.email.ports.EmailPort.send_email
abstractmethod
¶
send_email(
to_email: EmailStr | list[EmailStr],
subject: str,
body: str,
cc: EmailStr | list[EmailStr] | None = None,
bcc: EmailStr | list[EmailStr] | None = None,
attachments: list[str | EmailAttachmentDTO]
| None = None,
html: bool = False,
template: str | None = None,
template_vars: dict | None = None,
) -> None
Send an email with various options and features.
This method handles the composition and delivery of an email with support for multiple recipients, HTML content, templates, and attachments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_email
|
EmailStr | list[EmailStr]
|
Primary recipient(s) of the email |
required |
subject
|
str
|
Email subject line |
required |
body
|
str
|
Email body content (either plain text or HTML) |
required |
cc
|
EmailStr | list[EmailStr] | None
|
Carbon copy recipient(s) |
None
|
bcc
|
EmailStr | list[EmailStr] | None
|
Blind carbon copy recipient(s) |
None
|
attachments
|
list[str | EmailAttachmentDTO] | None
|
List of file paths or EmailAttachmentDTO objects |
None
|
html
|
bool
|
If True, treats body as HTML content, otherwise plain text |
False
|
template
|
str | None
|
A template string to render using template_vars |
None
|
template_vars
|
dict | None
|
Variables to use when rendering the template |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Examples:
>>> # Simple text email
>>> adapter.send_email(to_email="user@example.com", subject="Hello", body="This is a test email")
>>>
>>> # HTML email with attachment
>>> adapter.send_email(
... to_email=["user1@example.com", "user2@example.com"],
... subject="Report",
... body="<h1>Monthly Report</h1><p>Please see attached</p>",
... html=True,
... attachments=["path/to/report.pdf"],
... )
>>>
>>> # Template-based email
>>> template = "Hello {{ name }}, your account expires on {{ date }}"
>>> adapter.send_email(
... to_email="user@example.com",
... subject="Account Expiration",
... body="", # Body will be rendered from template
... template=template,
... template_vars={"name": "John", "date": "2023-12-31"},
... )
Source code in archipy/adapters/email/ports.py
options: show_root_toc_entry: false heading_level: 3
Adapters¶
Concrete email adapter implementing SMTP-based email sending with ArchiPy conventions.
archipy.adapters.email.adapters.EmailConnectionManager ¶
Manages SMTP connections with connection pooling and timeout handling.
Source code in archipy/adapters/email/adapters.py
archipy.adapters.email.adapters.EmailConnectionManager.smtp_connection
instance-attribute
¶
archipy.adapters.email.adapters.EmailConnectionManager.last_used
instance-attribute
¶
archipy.adapters.email.adapters.EmailConnectionManager.connect ¶
Establish SMTP connection with authentication.
Source code in archipy/adapters/email/adapters.py
archipy.adapters.email.adapters.EmailConnectionManager.disconnect ¶
Close SMTP connection safely.
Source code in archipy/adapters/email/adapters.py
archipy.adapters.email.adapters.EmailConnectionManager.refresh_if_needed ¶
Refresh connection if needed based on timeout.
Source code in archipy/adapters/email/adapters.py
archipy.adapters.email.adapters.EmailConnectionPool ¶
Connection pool for managing multiple SMTP connections.
Source code in archipy/adapters/email/adapters.py
archipy.adapters.email.adapters.AttachmentHandler ¶
Enhanced attachment handler with better type safety and validation.
Source code in archipy/adapters/email/adapters.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
archipy.adapters.email.adapters.AttachmentHandler.create_attachment
staticmethod
¶
create_attachment(
source: str | bytes | BinaryIO | HttpUrl,
filename: str,
attachment_type: EmailAttachmentType,
content_type: str | None = None,
content_disposition: EmailAttachmentDispositionType = EmailAttachmentDispositionType.ATTACHMENT,
content_id: str | None = None,
max_size: int | None = None,
) -> EmailAttachmentDTO
Create an attachment with validation.
Source code in archipy/adapters/email/adapters.py
archipy.adapters.email.adapters.AttachmentHandler.process_attachment
staticmethod
¶
Process and attach the attachment to the email message.
Source code in archipy/adapters/email/adapters.py
archipy.adapters.email.adapters.EmailAdapter ¶
Bases: EmailPort
Email adapter implementing EmailPort for sending emails with SMTP.
Source code in archipy/adapters/email/adapters.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
archipy.adapters.email.adapters.EmailAdapter.connection_pool
instance-attribute
¶
archipy.adapters.email.adapters.EmailAdapter.send_email ¶
send_email(
to_email: EmailStr | list[EmailStr],
subject: str,
body: str,
cc: EmailStr | list[EmailStr] | None = None,
bcc: EmailStr | list[EmailStr] | None = None,
attachments: list[str | EmailAttachmentDTO]
| None = None,
html: bool = False,
template: str | None = None,
template_vars: dict | None = None,
) -> None
Send email with advanced features and connection pooling.
Source code in archipy/adapters/email/adapters.py
options: show_root_toc_entry: false heading_level: 3