iT邦幫忙

2025 iThome 鐵人賽

DAY 12
0
DevOps

Mac 環境 n8n 30 天學習計畫系列 第 12

Day12:If 判斷 → 分流處理 Email📧

  • 分享至 

  • xImage
  •  

🎯 目標

  • 使用 Cron → Function → If → Email
  • 每天自動產生一個訊息
  • 根據條件(例如:提醒是「喝水」還是「運動」)分流處理,寄送不同的 Email

1️⃣ 建立基本流程

https://ithelp.ithome.com.tw/upload/images/20250923/20169144afZi0VMwLG.png

  1. 開啟 n8n → 點擊 Create Workflow

  2. 拖入 Cron Node(排程觸發):

    • 測試:設成每分鐘一次。
    • 正式用:設成每天早上 9:00。

2️⃣ Function Node → 產生隨機提醒訊息

拖一個 Function Node,內容輸入以下程式碼:

const messages = [
  { type: "drink", text: "📢 每日提醒:記得喝水 💧" },
  { type: "stretch", text: "📢 每日提醒:起來伸展一下 🧘" },
  { type: "focus", text: "📢 每日提醒:保持專注 🔥" }
];

const randomMsg = messages[Math.floor(Math.random() * messages.length)];

return [{
  json: {
    subject: "每日提醒",
    type: randomMsg.type,
    text: randomMsg.text
  }
}];

https://ithelp.ithome.com.tw/upload/images/20250923/20169144sf4NcGhWcx.png
這樣每次執行都會隨機產生不同提醒,並附帶一個 type 標籤(例如 "drink" / "stretch" / "focus")。


3️⃣ If Node → 判斷提醒類型

  1. 拖入一個 If Node

  2. 設定條件:

    • Value 1: {{$json["type"]}}
    • Operation: equals
    • Value 2: drink

📌 意思是:如果 type = "drink",走 True 分支;否則走 False 分支
https://ithelp.ithome.com.tw/upload/images/20250923/20169144kkautsucwS.png

4️⃣ Email 節點 → 分流寄送不同內容

True 分支(喝水提醒)

  1. 在 If Node 的 True output 接一個 Email Node

  2. 設定:

    • To Email: 你的信箱
    • Subject: ={{$json["subject"]}}
    • Text: ={{$json["text"]}} (這是喝水提醒 💧)
      https://ithelp.ithome.com.tw/upload/images/20250923/20169144H37g5E4HzL.png

False 分支(其他提醒)

  1. 在 If Node 的 False output 接另一個 Email Node

  2. 設定:

    • To Email: 你的信箱
    • Subject: ={{$json["subject"]}}
    • Text: ={{$json["text"]}} (這是非喝水提醒 🚀)
      https://ithelp.ithome.com.tw/upload/images/20250923/20169144MyW0GheCsZ.png

5️⃣ 測試與啟動

  1. 點右上角 Execute Workflow → 測試隨機提醒是否分流正確。
  2. 如果成功,開啟 Activate,就會自動每天依條件寄信。

https://ithelp.ithome.com.tw/upload/images/20250923/20169144W2j3aA3Quu.png
https://ithelp.ithome.com.tw/upload/images/20250923/20169144U5Z8NTjzJP.png

https://ithelp.ithome.com.tw/upload/images/20250923/20169144fGew6S1nEd.png


上一篇
📝Day11:Notion CRUD 自動化練習
系列文
Mac 環境 n8n 30 天學習計畫12
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言