Skip to main content

Email notifications

Header

Level: Intermediate

Keywords: notifications, email, SMTP

The result: Send email

We use email notifications to inform people who are not the users of the system (do not have a user account) or if we want to send a message directly to an email address. If we want to send a message to an email using user information, we will use Message notifications.

SMTP server​

At first, we need to configure the SMTP server. Add following configuration in tenant file ..\lids-bussines-service\tenants\tenantName.json.

tenant - configuration example
{
"email" : {
"type" : "SMTP",
"smtpConfiguration" : {
"server" : "{$smtp_server}",
"port" : "{$smtp_port}",
"user" : "{$smtp_user}", // optional
"password" : "{$smtp_password}", // optional
"useSSL" : "{$smtp_useSSL}" // optional
}
}
}

Sending using action step​

In the following example, if the action is invoked on an entity instance, we can access the entity instance directly from the data model. We also have access to the environment variables here. In the template, we can then access each attribute of the entity.

action step sendSMS - example
{
"type": "sendEmail",
"from": "{#message.sender}",
"to": [
"{$portal.contactAdminMail}"
],
"subject": "AGPortal - Contact admin",
"templateGroup": "portal",
"template": "contact-admin/contactAdmin"
}

More definition about Send email step can be found in Business Server Configuration.

Sending using ScriptingAPI​

If we send the message using ScriptingAPI, we compile the whole model for the template ourselves.

In the following example, the message is sent to three email addresses using ScriptingAP and contains three attachments.

sending using ScriptingAPI - example
var feature = context.entity;
api.messages()
.prepareSendEmail(context)
.sender("no-reply@asseco-ce.com")
.addRecipient("admin@asseco-ce.com")
.addCCRecipient("admin2@asseco-ce.com")
.addBCCRecipient("admin3@asseco-ce.com")
.subject("test")
.templateGroup("test")
.template("testEmailTemplate")
.model(feature)
.prepareFeatureAttachment()
.attachmentId("677369054072634552295904")
.attach()
.prepareTemporaryFileAttachment()
.temporaryFileId("677369054072634552295905")
.attach()
.prepareFileAttachment()
.templateGroup("test")
.template("testWordTemplate")
.model(feature)
.name("testAttachment.pdf")
.outputType("pdf")
.attach()
.prepareEntityAttachment()
.entityType(feature.type)
.entityId(feature.id)
.attach()
.send();

You can use the prepareFeatureAttachment(), prepareFileAttachment() or prepareTemporaryFileAttachment() methods to attach attachments, but they are optional. Usually e-mails are sent without attachments. Method prepareFileAttachment() creates a attachment from the template.

The same goes for the copy recipient addCCRecipient() and the hidden email recipient addBCCRecipient().