Mail¶
Outgoing mail, chatter (MailThread), and email templates.
Email templates (mail.template)¶
Admins manage templates under Settings → Workflows → Email templates.
Each template targets a model (res.partner, etc.) and stores a Jinja2
subject plus an Html body (sanitized on save). The editor has:
- Write — TipTap v3 ribbon toolbar (styles, font, lists, insert image, etc.)
- HTML source — CodeMirror 6 (language html or jinja), Jinja variable autocomplete (Ctrl+Space)
- Preview — sanitized HTML; optional preview with record via
POST /api/mail/templates/previewand a record picker on the Preview tab
Bundled via npm into /web/static/dist/mail_editor.js — run
npm install && npm run build:editor after changing editor code. Prefer
HTML source for heavy {{ object.* }} editing; the Write tab may
normalize markup around placeholders.
Insert variable — searchable dropdown (type to search when the model has
more than 25 fields). Model list: GET /api/mail/templates/models; fields:
GET /api/mail/templates/variables?model=….
Legacy Odoo syntax ${object.name} is accepted.
From code on a MailThread record:
partner.send_mail(template, to="user@example.com", extra={"ctx": {"token": "abc"}})
# or
template.send_mail(partner, to="user@example.com")
Rendered mail is queued on mail.message with body_is_html=True and
dispatched by the usual cron / Message.dispatch_outgoing().
Html field¶
Subclass of Text; values pass through pyvelm/html_sanitizer.py on write.
Renders with the HTML editor widget by default.
API reference¶
mail ¶
Mail thread mixin, message model, and outgoing-mail dispatcher.
Three layers ship here:
-
mail.message— stores every message / log entry, keyed by (model, res_id) so messages associate with any record on any model. -
MailThread— mixin:record.message_post(body)records a log entry without sending mail.record.notify(...)records an entry and queues it for SMTP delivery. -
MailBackend+Message.dispatch_outgoing()— the queue walker the cron runner calls every tick. Picks up rows whererecipient_emailis set andstate="outgoing", hands them to the configured backend, and transitions the row tosentorfailed(with the error captured).
Fields on mail.message¶
model _name of the owning model (e.g. "res.partner").
res_id Primary key of the owning record.
author_id Many2one to res.users (nullable — system messages use None).
body Text body (plain text or HTML; no enforcement here).
message_type "comment" | "notification" | "email" (default "comment").
subtype Free-form subtype label (e.g. "note", "done").
date UTC naive datetime of posting.
recipient_email Optional address; when set, the row is dispatched via SMTP.
subject Subject line for outgoing email (falls back to body[:80]).
state "outgoing" | "sent" | "failed" (default "outgoing").
error Last dispatch failure (free-form string).
Mail backend selection¶
PYVELM_MAIL_BACKEND env var picks the implementation:
console(default) — logs to stdout. Safe for dev and tests.disabled— silently drops every send. Use this in CI or when you want the dispatcher to mark messages assentwithout actually contacting an SMTP server.smtp— talks SMTP toPYVELM_SMTP_HOST/PORT/....
SMTP env knobs:
PYVELM_SMTP_HOST Hostname (e.g. ``smtp.gmail.com``).
PYVELM_SMTP_PORT Port (default 587).
PYVELM_SMTP_USER Username, if the server requires auth.
PYVELM_SMTP_PASSWORD Password.
PYVELM_SMTP_FROM From-address. Required for the smtp backend.
PYVELM_SMTP_USE_TLS ``1`` (default) to STARTTLS, ``0`` to skip.
MailBackend ¶
Bases: Protocol
A pluggable transport for outgoing mail.
Implementations raise on transient failures so the dispatcher can
flip the row to state="failed" and surface the reason via
error. The protocol is intentionally narrow — one method —
because every backend in this file collapses to "deliver this
text to that address."
Source code in pyvelm/mail.py
ConsoleBackend ¶
Logs the would-be send to stdout. Default backend in dev/CI.
Useful for the smoke test + interactive demo because there's no SMTP server to stand up and the operator gets visible feedback that the dispatcher ran.
Source code in pyvelm/mail.py
DisabledBackend ¶
SmtpBackend ¶
RFC-5321 SMTP transport via the standard-library smtplib.
Config comes from the PYVELM_SMTP_* env vars (see module
docstring). The backend opens a fresh connection for each call —
fine at typical pyvelm cadences (one tick per minute). A future
pooling refinement is on the table if mail volume grows.
Source code in pyvelm/mail.py
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 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 | |
MailThread ¶
Mixin that adds chatter / message-thread capability to any model.
Must appear before BaseModel in the MRO so that its __init__
still delegates upward correctly:
class MyModel(MailThread, BaseModel):
_name = "my.model"
Source code in pyvelm/mail.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
message_ids
property
¶
Return the ids of all messages attached to this record.
message_post ¶
message_post(body: str, *, message_type: str = 'comment', subtype: str = '', attachment_ids: list[int] | None = None) -> 'Message'
Create a new mail.message log entry for this record.
The returned message is NOT queued for SMTP — it's just a log
line. Use notify() when you want both a log entry and an
outgoing email.
attachment_ids re-points existing ir.attachment rows
at the new message (res_model = "mail.message", res_id =
msg.id). Callers upload the bytes first (via
POST /api/attachment/upload) and pass the resulting ids in.
Source code in pyvelm/mail.py
notify ¶
notify(body: str, *, recipient_email: str, subject: str = '', message_type: str = 'email', subtype: str = '', attachment_ids: list[int] | None = None, cc: str | None = None, bcc: str | None = None, reply_to: str | None = None, body_is_html: bool = False) -> 'Message'
Log a message AND queue it for SMTP delivery.
Same shape as message_post but additionally sets the
recipient_email and subject fields so the next
dispatcher tick picks the row up.
attachment_ids works the same way as in message_post —
the SMTP backend currently doesn't add the bytes to the
outgoing mail (that's a follow-up), but the linkage is
recorded so the chatter UI can render them.
Source code in pyvelm/mail.py
send_mail ¶
send_mail(template, *, to: str, cc: str | None = None, bcc: str | None = None, reply_to: str | None = None, extra: dict | None = None, attachment_ids: list[int] | None = None) -> 'Message'
Queue email rendered from a mail.template record.
Source code in pyvelm/mail.py
mail_template ¶
Backward-compatible re-export of mail.template.
build_mail_template_context ¶
build_mail_template_context(env, *, model: str, record=None, extra: dict[str, Any] | None = None) -> dict[str, Any]
Build the dict passed to Jinja when rendering a template.
Source code in pyvelm/mail_template_render.py
render_mail_template_string ¶
Render source with Jinja2 (sandboxed, auto-escaped).
Source code in pyvelm/mail_template_render.py
html_sanitizer ¶
HTML sanitizer for Html field values and mail.template bodies.
The framework stores HTML written by admins (email templates, rich text in
chatter etc.). That HTML lands in the DOM via x-html and in outgoing
SMTP messages, so anything that would execute code in either context has
to be stripped before storage. We deliberately do this at the write
boundary (in the field's to_python / to_sql_param) so a malicious
payload never reaches the database — even a future bug that bypasses the
x-html render path can't reach back to a tainted column.
The allowlist is small and email-shaped: structural tags, common inline
formatting, lists, tables, links, images. Everything else is silently
dropped (tag stripped; children kept). No external dependency — the
parser is pure html.parser. For complex Markdown-flavoured content,
swap this for bleach or nh3 at the field level; the public API
is one function (:func:sanitize_html).
sanitize_html ¶
Return value with disallowed tags / attributes / URL schemes stripped.
None / "" round-trip unchanged. The output is always a
str; callers that want :class:markupsafe.Markup can wrap it.