使用 Zapier 轉發信件至 Line 群組 === Gmail 的轉寄設定需要先用目的地址收發驗證碼,這會讓 IFTTT 無法通過驗證。這篇範例使用 Zapier 在收到信件後使用 Python 處理內容,並轉送到 Line 群組中。 [toc] ## Zapier 設定 - 建立 Trigger 並設定 - APP: `Email by Zapier` - Event: `New Inbound Email` - 自訂一個名稱,之後要處理的信都要寄到這裡。 在 Gmail 中可以在右上角齒輪 > `查看所有設定` > `轉寄` 裡面輸入這個信箱並驗證。之後只需要建立篩選器把相關的信件轉寄到這邊就好。 ![](https://i.imgur.com/nFsMkdf.png) ## Gmail 轉寄設定 接著 Action 中使用 Code by Zapier,下面範例程式碼都使用 Python ,不過 Zapier 也有支援 Javascript 。 首先簡單的內容印出等一下收到的驗證碼,主要把 `Body Plain` 放在 Input Data ,並輸出到 output 變數。 ```python= output = {'title': input_data['title'], 'content': input_data['content']} ``` 等 Zap 啟動後就可以從 Gmail 寄送驗證信,並在執行紀錄中取得驗證碼。 ![](https://i.imgur.com/ieomqql.png) - 在 Outlook 也有類似的轉寄設定 :::warning 未測試 ::: ![](https://i.imgur.com/QNfWYhB.png) ## 轉發到 Line Notify ```python= access_token = 'LINE_NOTIFY_TOKEN' headers = {'Authorization': f'Bearer {access_token}'} notify_api = 'https://notify-api.line.me/api/notify' message = f"tittle:{input_data['title']}<br>content:{input_data['content']}" res = requests.post(notify_api, data={'message':message}) output = ['status': res.status_code] ``` ![](https://i.imgur.com/SqH77Md.png) 之後也可以先處理訊息內容後才送出,例如使用正規表示法只取出驗證碼而不是轉送所有信件內容。 ```python= import re input_data = {"title": "test", "content": "驗證碼: 123456"} code = re.search(r"\d{6}", input_data["content"]).group(0) ``` ## 其他注意事項 Zapier 其實 Triger 有直接支援 Gmail 和 Outlook 等服務,但是 1. 要授權讓他們收發所有信件,除非這個帳號沒有其他用途,不然真的==非常不建議==這麼做。 2. 如果是使用其他沒有支援的信箱還是得用轉寄的方式。 ![](https://i.imgur.com/OCrOst9.png) ![](https://i.imgur.com/yUcRnX8.png) 免費版本的 Zapier 有一些限制,目前新帳號還在試用期不確定轉為免費版上述方法都能正常使用。 1. 每個月只能執行 100 次。 2. 執行程式碼有些[限制](https://help.zapier.com/hc/en-us/articles/8496181445261#code-throttling-0-4),以發送請求到 Line Notify 有可能會超時。 ![](https://i.imgur.com/pTYPqOh.png) ## Reference - [Use Python code in Zaps](https://help.zapier.com/hc/en-us/articles/8496326417549-Use-Python-code-in-Zaps) - [LINE Notify API Document](https://notify-bot.line.me/doc/en/)