在前端开发中,我们经常需要通过邮件来发送信息或者触发一些行为。Node-RED 提供了 node-red-contrib-mail-actions 这个 npm 包,帮助我们通过 Node-RED 管理邮件并执行一些操作。本教程将详细介绍如何使用该 npm 包。
安装
首先,我们需要全部安装 Node.js 和 Node-RED。启动 Node-RED 后,可以在 Dashboard 中点击右上角的菜单按钮,选择 Manage palette
,打开 Palette
窗口,点击 Install
标签,然后在搜索框中输入 node-red-contrib-mail-actions
并安装该包。
配置
在 Palette
中找到 email server
节点并拖到 Flow 中,然后打开节点并配置以下信息:
Name
:用来标识节点的名称Protocol
:选择用于连接邮件服务器的协议(IMAP 或 POP3)Server
:输入 SMTP,IMAP 或 POP3 的服务器地址Port
:输入 SMTP,IMAP 或 POP3 的服务器端口号TLs / STARTLS
:选择是否启用 TLS 或者 STARTTLS 安全协议
完成配置后,点击节点上的 Deploy
按钮以保存更改。
使用
在配置好邮件服务器节点后,我们便可以开始使用其它节点来管理邮件。此处以 email in IMAP
节点为例,介绍如何获取邮件并执行操作。
首先,找到 email in IMAP
节点,拖到 Flow 中,然后链接到已经配置好的 email server
节点。向 email in IMAP
节点发送启动信号后,将打开 IMAP 连接并获取邮件。
接着,我们需要选择邮件并执行操作。此处以判断邮件主题是否包含指定字符串为例:
首先,找到 switch
节点,将 msg.payload.subject
附加到 Rules
列表中,比较操作符选择 contains
并输入需要检查的字符串。这样,当收到的邮件主题包含该字符串时,该邮件将自动转发或者触发其它自定义操作。
下面是示例代码:
[{"id":"c6dfb364.88d2c","type":"email in imap","z":"b1e415da.6adb8","name":"imap server","protocol":"IMAP","server":"imap.gmail.com","useSSL":true,"port":"993","box":"INBOX","disposition":"Read","criteria":"UNSEEN","repeat":"25","fetch":"auto","inputs":0,"outputs":1,"x":512,"y":320,"wires":[["d80f09c3.1e08","d3578598.aa0c28"]]},{"id":"4ea9476b.ce9da","type":"switch","z":"b1e415da.6adb8","name":"","property":"payload.subject","propertyType":"msg","rules":[{"t":"cont","v":"Your awesome string","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":754,"y":320,"wires":[["8227d54f.8a681"]]},{"id":"d80f09c3.1e08","type":"debug","z":"b1e415da.6adb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"node","x":744,"y":272,"wires":[]},{"id":"d3578598.aa0c28","type":"delay","z":"b1e415da.6adb8","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":676,"y":320,"wires":[["4ea9476b.ce9da"]]},{"id":"8bcaf158.42af98","type":"email out","z":"b1e415da.6adb8","name":"email out","protocol":"SMTP","server":"smtp.gmail.com","port":"465","tls":true,"sensitive":true,"x":994,"y":320,"wires":[]},{"id":"8227d54f.8a681","type":"change","z":"b1e415da.6adb8","name":"","rules":[{"t":"set","p":"payload.to","pt":"msg","to":"example@example.com","tot":"str"},{"t":"set","p":"payload.subject","pt":"msg","to":"Your email subject","tot":"str"},{"t":"set","p":"payload.cc","pt":"msg","to":"","tot":"str"},{"t":"set","p":"payload.replyTo","pt":"msg","to":"","tot":"str"},{"t":"set","p":"payload.headers","pt":"msg","to":"","tot":"str"},{"t":"set","p":"payload.encoding","pt":"msg","to":"utf8","tot":"str"},{"t":"set","p":"payload.body","pt":"msg","to":"Email body","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":876,"y":320,"wires":[["8bcaf158.42af98"]]}]
结论
通过本教程,我们学习了如何使用 npm 包 node-red-contrib-mail-actions 来管理邮件并执行操作。我们可以通过此包来自动触发各种后续操作,提高我们的开发效率。希望这篇文章对你有所帮助!
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/60066b5651ab1864dac66bff