cardinal

Print formats and mail

Ship print formats, email templates and notification rules, and send mail, SMS, push and Telegram messages from code.

Ship a print format

Print Formats are fixtures. Keep the layout in templates/ and import it, so the record stays a few lines:

gst_compliance/fixtures/print_format.yaml
- name: GST Tax Invoice
  doc_type: Sales Invoice
  standard: "Yes"
  custom_format: true
  html: |
    {%- from "print/page.html.j2" import header -%}
    {%- from "print/gst_invoice.html.j2" import tax_invoice -%}
    {{ header(doc, letter_head, no_letterhead, print_settings) }}
    {{ tax_invoice(doc) }}

templates/print/gst_invoice.html.j2 holds the macro. A standard format is written by the installer, and only the app changes it. Set a RecType's default_print_format to open the print view with it.

Ship email templates

crm/fixtures/email_template.yaml
- name: CRM Lead Follow Up
  reference_rectype: CRM Lead
  subject: "Checking in, {{ doc.first_name }}"
  response: "<p>Hello {{ doc.first_name }},</p><p>I wrote a couple of days ago and wanted to check in.</p>"

CRM's reference automations send these. A template renders with the record as doc.

Ship notification rules

A Notification sends mail on a record event, with no code:

hrms/fixtures/notification.yaml
- name: Training Scheduled
  enabled: true
  channel: Email
  document_type: Training Event
  event: Submit
  recipients:
    - {receiver_by_document_field: employee_emails}
  subject: "Training Scheduled: {{ doc.name }}"
  message: >-
    <p>{{ _("Training Event:") }} {{ doc.event_name }}</p>

Send mail from code

Cardinal.Mail.send/2 queues a mail in the caller's transaction. It goes out after the commit and never after a rollback. ERP's Project invites a collaborator:

erp/lib/controllers/project.ex
Cardinal.Mail.send(ctx, %{
  to: [email],
  subject: ~s|{{ _("Project Collaboration Invitation") }}|,
  html:
    ~s|<p>{{ _("You have been invited to collaborate on the project {0}.", label) }}</p>| <>
      ~s|<p><a href="{{ url }}">{{ url }}</a></p>|,
  args: %{"label" => label, "url" => Cardinal.Ctx.site_url(ctx) <> path}
})

subject and html are template sources. Put user values in args, never in the source, so the template escapes them.

KeyMeaning
to, cc, bccAddresses. A User's name is its email
argsValues the templates read
attachmentsFile maps, or print descriptors (print with rectype, name and file_name), whose PDF renders as the sender. A print may take password
reference{rectype, name}. Unsubscribed addresses are dropped
communicationThe Communication the mail records
send_afterA DateTime. Past the 10 s undo delay the mail is Scheduled
accountThe Email Account to send from. Default: the site's outgoing account
in_reply_toThe Message-ID it answers, so clients thread it
now: trueSkip the 10 s delay
redact: trueDelete the body once sent, for sign-in links

It returns {:ok, queue_names} or {:error, :no_outgoing_account}. Cardinal.Mail.cancel_scheduled/2 and send_now/2 act on a Scheduled mail.

Other channels

CallSends
Cardinal.SMS.send(ctx, receivers, message)An SMS through SMS Settings, after commit. Writes an SMS Log
Cardinal.Push.send(ctx, user, payload)A Web Push to each browser the user subscribed, after commit
Cardinal.Telegram.send_text(ctx, user, html, opts)A message to the chat the user linked to the site's bot
Cardinal.Telegram.send_photo/4, send_document/6, typing/2A photo, a file, or the typing indicator
Cardinal.Notify.create(ctx, users, values)A bell entry. See Realtime and the bell

The Telegram calls wait on Telegram, so call them from a job with txn: false. They return :ok, {:error, :telegram_off}, {:error, :not_linked} or {:error, {:telegram, code, description}}. Cardinal.Telegram.message(ctx, rec), in a rec_events hook on Communication, tells whether the record is a message a user sent the bot.

PDFs and other files

CallDoes
Cardinal.Pdf.record(ctx, rectype, name, opts)A record's print PDF as the ctx's user
Cardinal.Pdf.screenshot/3A PNG of a self-contained HTML page
Cardinal.Template.render(ctx, source, context)A Jinja source rendered in the user's language and time zone

On this page