Email Processing Pipelines with Haystack

Combine Haystack's composable pipeline architecture with MultiMail's email infrastructure to build RAG-powered email agents that retrieve, reason, and respond.


Haystack is an end-to-end NLP framework by deepset for building search and RAG pipelines. Its composable architecture lets you snap together retrievers, generators, and custom components into processing pipelines. MultiMail provides the email infrastructure layer that turns Haystack's retrieval and generation capabilities into production email workflows.

By integrating MultiMail with Haystack, you can build pipelines that process inbound emails, retrieve relevant context from document stores, generate informed responses, and send replies through managed mailboxes. MultiMail's gated_send mode ensures that RAG-generated replies are reviewed for factual accuracy before reaching recipients.

Connect Haystack to MultiMail by creating custom pipeline components that call the MultiMail REST API. Haystack's component model makes it natural to add email input/output stages to any pipeline.

Built for Haystack developers

Pipeline-Native Email Components

Create custom Haystack components for email operations that integrate seamlessly into your pipelines. An EmailFetcher component can pull from MultiMail's inbox, and an EmailSender component can deliver generated responses.

RAG-Powered Email Responses

Haystack excels at retrieval-augmented generation. Combine its retriever and generator components with MultiMail to build pipelines that answer emails based on your knowledge base, ensuring responses are grounded in actual data.

Human Review for Generated Content

RAG pipelines can still produce inaccurate responses. MultiMail's gated_send mode adds a human review step between Haystack's output and actual email delivery, catching errors that retrieval alone cannot prevent.

Email Indexing Pipeline

Build a Haystack pipeline that fetches emails from MultiMail and indexes them into a document store. This makes your email history searchable and retrievable for future context-aware responses.

Composable Email Workflows

Haystack's pipeline architecture lets you compose complex email workflows from simple components. Chain classification, routing, retrieval, generation, and sending stages into end-to-end email processing pipelines.


Get started in minutes

Create MultiMail Pipeline Components
python
import requests
from haystack import component, Document

MULTIMAIL_API = "https://api.multimail.dev/v1"
HEADERS = {"Authorization": "Bearer mm_live_your_api_key"}

@component
class EmailFetcher:
    @component.output_types(emails=list[Document])
    def run(self, mailbox_id: str, limit: int = 10):
        resp = requests.get(
            f"{MULTIMAIL_API}/mailboxes/{mailbox_id}/inbox",
            headers=HEADERS, params={"limit": limit}
        )
        emails = resp.json().get("emails", [])
        docs = [
            Document(
                content=f"From: {e[&"cm">#039;from']}\nSubject: {e['subject']}\n\n{e['body']}",
                meta={"message_id": e["message_id"], "from": e["from"]}
            )
            for e in emails
        ]
        return {"emails": docs}

@component
class EmailSender:
    @component.output_types(result=dict)
    def run(self, to: str, subject: str, body: str, mailbox_id: str):
        resp = requests.post(f"{MULTIMAIL_API}/send", headers=HEADERS, json={
            "mailbox_id": mailbox_id, "to": to,
            "subject": subject, "body": body
        })
        return {"result": resp.json()}

Build custom Haystack components that fetch emails from and send emails through MultiMail.

Build a RAG Email Response Pipeline
python
from haystack import Pipeline
from haystack.components.generators import OpenAIGenerator
from haystack.components.builders.prompt_builder import PromptBuilder
from haystack_integrations.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever

"cm"># Set up document store with your knowledge base
doc_store = InMemoryDocumentStore()
retriever = InMemoryBM25Retriever(document_store=doc_store)

prompt_template = """
Based on the following context, draft a professional email reply.
Context:
{% for doc in documents %}
{{ doc.content }}
{% endfor %}

Original email:
{{ email_content }}

Draft a concise, helpful reply:
"""

pipeline = Pipeline()
pipeline.add_component("retriever", retriever)
pipeline.add_component("prompt", PromptBuilder(template=prompt_template))
pipeline.add_component("generator", OpenAIGenerator(model="gpt-4o"))
pipeline.add_component("sender", EmailSender())

pipeline.connect("retriever.documents", "prompt.documents")
pipeline.connect("prompt", "generator")

"cm"># Run the pipeline (send step done separately for oversight)
result = pipeline.run({
    "retriever": {"query": "pricing information"},
    "prompt": {"email_content": "What are your current rates?"}
})

Create a pipeline that retrieves context from a document store and generates email replies.

Email Indexing Pipeline
python
from haystack import Pipeline
from haystack.components.preprocessors import DocumentCleaner, DocumentSplitter
from haystack.components.writers import DocumentWriter

index_pipeline = Pipeline()
index_pipeline.add_component("fetcher", EmailFetcher())
index_pipeline.add_component("cleaner", DocumentCleaner())
index_pipeline.add_component("splitter", DocumentSplitter(
    split_by="sentence", split_length=3
))
index_pipeline.add_component("writer", DocumentWriter(
    document_store=doc_store
))

index_pipeline.connect("fetcher.emails", "cleaner")
index_pipeline.connect("cleaner", "splitter")
index_pipeline.connect("splitter", "writer")

"cm"># Index recent emails
index_pipeline.run({"fetcher": {"mailbox_id": "your_mailbox_id", "limit": 50}})

Fetch emails from MultiMail and index them into a Haystack document store for future retrieval.


Step by step

1

Create a MultiMail Account and API Key

Sign up at multimail.dev, create a mailbox, and generate an API key. Your key will start with mm_live_.

2

Install Dependencies

Install Haystack and requests for calling the MultiMail API.

bash
pip install haystack-ai requests
3

Create Custom Components

Build Haystack @component classes for EmailFetcher and EmailSender that wrap MultiMail API endpoints.

4

Build Your Pipeline

Compose your email processing pipeline by connecting components: fetcher, retriever, prompt builder, generator, and sender.

bash
pipeline = Pipeline()
pipeline.add_component("fetcher", EmailFetcher())
pipeline.add_component("generator", OpenAIGenerator())
pipeline.connect("fetcher", "generator")
5

Run and Review

Execute the pipeline. Review and approve pending emails in the MultiMail dashboard when using gated_send mode.

bash
result = pipeline.run({"fetcher": {"mailbox_id": "your_mailbox_id"}})

Common questions

How do I create a Haystack component for MultiMail?
Use Haystack's @component decorator to create custom components that call the MultiMail REST API. Define input and output types using @component.output_types, and implement the run() method to make API calls. Components integrate into pipelines just like built-in Haystack components.
Can I index my email history into a Haystack document store?
Yes. Build an indexing pipeline with an EmailFetcher component, document preprocessors (cleaner, splitter), and a DocumentWriter. This indexes your MultiMail emails into any Haystack-compatible document store for semantic search and retrieval in future response generation.
What oversight mode works best for RAG email pipelines?
Start with gated_send to review every RAG-generated reply before delivery. As your pipeline proves reliable, consider monitored mode where emails send immediately but remain visible for review. Autonomous mode should only be used for thoroughly tested, high-confidence response pipelines.
Can I use Haystack's evaluation tools to measure email response quality?
Yes. Haystack provides evaluation components for measuring RAG quality metrics like faithfulness and relevance. Run your email response pipeline through evaluation before switching from gated_send to less restrictive oversight modes, building quantitative confidence in your pipeline's output quality.
Does Haystack support streaming for email generation?
Haystack's generator components support streaming, which is useful for long email drafts. However, the final email is sent to MultiMail as a complete message via the REST API. Streaming is most beneficial for user-facing previews of the draft before it enters MultiMail's approval queue.

Explore more

The only agent email with a verifiable sender

Email infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.