XiaTools

How to Test SPF Records Using Terminal and Command Line Tools

Updated 13 Oct 2026

Testing your Sender Policy Framework (SPF) records via the command line is the most reliable way to verify your email authentication without relying on bloated web interfaces. By querying the Domain Name System (DNS) directly, you see the exact TXT records that receiving mail servers evaluate when your domain sends an email. Whether you are troubleshooting delivery failures to Gmail or setting up a brand-new domain, mastering terminal-based DNS lookups gives you immediate, unfiltered diagnostic data.

To build a flawless record before running your terminal diagnostics, you can use the SPF Record Generator to correctly format your IP addresses and include mechanisms without syntax errors. Let's dive into how to execute these command line tests effectively.

Understanding SPF Records and DNS Architecture

An SPF record is a single DNS TXT record associated with your root domain (e.g., example.com) that explicitly lists the mail servers and IP addresses authorized to send email on behalf of that domain. When a mail transfer agent (MTA) receives an email claiming to be from example.com, it performs a DNS lookup for the domain's TXT records, locates the string starting with v=spf1, and evaluates the mechanisms (like ip4, include, and a) against the sender's connecting IP.

Because email receivers strictly enforce limits—such as the maximum of 10 DNS lookup limits defined in RFC 7208—validating your record structure via the command line ensures you do not inadvertently break your email delivery chain. If your SPF record triggers a permanent error due to too many lookups or syntax typos, receiving servers may reject your messages outright.

Using dig to Query SPF TXT Records

The dig (Domain Information Groper) utility is the industry-standard tool for querying DNS name servers. It is built into macOS and Linux distributions, and is readily available on Windows via PowerShell (as part of RSAT or modern Windows Terminal environments using bundled BIND utilities).

Basic DNS Query for TXT Records

To view all TXT records for your domain, open your terminal and run the following command, replacing example.com with your actual domain name:

dig example.com TXT

Sample Output:

;; QUESTION SECTION:
;example.com. IN TXT

;; ANSWER SECTION:
example.com.      3600    IN      TXT     "v=spf1 ip4:192.0.2.1 include:_spf.example.com ~all"
example.com.      3600    IN      TXT     "google-site-verification=abcdef123456"

Look closely at the output. You will notice multiple TXT records. One contains your SPF string (v=spf1...), while others might be for domain verification or site ownership. A major command line pitfall is having more than one SPF TXT record active on a single domain, which causes an immediate permerror during evaluation.

Querying Specific DNS Nameservers

If you have recently updated your DNS records through your registrar or hosting provider, global propagation delays can skew your results. You can query a specific authoritative nameserver—such as Cloudflare or Google Public DNS—to bypass local caching:

dig @8.8.8.8 example.com TXT

This command forces the Google public resolver to fetch the live record directly from the authoritative nameserver holding your zone file.

Using nslookup for Cross-Platform SPF Testing

If you are on a restricted Windows machine where dig is unavailable, nslookup is pre-installed and provides a quick alternative for checking SPF records.

Running an Interactive or Direct nslookup Query

Open your Windows Command Prompt or PowerShell terminal and enter:

nslookup -type=TXT example.com

Sample Output:

Server:  dns.google
Address:  8.8.8.8

Non-authoritative answer:
example.com       text =
        "v=spf1 ip4:192.0.2.1 include:_spf.example.com ~all"
example.com       text =
        "google-site-verification=abcdef123456"

While nslookup is great for a quick sanity check, keep in mind that its output formatting for multi-string TXT records can occasionally wrap lines or truncate long entries, making dig the preferred tool for complex SPF configurations involving multiple include mechanisms and IPv6 blocks (2001:db8::/32).

PowerShell Methods for Windows Administrators

Windows system administrators working in modern environments can leverage PowerShell's native DNS cmdlets to query records programmatically without third-party utilities.

Using Resolve-DnsName

Open PowerShell and run the following cmdlet:

Resolve-DnsName -Name example.com -Type TXT

Sample Output:

Name             Type   TTL   Section   Strings
----             ----   ---   -------   -------
example.com      TXT    3600  Answer    {v=spf1 ip4:192.0.2.1 include:_spf.example.com ~all}

This output is exceptionally clean and structured as an object, making it easy to pipe into text files or automated monitoring scripts if you manage hundreds of client domains.

Simulating SPF Evaluation with curl and APIs

While DNS queries show you what the record is, they do not tell you whether an incoming mail server will accept or reject a specific IP address. To simulate how a mail server evaluates your SPF record against a sender IP, you can use command line HTTP requests against diagnostic APIs, or inspect mail server logs via openssl and telnet connections.

For an active SMTP handshake test, you can connect directly to your mail server port to check banner responses and TLS configurations:

openssl s_client -connect mail.example.com:25 -starttls smtp

Comparison Table: CLI Tools for SPF Testing

Tool Primary OS Best Used For Handles Long Strings Query Specific Nameserver
dig macOS / Linux / Win Detailed DNS inspection & debugging Excellent Yes (@server)
nslookup Windows / All Quick sanity checks Fair (can wrap) Yes (server ip)
Resolve-DnsName Windows PowerShell Scripting & native Windows queries Excellent Yes (-Server)
curl All Testing against web validation APIs N/A N/A

Common Mistakes and How to Fix Them

When testing SPF records via the command line, engineers frequently encounter specific configuration errors that break email delivery. Here is how to identify and fix them:

  • Multiple SPF Records: If your dig query returns two or more strings starting with v=spf1, receiving servers will instantly discard both and return a permerror. Fix: Log into your DNS provider control panel and consolidate all authorized senders into a single TXT record.
  • Exceeding the 10-Lookup Limit: Using too many nested include statements (e.g., include:spf.a.com include:spf.b.com) forces the validating server to perform more than 10 DNS queries. Fix: Flatten your SPF record by replacing nested includes with direct ip4 and ip6 declarations for your sending infrastructure.
  • Trailing Spaces or Quotes: Accidentally inserting quotation marks inside the DNS record value field can cause parsers to fail. Fix: Ensure the entire string is wrapped in a single set of quotes or let your DNS manager handle quoting automatically.
  • Using ptr or exist Mechanisms: These mechanisms are deprecated or discouraged in RFC 7208 due to performance overhead and slow DNS response times. Fix: Remove them and rely on explicit IP ranges or include statements.

Quick Checklist for Terminal SPF Testing

Before you wrap up your DNS validation process, run through this quick operational checklist:

  1. Run dig domain.com TXT to confirm only one v=spf1 record exists.
  2. Verify that your primary sending IP addresses (e.g., 192.0.2.1) and IPv6 blocks (2001:db8::/32) are explicitly declared.
  3. Check that your record ends with a proper qualifier, typically ~all (softfail) or -all (fail).
  4. Query an external public resolver (@8.8.8.8 or @1.1.1.1) to ensure global DNS propagation is complete.
  5. Confirm your total lookup count remains under the strict 10-lookup threshold.

Frequently asked questions

Why do I see multiple TXT records when I test my SPF record?

It is common for domains to have multiple TXT records for different services, such as Google Workspace verification, DMARC reporting, or domain ownership checks for SSL certificates. However, you should only see one TXT record that begins with 'v=spf1'. Having multiple SPF records violates RFC standards and causes delivery failures.

How do I know if my SPF record exceeds the 10-lookup limit from the command line?

Standard terminal tools like dig only display the text of the record, not the recursive lookups triggered by nested includes. To accurately check your lookup count, you must recursively follow every 'include' statement in your record or use a dedicated DNS evaluation tool to inspect the nested chains.

What is the difference between ~all and -all in my SPF record?

The '~all' mechanism specifies a 'softfail', meaning emails from unauthorized servers are accepted but marked as spam or suspicious. The '-all' mechanism specifies a hard fail, instructing receiving mail servers to outright reject emails originating from unauthorized IP addresses.

Why is my updated SPF record not showing up when I run dig?

This is almost always caused by DNS caching or Time-To-Live (TTL) expiration windows. If you recently updated the record at your DNS provider, your local resolver or ISP is caching the old record; waiting for the TTL to expire or querying a public nameserver directly usually resolves this discrepancy.

Can I use IPv6 addresses in my command line tested SPF record?

Yes, you can include IPv6 addresses using the 'ip6:' mechanism followed by a valid IPv6 CIDR block notation. Receiving mail servers evaluate these blocks against the connecting sender's IPv6 address just as they do with IPv4 blocks.

Related articles

Free tools