How to Harden Email Security on a Production VPS for Claude Code
Share your love
A growing number of teams now run an agentic coding assistant on the same server that sends their mail, and that convenience has a sharp edge. Claude Code executes commands at machine speed, so a loose mail setup can turn a production box into a spam relay within hours.
An exposed or unauthenticated mail service invites relay abuse, spoofing, and a wrecked sending reputation long before anyone notices the traffic.
There is a subtler threat, too. An agentic tool can be steered by a prompt injection buried in the data it reads, then coerced into abusing local mail commands or mailing credentials to an attacker. Hardening the stack means layering authentication, encryption, isolation, and least privilege, so a single bad instruction cannot escalate into an outbound leak.
1. Establish Domain Authentication First
Lock your domain's identity in DNS before you touch any local mail software. Receiving servers lean on three records to decide whether your messages are genuine, which makes email authentication the foundation of everything that follows. Begin with a Sender Policy Framework (SPF) record that names only the IPs allowed to send for your domain, closed with a -all hard fail so every other source is rejected outright.
Add DomainKeys Identified Mail (DKIM) next, signing outbound messages with a 2048-bit key and publishing the matching public key under a selector in DNS. Tie both signals together with DMARC: begin at p=none to gather reports without disrupting delivery, confirm that legitimate mail passes, then tighten to p=reject once the data is clean.
Point DMARC's aggregate reports at a mailbox you actually monitor, because they surface both spoofing attempts and your own senders that quietly fail authentication. Finish with a reverse DNS (PTR) record so your IP resolves back to its fully qualified domain name, a check many receivers run before trusting a byte.
2. Restrict the Mail Transfer Agent
If local scripts rely on a mail transfer agent (MTA) to relay notifications, constrain the daemon rather than leaving defaults in place. Bind it to the loopback interface only, so it never listens on a public address. Enforce modern transport encryption by requiring TLS 1.2 or higher on every outbound path.
Disable open relaying by permitting only localhost to inject mail, require a proper HELO handshake, and switch off the verify command to block username enumeration. Each setting closes a door that automated scanners test around the clock.
3. Contain Claude Code With Least Privilege
Isolation is where agentic risk is won or lost, so decide early how much the assistant may touch. Run it under a dedicated, non-root user and treat a production VPS for Claude Code as space to compartmentalize, not share. Keep that account out of mail-management groups, deny it a broad shell, and sandbox the agent so it cannot reach the host's mail queue.
The sharper danger is indirect prompt injection. A poisoned comment in a pulled file could tell the agent to pipe its stored tokens or an .env file into a mail command. Counter it with a managed permission policy that denies the mail binaries outright and blocks reads of credential, .env, and key files. Sanitize any external string before it reaches a header or body, since unsanitized input is the root of SMTP header injection.
4. Tighten the Firewall
Treat the host firewall as a second wall behind your mail rules. If the server only sends outbound notifications, deny all inbound traffic on the mail ports (25, 465, and 587) so nothing can probe a listener you never meant to expose.
Most providers already block outbound port 25 to curb spam, and that default is worth keeping. Leave it closed, and route outbound mail only over an authenticated submission port to your chosen relay, never to the open internet. Rate-limiting new outbound connections helps as well, so a hijacked process cannot blast thousands of messages before the spike surfaces in your logs.
5. Offload Mail to an Authenticated Relay
The most resilient design is to run no local mail server at all. Disable the local MTA, then route every programmatic message through an authenticated SMTP relay over an encrypted submission port.
A dedicated sending service keeps your server's IP out of the deliverability firing line and centralizes abuse controls. Store the relay credentials in an isolated secrets file locked down with chmod 600, readable by the agent's user and no one else, so a compromised process on the box cannot simply scoop them up.
Conclusion
Email on an agent-driven server is safe only when several layers agree. Authentication proves who you are, encryption guards the transport, a loopback-bound MTA shrinks the attack surface, and least privilege keeps a hijacked prompt from reaching anything sensitive.
The throughline is restraint: grant the narrowest access that still lets the work happen. Build the stack this way, and your assistant can send what it needs to send without ever becoming the weak point an attacker was hoping to find.



