Skip to content

Attach the totals as a CSV file to the summary email - #1

Merged
VeljkoRailsware merged 1 commit into
VeljkoRailsware:mainfrom
yevody:feature/email-summary-csv-attachment
Oct 16, 2026
Merged

VeljkoRailsware merged 1 commit into
VeljkoRailsware:mainfrom
yevody:feature/email-summary-csv-attachment

Conversation

@yevody

@yevody yevody commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

What this adds

The summary email now carries the totals as a CSV attachment, so the report opens straight in Excel, Numbers or Google Sheets instead of being copied out of the email body.

For examples/sales.csv the email gets sales-summary.csv:

column,total
quantity,6
amount,36.50

It uses the attachments array of the Mailtrap Email API (base64 content, text/csv, disposition: attachment). core.Mailer and the attachment() helper already supported this; the upload route just didn't use them. Sandbox, production and log mode all work, and log-mode .eml previews include the file.

Details

  • Filename: <upload name>-summary.csv, cleaned with werkzeug.utils.secure_filename (../May sales.csv becomes May_sales-summary.csv; falls back to csv-summary.csv).
  • CSV injection: column names come from the upload, so names starting with =, +, - or @ get a leading ', and spreadsheets open them as text instead of running them as formulas. Totals stay plain numbers.
  • No numeric columns: no attachment is added, and the email body is unchanged.
  • The email text says which file is attached.

Docs

  • README: a descriptive title and intro that say what the app does and that it sends through the Mailtrap Email API (stdlib urllib, no SDK), with the Mailtrap Email Sandbox for testing; an updated "Try it" section; and an FAQ (sending an attachment from Flask, testing without real delivery, CSV injection, failed sends). This makes the README easier for people, search engines and LLM assistants to match to questions like "send CSV attachment from Flask".
  • docs/email-setup.md: describes the attachment.

Suggested repo "About" text (needs repo admin)

Flask app that summarizes uploaded CSV files (rows, columns, numeric totals) and emails the report with a CSV attachment via the Mailtrap Email API. Uses Mailtrap Email Sandbox for safe testing.

Suggested topics: flask, python, csv, email, email-api, transactional-email, mailtrap.

Testing

  • python -m pytest -q: 18 passed on Python 3.12 (2 new tests: attachment content, filename and formula escaping; no attachment without numeric columns).
  • Manually ran the app in MAIL_MODE=log, uploaded examples/sales.csv and checked the .eml preview: sales-summary.csv, text/csv, correct totals.
  • Not tested against a live Mailtrap account; the API payload shape is covered by the existing test_sandbox_and_live_http_payload_and_attachment.

🤖 Generated with Claude Code

The summary email now carries <upload>-summary.csv (column,total) through
the Mailtrap Email API attachments array, so the report opens straight in a
spreadsheet. Column names that a spreadsheet would run as formulas are
prefixed with a quote to prevent CSV injection. No attachment is added when
no column is fully numeric.

The README gets a descriptive title, an intro that names the Mailtrap Email
API and Email Sandbox, and an FAQ.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@VeljkoRailsware
VeljkoRailsware merged commit 184312e into VeljkoRailsware:main Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants