立即试用 商务报价
社区版
Send HTML or image inside email

本页目录

Send HTML or image inside email

This Tutorial is to show you how to send an email message with HTML page or image inside.

Prerequisites

Message flow

  • Like a start point we will use Generator that will imitate regular rule chain messages flow: prepared message and metadata where we can contain some dynamic field for to email node.
  • The To email node prepares data, destination email, and other for email message.
  • The Send email node sends a message.

Configuring Rule Nodes

Configuring “to email” node

At first, lets create and configure “to email” node.

  1. Go to rule chain, find to email node and drag it to the canvas.
  2. Specify: Name, From Template, To Template - we will use pattern to find an email in data of message, Subject Template. Select Mail Body Type HTML or Dynamic. We will use the Dynamic.
  3. Specify HTML to Body Template (you can use our example).
  4. Press Add.

Example of HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<table class="main" style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; box-sizing: border-box; border-radius: 3px; width: 100%; background-color: #f6f6f6; margin: 0px auto;" cellspacing="0" cellpadding="0" bgcolor="#f6f6f6">
    <tbody>
    <tr style="box-sizing: border-box; margin: 0px;">
        <td class="content-wrap" style="box-sizing: border-box; vertical-align: top; margin: 0px; padding: 20px;" align="center" valign="top">
            <table style="box-sizing: border-box; border: solid 1px #e9e9e9; border-radius: 3px; margin: 0px; height: 127px; padding: 20px; background-color: #ffffff; width: 600px; max-width: 600px !important;" width="600" cellspacing="0" cellpadding="0">
                <tbody>
                <tr style="box-sizing: border-box; margin: 0px;">
                    <td class="content-block" style="color: #348eda; box-sizing: border-box; border-radius: 6px; vertical-align: top; margin: 0px; padding: 0px 0px 20px; width: 839px;" valign="top">
                        <h2>Test message using 'to email' rule node</h2>
                    </td>
                </tr>
                <tr style="box-sizing: border-box; margin: 0px;">
                    <td class="content-block" style="box-sizing: border-box; vertical-align: top; margin: 0px; padding: 0px 0px 20px; width: 600px;" valign="top"><span style="color: #000000;">This email is indicating that your outgoing mail settings were set up correctly.</span></td>
                </tr>
                <tr style="box-sizing: border-box; margin: 0px;">
                    <td class="content-block" style="box-sizing: border-box; vertical-align: top; margin: 0px; padding: 0px 0px 20px; width: 600px;" valign="top"><span style="color: #000000;">&mdash; The ThingsBoard :)</span></td>
                </tr>
                </tbody>
            </table>
        </td>
    </tr>
    </tbody>
</table>

To send an image, you just need to specify the name of the image with the file extension.

Example of HTML with image

1
<img src="cid:tb.example.png" />

Configuring generator

At the second stage, let’s configure the “generator” node:

  1. Find in the Rule Chain Generator and drag it to the canvas
  2. Specify name field, choose “1” for message count and, for example “2” for Period in seconds
  3. Now we need to prepare JS code, also you can use our example.

Here we need to specify fields in metadata, that are dynamic in “to email” node. In our example it is “isHtml” and “userEmail” fields.

Example of generator JS code

1
2
3
var metadata = { userEmail: 'info@thingsboard.org', isHtml: true };
var msgType = "POST_TELEMETRY_REQUEST";
return { msg: {}, metadata: metadata, msgType: msgType }

To send an image you need to encode the image to Base64. To do this you need to find some service that can do this and set the result string for encodedImage in the next example.

Example of generator JS code

1
2
3
4
5
6
7
8
9
10
11
var encodedImage = 'HERE_YOU_BASE_64_ENCODED_IMAGE';
var images = {
    "tb.example.png": encodedImage
};
var metadata = { 
    userEmail: 'info@thingsboard.org', 
    images: JSON.stringify(images), 
    isHtml: true 
};
var msgType = "POST_TELEMETRY_REQUEST";
return { msg: {}, metadata: metadata, msgType: msgType }

Send email and finishing

Find and configure new send email node and connect all nodes between themselves, like on the screen shoot. Save rule chain.

Result

Check destination email to see the result of the “to email” node work. We got the next message:

See Also

Next steps

  • 入门指南 - 快速学习ThingsBoard相关功能。

  • 安装指南 - 学习如何在各种操作系统上安装ThingsBoard。

  • 连接设备 - 学习如何根据你的连接方式或解决方案连接设备。

  • 可 视 化 - 学习如何配置复杂的ThingsBoard仪表板说明。

  • 数据分析 - 学习如何使用规则引擎执行基本的分析任务。

  • 硬件样品 - 学习如何将各种硬件平台连接到ThingsBoard。

  • 高级功能 - 学习高级ThingsBoard功能。

  • 开发指南 - 学习ThingsBoard中的贡献和开发。