DEV Community

 AI Automation Enterenpeur
AI Automation Enterenpeur

Posted on

Turn Newsletter Replies Into a Feedback Log With 40 Lines of Python

Ending a newsletter with one question ("What should we build next?") is one of the cheapest ways to get customer feedback. The hard part isn't getting replies. It's not losing them.

Here's a small Python script that checks an inbox for new replies to your newsletter, tags each one by keyword, and appends it to a CSV you can open in any spreadsheet.

What it does

  1. Connects to your inbox over IMAP.
  2. Finds unread messages whose subject matches your newsletter's reply subject.
  3. Pulls the plain-text body.
  4. Tags it as pricing, bug, feature_request, praise or other.
  5. Appends date, sender, tag and the first line to newsletter_replies.csv.

No third-party packages. Just the standard library.

The code

import csv
import email
import imaplib
import os
from email.utils import parseaddr

SUBJECT = "Re: The Weekly Build"  # your newsletter's subject

TAGS = {
    "pricing": ["price", "pricing", "cost", "plan"],
    "feature_request": ["wish", "would love", "feature", "could you add"],
    "bug": ["bug", "broken", "error", "doesn't work"],
    "praise": ["love", "great", "thanks", "helpful"],
}


def tag_reply(text):
    text = text.lower()
    found = [tag for tag, words in TAGS.items() if any(w in text for w in words)]
    return ", ".join(found) or "other"


def get_body(msg):
    parts = msg.walk() if msg.is_multipart() else [msg]
    for part in parts:
        if part.get_content_type() == "text/plain":
            payload = part.get_payload(decode=True) or b""
            return payload.decode(errors="ignore")
    return ""


def main():
    box = imaplib.IMAP4_SSL(os.environ["IMAP_HOST"])
    box.login(os.environ["IMAP_USER"], os.environ["IMAP_PASS"])
    box.select("INBOX")
    _, ids = box.search(None, f'(UNSEEN SUBJECT "{SUBJECT}")')

    with open("newsletter_replies.csv", "a", newline="") as f:
        writer = csv.writer(f)
        for num in ids[0].split():
            _, data = box.fetch(num, "(RFC822)")
            msg = email.message_from_bytes(data[0][1])
            _, sender = parseaddr(msg["From"])
            body = get_body(msg).strip()
            first_line = body.splitlines()[0][:200] if body else ""
            writer.writerow([msg["Date"], sender, tag_reply(body), first_line])

    box.logout()


if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

How to run it

Set IMAP_HOST, IMAP_USER and IMAP_PASS as environment variables (for Gmail, use imap.gmail.com and an app password), then run:

python3 log_replies.py
Enter fullscreen mode Exit fullscreen mode

Fetching a message marks it as read, so each reply is logged only once. Schedule it with cron to run every morning:

0 8 * * * cd /path/to/script && python3 log_replies.py
Enter fullscreen mode Exit fullscreen mode

Ideas to extend it

  • Strip quoted text (lines starting with ">") so you only keep the new reply.
  • Swap the keyword tagger for an LLM call that returns a tag and a one-sentence summary.
  • Write rows to Google Sheets or your CRM instead of a CSV.
  • Post a Slack or Discord message when a reply is tagged bug.

Why bother

Keyword tagging won't be perfect, but it turns a pile of unread replies into a sortable list. Once a month, filter by feature_request and you have a ranked, real-words wishlist from your own customers.

I build automations like this for businesses and SaaS teams: https://ai-automation-entrepreneur.vercel.app/

Top comments (0)