How to Integrate Namecheap DNS with Cloudflare for Email Forwarding While Using Cloudflare’s CDN


6 views

Many developers face this dilemma: Cloudflare's superior CDN performance vs Namecheap's email forwarding capability. Here's how to get the best of both worlds without full DNS migration.

You'll need to modify these essential records:

; Cloudflare-required records
@ 300 IN A 192.0.2.1 (your origin server IP)
www 300 IN CNAME yourdomain.com.

; Namecheap email records
@ 300 IN MX mail.namecheap.com.
@ 300 IN TXT "v=spf1 include:namecheap.com ~all"
_dmarc 300 IN TXT "v=DMARC1; p=none; rua=mailto:you@yourdomain.com"

1. In Namecheap's DNS management:

- Keep all MX, TXT, and SPF records
- Remove A/CNAME records for services going through Cloudflare
- Add Cloudflare nameservers (ns1.cloudflare.com, ns2.cloudflare.com)

2. In Cloudflare dashboard:

- Add only the A/CNAME records for web services
- Set DNS records to "DNS only" (grey cloud) for email-related entries
- Enable proxy (orange cloud) for web traffic

For a domain using WordPress and GSuite:

; Namecheap retains:
@ 3600 IN MX 1 aspmx.l.google.com.
@ 3600 IN MX 5 alt1.aspmx.l.google.com.
@ 3600 IN TXT "google-site-verification=abc123"

; Cloudflare handles:
@ 300 IN A 104.28.0.1
www 300 IN CNAME yourdomain.com.
api 300 IN A 104.28.0.2

DNS Propagation Delays: Use dig +trace yourdomain.com to verify paths

Email Delivery Problems: Check SPF/DKIM alignment with:

nslookup -type=txt yourdomain.com
nslookup -type=mx yourdomain.com

For microservices architectures:

; Split traffic between providers
app 300 IN A 192.0.2.1 (Cloudflare-proxied)
legacy 300 IN A 198.51.100.1 (Namecheap-direct)

When you need Cloudflare's performance and security features but must retain Namecheap's email forwarding capability, a hybrid DNS configuration becomes necessary. The key is selectively delegating only certain record types to Cloudflare while keeping others with Namecheap.

For most websites, these are the critical records that should point to Cloudflare:

; A record for root domain
@ 1800 IN A 192.0.2.1 (Cloudflare's IP)

; A record for www subdomain
www 1800 IN A 192.0.2.1 

; CNAME for other subdomains
api 1800 IN CNAME yourdomain.com.cdn.cloudflare.net

Maintain these records in Namecheap for email functionality:

; MX records for email
@ 1800 IN MX 10 mail.yourdomain.com.
@ 1800 IN MX 20 mail2.yourdomain.com.

; TXT records for SPF/DMARC/DKIM
@ 1800 IN TXT "v=spf1 include:_spf.yourdomain.com ~all"
_dmarc 1800 IN TXT "v=DMARC1; p=none; rua=mailto:dmarc@yourdomain.com"

1. In Namecheap DNS settings, remove only the A/CNAME records for services you want Cloudflare to handle
2. In Cloudflare DNS, add those same records pointing to Cloudflare's infrastructure
3. Verify all remaining MX/TXT records stay intact in Namecheap

For those managing multiple domains, consider this Python script to automate record updates:

import requests

def update_dns_records(domain, cf_api_key, namecheap_api_key):
    # Cloudflare API call to add A record
    cf_headers = {
        'Authorization': f'Bearer {cf_api_key}',
        'Content-Type': 'application/json'
    }
    cf_data = {
        "type": "A",
        "name": domain,
        "content": "192.0.2.1",
        "ttl": 1800,
        "proxied": True
    }
    requests.post(
        f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records',
        headers=cf_headers,
        json=cf_data
    )
    
    # Similar calls for other record types
    ...

After setup, verify with these commands:

dig +short A yourdomain.com
dig +short MX yourdomain.com
nslookup -type=TXT _dmarc.yourdomain.com

- Ensure TTL values are low (300-600) before making changes to minimize downtime
- Double-check Cloudflare's proxy status (orange cloud icon) for A/CNAME records
- Remember Cloudflare has strict CNAME flattening requirements for root domains