Email to CSV (not Excel)
CSV is the right output when the file has to be imported somewhere: CRM, BI, a script. Excel adds formatting you do not need.
Field mapping that avoids pain later
| Column | Format | Why |
|---|---|---|
| date | ISO 8601 (2026-10-19T10:30:00Z) | Sortable, unambiguous timezone |
| from_name | text | Display |
| from_email | lowercase email only | Joins and dedupe |
| subject | text, quotes escaped | Search |
| folder | text | Preserve source |
| has_attachments | 0/1 | Filter in CRM |
Working export to CSV
import csv, email, imaplib
imap = imaplib.IMAP4_SSL("imap.example.com")
imap.login("user@example.com", "APP_PASSWORD")
imap.select("INBOX", readonly=True)
status, data = imap.search(None, "(SINCE 01-Sep-2026)")
with open("mail.csv", "w", newline="", encoding="utf-8-sig") as f:
writer = csv.writer(f)
writer.writerow(["date", "from_email", "subject", "folder", "has_attachments"])
for num in data[0].split():
status, msg_data = imap.fetch(num, "(RFC822)")
msg = email.message_from_bytes(msg_data[0][1])
writer.writerow([
msg["Date"],
email.utils.parseaddr(msg["From"])[1].lower(),
msg["Subject"],
"INBOX",
int(any(p.get_filename() for p in msg.walk())),
])
Encoding rules
- Use UTF-8 with BOM (
utf-8-sig) if the file will be opened in Excel. - Quote every field containing commas, quotes or newlines.
- Normalize line breaks inside message text before writing.
Excel vs CSV
| CSV | Excel | |
|---|---|---|
| CRM / BI import | ✅ native | needs conversion |
| Human reading | plain | ✅ formatting, filters |
| Formulas, multiple sheets | ❌ | ✅ |
| File size | smaller | larger |
If you need both, generate CSV and XLSX from the same data in one pass.
FAQ
Why does Excel show garbled text in my CSV?
The file is UTF-8 without BOM. Save with utf-8-sig or import via Data → From Text/CSV and choose UTF-8.
Can I import email CSV into a CRM?
Yes, map CSV columns to CRM fields. Keep email addresses lowercase and dates in ISO 8601 to avoid import errors.
Related guides
- IMAP → ExcelSame data, spreadsheet output.
- Invoices from emailA B2B case where CSV feeds accounting.