Send email notification with attachments

🟡 Intermediate

notification report

This example aims at showing you how to add attachments for a email notification triggered from a script.

  • Create a report generator using a FreeMarker template and an analytics query to produce a CSV file.

  • Generate the report from a script using reports.generate with the generator ID.

  • Transform the generated report into the expected attachment structure for the notification API.

  • Send the email notification with the attachment from the same script.

git checkout origin/osp-communications-configuration .
git checkout origin/osp-reports-configuration .
git checkout origin/osp-analytics-configuration .
git checkout origin/osp-waveforms-configuration .
git checkout origin/osp-scripts-configuration .
git checkout origin/example-communications-email-attachments .

Add this example in your configuration

You can directly use the following command to add this example into your configuration:

Steps

1. Create a generator to generate the attachments

In this example, we will use a generator to fetch the data using the analytics query. We need a template for the csv file and an analytics query.

root/00_examples/email_attachment/report/csv/report.ftl

<#list analytics?keys as point>
<#list analytics[point] as inputs>
${inputs.field}, ${inputs.time?c}, ${inputs.value}
</#list>
</#list>

root/00_examples/email_attachment/analytics/query/query.analytics

{
    "moduleId": "modules.analytics.analytics-1",
    "query": {
        "collection": "month",
        "range": {
            "start": {
                "value": 1,
                "unit": "HOURS"
            }
        },
        "measurements": ["root.00_examples.email_attachment.analytics.output"],
        "fields": ["root.00_examples.email_attachment.waveforms"],
        "limit": {"limit": 500}
    }
}

root/00_examples/email_attachment/report/generator/generator.reports

{
    "moduleId": "modules.reports.reports-1",
    "template": "root.00_examples.email_attachment.report.csv",
    "dataSources": {
        "analytics": {
            "type": "Analytics",
            "query": "root.00_examples.email_attachment.analytics.query"
        }
    }
}

2. Generate the report and send the notification

We can simply generate the report using reports.generate with the id of the generator created before. Then, we use the result, transform it to accommodate the expected structure and send the notification with it.

root/00_examples/email_attachment/script/generate-and-send-mail.js

function main() {
    const reportResult = reports.generate("root.00_examples.email_attachment.report.generator", "test", "en", "US");
    const res = reportResult.getContent();

    if (reportResult.isSuccess()) {
        communications.sendStaticNotification(
            "root.00_examples.email_attachment.mail.notification",
            {
                "smsSender": "test",
                "emailSenderAddress": "test@localhost",
                "emailSenderName": "test"
            },
            [
                {
                    "providerId": "root.00_examples.email_attachment.mail.provider"
                }
            ],
            "root.00_examples.email_attachment.mail.contact",
            {},
            [
                {
                    format: res.format,
                    name: res.name,
                    content: res.parsed
                }
            ]
        );   
    }

}

main()