We introduced two models:
expenses.models.UserEmailConfig: per-user alias like<random>[email protected]auto-created on user signup.expenses.models.UserEmailMessage: stores raw EML and basic metadata for matched emails.
uv run python backend/manage.py fetch_emails --username $EMAIL_USER --password $EMAIL_PASS
Environment/config settings used:
EMAIL_FETCH_IMAP_HOST(defaultimap.cachinapp.com)EMAIL_FETCH_IMAP_PORT(default993)EMAIL_FETCH_IMAP_SSL(defaultTrue)- Optionally
EMAIL_FETCH_USER/EMAIL_FETCH_PASSin settings/env if not passing CLI args.
Set the following environment variables (locally via .env, on Railway via Variables):
[email protected]
EMAIL_FETCH_PASS=<secure-password>
EMAIL_FETCH_IMAP_HOST=imap.cachinapp.com
EMAIL_FETCH_IMAP_PORT=993
EMAIL_FETCH_IMAP_SSL=True
Then run without CLI flags:
uv run python backend/manage.py fetch_emails
We added python-dotenv. If a .env file exists at the repo root, settings will load it automatically. Create one from the example:
cp .env.example .env
Now you can run the app and commands without exporting variables manually.
- Connects to IMAP, searches
UNSEENemails. - For each email, matches any recipient address to a user alias (
UserEmailConfig.full_address). - If matched, stores the raw EML and metadata in
UserEmailMessage. - Marks stored messages as
\\Seen.
Local (ejecución puntual)
# Fetch + parse + crear transacciones (tarea completa)
uv run python backend/manage.py shell -c "from expenses.tasks import fetch_emails_task; fetch_emails_task.delay()"
# Solo fetch IMAP (sin parsear a transacciones)
uv run python backend/manage.py fetch_emails
# Solo ingerir mensajes ya almacenados a transacciones/pending
uv run python backend/manage.py ingest_emails
# Limpiar mensajes y pendientes (para reintentar desde cero)
uv run python backend/manage.py clear_useremails
# Descargar un EML por id a stdout
uv run python backend/manage.py download_eml <id>
Local (Celery worker/beat, con Redis)
uv run celery -A misfinanzas worker -l info
uv run celery -A misfinanzas beat -l info # schedule cada 5 minutos
Railway
- Asegura 3 procesos/servicios: web, worker (
celery -A misfinanzas worker -l info), beat (celery -A misfinanzas beat -l info). - Broker/backend:
CELERY_BROKER_URLyCELERY_RESULT_BACKENDapuntando a Redis (REDIS_URL).
- Filter which emails to ingest.
- Parse EML into transactions.
- Background scheduling via Celery and Redis on Railway.