n8n series · AI · email · Telegram
n8n with AI: summarise and classify emails automatically
One of the most practical uses of n8n is to put a language model in the middle of a workflow: an email arrives, the AI decides what it is about and whether it is urgent, and n8n acts on that decision.
The example works with a self-hosted instance like the one in the n8n on AWS EC2 guide and with any provider: OpenAI, Anthropic (Claude), Google Gemini or Ollama locally.
Quick answer
Email Trigger (IMAP) → Basic LLM Chain with a chat model → Code that turns the answer into JSON → Switch by category → Telegram for urgent mail and Google Sheets as a log.
1. The complete workflow
Email Trigger (IMAP) → Basic LLM Chain → Code → Switch → Telegram / Google Sheets
- Email Trigger (IMAP): connects to your mailbox. Use Format: Resolved to get
from,subjectandtext. - Basic LLM Chain: add a Chat Model sub-node (OpenAI, Anthropic, Gemini or Ollama) and write the prompt.
- Code: extracts the JSON and protects the flow if the model answers something unexpected.
- Switch: one output for
urgente = trueand another for the rest.
2. The prompt: ask for JSON and trim the text
Ask for a fixed JSON structure with closed categories and trim the email so signatures and previous replies do not waste tokens. The example prompt is in Spanish; translate it if your mail is in English.
Eres un asistente que clasifica correos de un pequeño negocio.
Lee el correo y responde SOLO con un JSON válido, sin texto adicional:
{
"categoria": "cliente" | "factura" | "soporte" | "spam" | "otro",
"urgente": true | false,
"resumen": "una frase de máximo 25 palabras",
"accion": "qué debería hacer el destinatario"
}
Remitente: {{ $json.from }}
Asunto: {{ $json.subject }}
Texto:
{{ $json.text.slice(0, 4000) }}3. Turn the answer into data
// Code: convierte la respuesta del modelo en campos y descarta respuestas mal formadas
const salida = [];
for (const item of $input.all()) {
const texto = String(item.json.text ?? item.json.output ?? '');
const json = texto.match(/\{[\s\S]*\}/)?.[0];
try {
const r = JSON.parse(json);
salida.push({ json: {
categoria: r.categoria ?? 'otro',
urgente: Boolean(r.urgente),
resumen: String(r.resumen ?? '').slice(0, 300),
accion: String(r.accion ?? ''),
asunto: $('Email Trigger (IMAP)').item.json.subject,
de: $('Email Trigger (IMAP)').item.json.from,
} });
} catch {
salida.push({ json: { categoria: 'otro', urgente: false, resumen: 'No se pudo clasificar', accion: 'Revisar a mano' } });
}
}
return salida;Telegram message (Parse Mode: HTML) on the “urgent” output:
🚨 <b>{{ $json.asunto }}</b>
De: {{ $json.de }}
{{ $json.resumen }}
👉 {{ $json.accion }}4. Which model to use
A small cloud model (GPT mini, Claude Haiku, Gemini Flash) is enough to classify email and costs a fraction of a cent per message. Use Ollama if the data must not leave your server, but it needs far more RAM than a t3.small.
5. Privacy and limits
- Tell your team that emails go through an AI provider and review its data terms.
- Exclude mailboxes with health, payroll or specially protected data, or use a local model.
- Do not let the AI reply to customers on its own at first: use it to summarise and prioritise.
Frequently asked questions
Do I need paid n8n to use AI?
No. The AI nodes are included in the self-hosted Community Edition; you only pay the model provider.
What is the difference between Basic LLM Chain and AI Agent?
The chain makes one call with your prompt. The agent can decide to use tools over several steps. For classification the chain is cheaper and more predictable.