Postman collection
Invoices Write

pixiSendEmailForInvoice

Creates a Pixi automail of type `API_Invoice_eMail` for an invoice from the matching mail template and places it in the outbox or drafts.

Generates and queues an e-mail for one invoice using the Pixi automail module (AM) and the mail type API_Invoice_eMail.

Steps:

  1. An empty InvoiceNr is treated as missing. A missing Subject or Message becomes an empty string.
  2. Subject, Message and InvoiceNr are stored in a temporary table (Api_Subject, Api_Message, InvoiceNr). The mail type's SQL query can read them when it builds the mail.
  3. The mail type API_Invoice_eMail is looked up in AM_Type. It must be enabled.
  4. The invoice is read from Invoices and joined to its billing address in Addresses. This gives the customer, shop ID, country, shipping vendor and payment type.
  5. A mail template of this type is selected with fPixi_AM_GetTemplate. It looks for an enabled template that matches shop, country, payment type and shipping vendor. If no template matches all four, it falls back to templates that match fewer of these criteria, and finally to a generic template.
  6. pixi_AM_CreateMailForCSD builds the mail: recipient, subject, body and body type.
  7. Pixi_AM_SendManual stores the mail in AM_Mail with status CREATED. It may also attach the invoice document.
  8. The key of the new mail record is returned in the column AM_TypeKey.

If a check fails, the call returns a row with ReturnCode = ERROR and an ErrorMsg and stops.

Notes

  • The call does not send the mail directly. It creates an AM_Mail record in folder 1 (outbox), or in folder 0 (drafts) if the setting PixiMail_ForceManualMode = 1 or the mail type is in manual mode. It also publishes the MQ event MailCreated.
  • Test mode: if the setting PixiMail_ForceTestMode = 1 or the mail type has test mode enabled, the recipient is replaced by the address in the setting PixiMail_Test_eMail.
  • Subject and message: the passed Subject is overwritten by the subject that the template generates. Subject and Message only appear in the mail if the mail type's SQL query or the template uses Api_Subject / Api_Message. That query is stored in AM_Type.AM_TypeSQLproc and runs as dynamic SQL. Its exact behaviour cannot be seen from the code.
  • If the dynamic query fails, the error is only written to the AM log and the call returns eMail was not created!.
  • If the query returns no rows, an empty placeholder mail row is created. That row may then be stored with an empty recipient.
  • Mail type must also be a folder-1 type: Pixi_AM_SendManual requires the type with AM_TypeFolder = 1. Otherwise it raises Email Type is disabled. In that case the caller receives a SqlMessage and a row with AM_TypeKey = 0.
  • Misleading column name: AM_TypeKey holds the new mail key (identity of AM_Mail), not the type key.
  • Invoice attachment: the invoice document is attached via AM_MailAttachment only if the mail type allows attachments, the template has AttachInvoiceDocument = 1 and an attachment path is found.
  • AM_MailInvoiceNr is stored as NULL in the mail record.
  • UserName is passed to a varchar(20) parameter, so longer values are truncated.
  • Reads use NOLOCK. No explicit transaction is used.

Tables used

AM_Type · Read Invoices · Read Addresses · Read AM_Template · Read Customers · Read AM_Mail · Write AM_MailAttachment · Write
SQL object:
dbo.pipiSendEmailForInvoice
Updated at 2026-10-30