🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Emailing from an app using MDrivenServer
This page was created by Lars.olofsson on 2019-08-16. Last edited by Wikiadmin on 2026-07-29.

You can send email from a server-side ViewModel in an MDriven application when you configure SMTP on MDrivenServer and invoke an action whose name starts with email.

Before you start

MDrivenServer needs access to an SMTP service before it can send an email. Configure the SMTP connection in the MDrivenServer administration UI as described in Documentation:Fill in the email settings in the admin UI.

For a Turnkey site, you can apply the settings in the Portal or in the MDrivenServer email settings; Portal settings update the MDrivenServer settings. See Documentation:Turnkey email settings for Turnkey-specific settings and sender/login considerations.

Send an email from a server-side ViewModel

Create a server-side ViewModel that prepares the email values and has an action named email, or another action whose name begins with email. When that action runs, MDrivenServer looks for email-related properties in the ViewModel and sends the message.

  1. Create the server-side ViewModel that will prepare and send the message.
  2. Add an action named email. Action names such as emailOrderConfirmation also trigger sending because the name starts with email.
  3. Add and populate the recipient, sender, subject, and body properties described below.
  4. Run the action from your server-side process. For patterns for executing server-side work, see Training:MDrivenServer periodic server-side actions.
  5. Inspect emailsendresult after the action runs to record the SMTP server feedback. Use emailresendadvised to determine whether a failed send is advised for retry.

Property names are not case-sensitive.

Email properties

Property Type or value Purpose Example
to String The recipient address or addresses. customer@example.com
cc String Carbon-copy recipient address or addresses. sales@example.com
bcc String Blind-carbon-copy recipient address or addresses. audit@example.com
from String The sender address. You can include a presentation name with the address. Orders <orders@example.com>
subject String The email subject line. Your order has been received
body String The message body. Thank you for your order.
attachments Nesting A nesting that supplies zero or more file attachments. Its required columns are described in Add attachments. One row for invoice.pdf
emailsendresult String Feedback from the SMTP server after a send attempt. Store or display the returned SMTP status text.
emailresendadvised Boolean Indicates whether the failed send is advised for retry. It is true for network-dependent SMTP errors and SMTP status codes from 400 through 499. true

Address format and multiple recipients

Use a comma-separated string to send to more than one recipient. Each address can use the form Presentation name<email address> to show a common name instead of only the email address.

For example, set to to:

PopularName1<email@email.x>,PopularName2<email2@email.x>

The same formatting rules apply to to, cc, bcc, and from.

Sender presentation name

Use the presentation-name-and-address form directly in from. The older frompresentation property is no longer used; sender presentation is handled by from.

For example:

from = Orders <orders@example.com>

Add attachments

To attach files, add a nesting named attachments to the sending ViewModel. Each attachment row must provide the file content, its file name, and its media type.

Column in the attachments nesting Type Example
file Blob The binary content of an invoice PDF
name String invoice-1042.pdf
mediatype String application/pdf

The nesting name and column names are not case-sensitive.

For example, an email with a PDF attachment has one attachments row where file contains the PDF blob, name is invoice-1042.pdf, and mediatype is application/pdf.

Include images in an HTML email

For an HTML email, you can embed an image as base64 data in the HTML body:

<img src="data:image/jpg;base64,base64-data-string-here" />

Alternatively, add the image as an attachment and reference it by content ID (CID). Add a contentid column to the attachments nesting and use the same value in the HTML body:

<img src="cid:THECONTENTID" />

For example, set an attachment row's contentid to logo-01 and include this in body:

<img src="cid:logo-01" />

When MDrivenServer finds contentid, it assigns that ID to the attachment and sets its disposition to inline. The email client can then show the image where the CID is referenced rather than listing it as a normal attachment. If no contentid column is present, MDrivenServer still sends the attachment without an ID.

Diagnose sending problems

Start by verifying the SMTP settings outside MDrivenServer using the PowerShell test described in Documentation:Fill in the email settings in the admin UI. This separates SMTP-account and network problems from ViewModel setup problems.

Then inspect the ViewModel's emailsendresult value after the send action. Check the Documentation:MDrivenServer log for MDrivenServer status and errors. If emailresendadvised is true, the SMTP response indicates a network-dependent error or a status from 400 through 499, so the send is a candidate for retry.

See also