Core API Reference¶
The exmailer core module provides the primary interface for interacting with Microsoft Exchange.
ExchangeEmailer Class¶
The main controller for email operations. It is designed to be used as a context manager to ensure connections are properly closed.
Methods¶
__init__(self, config: dict[str, Any] | None = None) -> None¶
Initializes the emailer. If no config is provided, it attempts auto-discovery via environment variables or local files.
ExchangeEmailer ¶
ExchangeEmailer(config_path: str | None = None, config: dict[str, Any] | None = None, verbose: bool = False)
Send emails via Microsoft Exchange server with flexible HTML template support.
Initialize the Exchange emailer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str | None
|
Path to JSON/YAML configuration file |
None
|
config
|
dict[str, Any] | None
|
Direct configuration dictionary (highest priority) |
None
|
verbose
|
bool
|
Enable verbose logging |
False
|
Examples:
Method 1: Programmatic config (recommended for scripts)¶
emailer = ExchangeEmailer(config={
"domain": "corp",
"username": "john.doe",
"password": "secret123",
"server": "mail.corp.com",
"email_domain": "corp.com"
})
Method 2: Config file¶
Method 3: Auto-discovery (looks in default locations)¶
Source code in exmailer/core.py
send_email(...) -> bool¶
Sends an email through the Exchange server.
send_email ¶
send_email(subject: str, body: str, recipients: Sequence[str], attachments: Sequence[str] | None = None, cc_recipients: Sequence[str] | None = None, bcc_recipients: Sequence[str] | None = None, template: str | TemplateType | None = TemplateType.PERSIAN, template_vars: dict[str, Any] | None = None, importance: Literal['Low', 'Normal', 'High'] = 'Normal') -> bool
Send an email with optional attachments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subject
|
str
|
Email subject |
required |
body
|
str
|
Email body content |
required |
recipients
|
Sequence[str]
|
List of recipient email addresses |
required |
attachments
|
Sequence[str] | None
|
List of file paths to attach |
None
|
cc_recipients
|
Sequence[str] | None
|
List of CC recipient email addresses |
None
|
bcc_recipients
|
Sequence[str] | None
|
List of BCC recipient email addresses |
None
|
template
|
str | TemplateType | None
|
Template to use. Options: - TemplateType.PERSIAN: Persian RTL template - TemplateType.DEFAULT: English LTR template - TemplateType.PLAIN: Plain text (no template) - str: Custom template name registered via register_custom_template() - None: Use plain text (no template) |
PERSIAN
|
template_vars
|
dict[str, Any] | None
|
Variables to replace in template (e.g., {"date": "14/08/1404"}) |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if email was sent successfully |
Examples:
>>> # Using built-in Persian template
>>> emailer.send_email(
... subject="سلام",
... body="متن پیام",
... recipients=["user@example.com"],
... template=TemplateType.PERSIAN
... )
>>> # Using built-in English template
>>> emailer.send_email(
... subject="Hello",
... body="Message content",
... recipients=["user@example.com"],
... template=TemplateType.DEFAULT
... )
>>> # Using plain text (no template)
>>> emailer.send_email(
... subject="Hello",
... body="Plain message",
... recipients=["user@example.com"],
... template=None
... )
>>> # Using custom template
>>> emailer.send_email(
... subject="Newsletter",
... body="Content here",
... recipients=["user@example.com"],
... template="my_custom_template"
... )
Source code in exmailer/core.py
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 216 217 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 | |
| Parameter | Type | Default | Description |
|---|---|---|---|
subject |
str |
Required | The email subject line. |
body |
str |
Required | The HTML or plain text body. |
recipients |
list[str] |
Required | List of primary recipient addresses. |
cc_recipients |
list[str] \| None |
None |
List of CC addresses. |
bcc_recipients |
list[str] \| None |
None |
List of BCC addresses. |
template |
TemplateType \| str \| None |
TemplateType.DEFAULT |
The template to wrap the body in. |
template_vars |
dict[str, Any] \| None |
None |
Variables for f-string style replacement. |
Template Management¶
register_custom_template(name: str, template_string: str) -> None¶
Registers a new HTML template for global use within the application session.