Replace in unread emails

Hello. The function works great only for one unread email in Gmail. If there are more than one unread letters, then an empty message is left in the next element. Please tell me, I’ve tried a lot of code variants and it doesn’t work.

Here’s a function that works for one unread email:

const item = $node[“Получить новые письма”].json[“text”];
items[0].json.cuttext = item.replace(/<.+>/g, “”);
return items;

Here the nodes:

{
  "name": "для вопроса",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "451067437",
        "text": "={{$node[\"Отрезать лишнее\"].json[\"cuttext\"]}}",
        "additionalFields": {}
      },
      "name": "Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1230,
        300
      ],
      "alwaysOutputData": false,
      "retryOnFail": false,
      "credentials": {
        "telegramApi": "тест Телеграм"
      }
    },
    {
      "parameters": {
        "interval": 2,
        "unit": "minutes"
      },
      "name": "Interval",
      "type": "n8n-nodes-base.interval",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "getAll",
        "returnAll": true,
        "additionalFields": {
          "q": "label:unread"
        }
      },
      "name": "Получить новые письма",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 1,
      "position": [
        630,
        300
      ],
      "credentials": {
        "gmailOAuth2": "Gmail API"
      }
    },
    {
      "parameters": {
        "resource": "messageLabel",
        "operation": "remove",
        "messageId": "={{$node[\"Получить новые письма\"].json[\"id\"]}}",
        "labelIds": [
          "UNREAD"
        ]
      },
      "name": "Сделать прочитанными",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 1,
      "position": [
        830,
        300
      ],
      "credentials": {
        "gmailOAuth2": "Gmail API"
      },
      "disabled": true
    },
    {
      "parameters": {
        "functionCode": "const item = $node[\"Получить новые письма\"].json[\"text\"];\nitems[0].json.cuttext = item.replace(/<.+>/g, \"\");\nreturn items;\n\n\n\n      /*  var data = $node[\"Получить новые письма\"].json[\"text\"];\n        console.log(data);\n        var mails= data;\n        console.log(mails);\n        for (let i = 0; i < mails.length; i++) {\n             items[i].json['srez'] = mails;\n        };\n        return items;*/\n\n        /*var data = fs.readFileSync(\"users.json\", \"utf8\");\n        var users = JSON.parse(data);\n        var users = users.filter(function (e) {\n            return e.recipient == true;\n        });\n\n        for (let i = 0; i < users.length; i++) {\n            globalMessage(users[i]);\n        }*/\n\n\n/*const item = items;\nconsole.log({ $item(\"0\").$node[\"Отрезать лишнее\"].json[\"text\"] });\n//items[0].json.srez= item.text.replace(/<.+>/g, \"\");\nreturn items;*/\n\n\n/*let index = 0;\nfor (const data of $node[\"Получить новые письма\"].json[\"text\"]) {\n  console.log(items);\n  items[0].json[`${index++}`] = data.replace(/<.+>/g, \"\")[0];\n}\nreturn items;*/\n"
      },
      "name": "Отрезать лишнее",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1030,
        300
      ],
      "notes": "Replase"
    }
  ],
  "connections": {
    "Interval": {
      "main": [
        [
          {
            "node": "Получить новые письма",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Получить новые письма": {
      "main": [
        [
          {
            "node": "Сделать прочитанными",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Сделать прочитанными": {
      "main": [
        [
          {
            "node": "Отрезать лишнее",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Отрезать лишнее": {
      "main": [
        [
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "3"
}

This should iterates over all incoming items and run the replace:

items.forEach(item => {
  item.json.cuttext = item.json.text.replace(/<.+>/g, "");
});

return items;
1 Like

Thank you, comrade! I read earlier that n8n has a friendly community. Now I am convinced of this!

Happy to hear that I could help and that what you did hear matches reality.

Have fun!