Web Form to CRM in Two Seconds: Wiring Wix, n8n, Mailgun and Odoo 18 (and a Bug That Hid for Seven Weeks)
Vicente Louvet
- 11 minutes read - 2164 wordsI am a one-person consultancy. One of my clients is a sign shop whose quotes, leads and invoices live in a single self-hosted Odoo 18 instance on a small VPS, which is the source of truth for both my company and theirs as two companies in one database. The automation glue is n8n (2.x, Postgres 17, pinned images) in Docker on the homelab AI box .62, reachable from the internet only through a Cloudflare Tunnel on a dedicated webhooks hostname. Outgoing mail goes through Mailgun; pushes land on my phone through ntfy.
This post is not a tutorial on any one product. It is the plumbing between them, and it is three bugs that each took a real lead or a real week to surface: a DMARC policy that bounced the very first email about a new lead, an inbound-mail route that would have silently eaten every reply, and an n8n trap I want to tattoo somewhere, where a scheduled job failed every morning for seven weeks while the identical workflow passed every time I ran it by hand.
Everything here is exported into an infra-as-code repo, in the spirit of my earlier write-up on backing up a homelab as code, so none of this is a story about remembering how it was set up. It is the story of what the plumbing does, and the three places it quietly broke.
The setup #
Two companies, one Odoo database, one automation box, one tunnel in front of it all.
| Piece | Where | Role |
|---|---|---|
| Odoo 18 | small VPS | CRM/ERP, source of truth for both companies |
| n8n 2.x | Docker on .62 | automation glue; the workflows live here |
| Cloudflare Tunnel | webhooks hostname | the only way in from the internet |
| Mailgun | outbound + one inbound route | every message in or out of the system |
| ntfy | my phone | the push notifications |
| Postgres 17 | alongside n8n | n8n’s own store, and the star of the seven-week bug |
The tunnel is the interesting bit of network topology, because a self-hosted n8n that exposes its login page to the world is a liability. So the tunnel’s ingress only forwards paths matching one expression:
^/webhook/(wix|mailgun)-
Every other path gets a 404 at Cloudflare’s edge. I verified this from outside: / and /rest/login are bare 404s, and a POST to an unregistered wix- path returns n8n’s own “not registered” JSON. That proves the routing without exposing a single page of n8n to the internet. The login page has never been reachable from outside.
Web form to lead in two seconds #
The client’s Wix site has a quote-request form. A Wix Automation (“Send HTTP request”) POSTs each submission to the tunnel. The webhook path is /webhook/wix-quote-<16 random hex>. The random suffix is the shared secret, and it sits behind the ingress regex, so n8n never has to authenticate it itself.
When a submission lands, a 19-node workflow does the work:
- Normalise
body.data. - Look up
crm.leadby a custom fieldx_wix_submission_id. That is the dedupe key, and it is the same key the backup poller uses. - Create the lead in the client’s company.
- Match a partner by
email_normalized, thenphone_sanitized, then company namelike.
The three outcomes of the match are the careful part:
- Email or phone hit → link the partner and convert the lead to an opportunity.
- Name-only hit → leave it as a lead, with a note saying “convert by hand”. A company with the same name is not proof.
- No hit → create the company (website taken from a non-freemail domain, city from the form) and a contact, link everything, convert, and post a chatter note with the raw JSON for audit.
It finishes with an ntfy push carrying a click-through link straight to the new lead.
Webhooks are lossy by nature, so there is a backup poller: it reads the Wix CMS collection hourly for the last 48 hours, using the same x_wix_submission_id dedupe key. A dropped webhook still becomes a lead. This was proven with a real submission the day it went live.
One note for anyone replaying this: Wix matched my test submission to my own existing contact by phone number. Test with a number you do not mind being linked.
Three n8n details are worth a line each, because each one quietly ate time:
- The Odoo node’s
fieldsListoption is a comma-separated string, not an array. - many2one fields come back as
[id, name], not a bare id. - Expression values keep their JS types.
"={{ 2 }}"is a number, which a many2one field needs.
The DMARC bounce #
The hook went live, and within hours the first real web lead arrived. It was assigned to me, Odoo dutifully emailed me “You have been assigned…”, and Gmail bounced it back with 5.7.26 and a DMARC policy line. My company domain publishes p=reject, and its SPF lists only my mail provider. Odoo had used the author’s address (the bot user, which lives at my company domain) as the From, sent through a Mailgun SMTP server that was authorised for a different domain, the Odoo host’s own. No alias domain was configured, so Odoo had no default From address to fall back to. Everything conspired to send mail that looked forged.
The fix had three parts:
- Create a
mail.alias.domainfor the Odoo host’s domain, withdefault_from=notifications. - Give it a bounce alias and a catchall alias. The catchall is a required field in Odoo 18; a write with
Falseis refused outright. - Set that domain on both companies, and set the outgoing server’s
from_filterto it.
Now outgoing mail goes out as "bot" <notifications@…>, with the bounce address as the envelope sender, and Gmail answers 2.0.0 OK. The assignment email landed.
But the fix had a side effect that became the next section’s problem. With an alias domain in play, Odoo sets Reply-To to the catchall or the sales-team alias, both at a domain whose MX pointed at Mailgun with no routes configured. Any customer who replied would have been silently dropped, and nobody would have noticed, because a drop is silent.
And one Mailgun trap worth its own paragraph. Every API call I made from home returned 401 Invalid private key for a key I knew was valid. The account had an IP allowlist containing only the VPS. The same key, from the VPS, worked on the first try. The allowlist gates SMTP too, which is why the VPS was already on it.
Mail back into Odoo #
Inbound mail is a Mailgun route that matches .*@<odoo domain>, and it does two forward() actions plus a store(): one forward goes to an n8n webhook URL ending in /mime, which makes Mailgun post the raw MIME instead of parsed fields. The n8n Code node then calls Odoo over JSON-RPC, mail.thread.message_process, with that MIME in the body.
It works, and I proved both halves:
- A reply carrying
In-Reply-Tothreads straight into the lead’s chatter. - Mail to the sales alias creates a new lead with the right company and owner, set via
alias_defaults.
The trap here is subtle. Odoo 18 writes alias_status = 'valid' on the alias during processing, without sudo, so a non-admin bot user gets “not allowed to modify Email Aliases” and the whole message rolls back. The fix is to set every named alias to valid once, as admin. And because writing any alias field resets the status to not_tested, you must re-apply the fix after any alias edit. It is a one-time cost that will silently come back to bite later.
Mailgun’s free plan allows exactly one inbound route, so my single route carries two forward() actions — the /mime one for alias mail and a parsed-fields hook feeding the inbox-triage workflow. Mixed destinations on one route work fine.
A curl trap finished the job. A -F value that starts with < (an RFC 2822 Message-Id) is read as a filename, not a value. Use --form-string:
# a test message through the Mailgun API; the Message-Id value starts with "<"
curl -s --user "api:$MAILGUN_API_KEY" "https://api.mailgun.net/v3/<odoo domain>/messages" \
-F from='Test <test@<odoo domain>>' -F to='info@<odoo domain>' \
-F subject='route test' -F text='hello' \
--form-string 'h:Message-Id=<route-test-1@localhost>'
The bug that hid for seven weeks #
This one deserves the most space, because I got it wrong twice before I got it right.
A daily 07:00 workflow ran a dry-run price comparison between Odoo and the client’s web store. It failed every single morning from 2026-09-16 to 2026-11-03 with fieldsRaw.split is not a function — while every manual run from the editor passed, with no edit in between.
On day one I “fixed” it. Because of that earlier fieldsList gotcha, I patched the workflow with a SQL UPDATE to its nodes JSON, turning the array into a string. And GET /api/v1/workflows/{id} showed the fix was there. The error workflow still pushed an alert each morning; I assumed it was a flake. I even spent time on a wrong theory about the Odoo node’s type coercion before I gave up and looked at the actual data.
The actual cause is an n8n 2.x design decision that is brutal:
n8n 2.x triggers run the PUBLISHED version, stored in workflow_history and pointed to by workflow_entity.activeVersionId. The entity’s nodes column is the draft.
So a SQL patch, or an import, that only touches the entity changes the draft. Manual runs use the draft and pass. Scheduled runs use the published copy and fail. And the public API returns the draft, so it looks fixed from every angle except the one that matters. That is not a config bug; it is a versioning model I had simply assumed away.
The detection is a single join:
SELECT w.id, w.nodes::jsonb = h.nodes::jsonb AS published_matches_draft
FROM workflow_entity w
JOIN workflow_history h ON h."versionId" = w."activeVersionId";
false means unpublished edits. Four of my eleven workflows were drifted, not just the price-comparison one. This query is worth running after every manual edit or import.
The fix is one API call:
curl -X PUT ".../api/v1/workflows/{id}?publishIfActive=true" \
-H "X-N8N-API-KEY: $N8N_API_KEY" \
-d '{"name": "...", "nodes": [...], "connections": [...], "settings": {...}}'
Republishing wrote the draft I had patched into the published history. But I did not want to wait until tomorrow’s schedule to know it worked, so I reran the failed execution against the now-published copy inside the live process:
POST /api/v1/executions/{failed-id}/retry
{"loadWorkflow": true}
It passed, 71 of 71 products matched. The scheduled job has run clean every morning since.
“it works when I run it by hand” is a symptom, not a reassurance.
XML-RPC habits #
Driving Odoo over XML-RPC from small scripts has left me with a short list of rules I now treat as automatic:
- Several Odoo methods return
None:action_quotation_sent,action_set_lost,mail.mail.send. Over XML-RPC that raises a “cannot marshal None” fault after the write has committed. On any exception, re-read the record state; never trust the exception alone. message_postHTML-escapes its body over RPC. If you need real HTML, write thebodyfield afterwards.- A sale order’s
idis not its number. Readname; never infer it. - Every job script is dry-run by default, and diffs its lines against the live order before
--commit. In the first week that caught a quote whose price had been changed by hand in the UI and would have been silently reverted by a stale script.
Reports to my phone #
The funnel is a cron of n8n workflows pushing to ntfy: new leads every 30 minutes (silent when there are none), overdue invoices daily at 08:00, stale quotes Monday 08:15, Mailgun failed/rejected/complained events every half hour, and an error-trigger workflow that pushes a direct link to the failing execution. The Mailgun one must poll rather than batch, because the free plan keeps event logs for only about a day. And none of this survives losing the box, so nightly pg_dump plus a tar of the n8n data directory goes to the NAS and from there offsite.
Takeaways #
- n8n 2.x scheduled runs execute the published workflow, not the draft. Edit via import or raw SQL and every scheduled job diverges from what you see in the editor and the API.
- “It works when I run it by hand” is a symptom, not a reassurance. A job that fails only on the schedule is almost always running different code than you are.
- DMARC does not care that the sender is you. A
p=rejectdomain and a bot user with no configured default From will bounce the notification about your first real lead. Alias domain,from_filter, and both required alias fields. - Inbound mail has several silent failure modes. Reply-To pointing at a domain with an MX record but no routes drops customers silently, and Odoo’s alias status reset drops messages unless every alias is set valid as admin.
- Publish the whole config as code, then test the reply path. The first week’s dry-run diff caught a hand-edited quote before it was reverted, and a real reply proved the inbound route.
- Run the version-consistency SQL after any manual edit or import of a workflow; drift is the norm, not the exception.