Yes, you absolutely can. An SPF record is designed to be flexible, allowing you to specify all your authorized mail servers in a single record, regardless of whether they use IPv4 or IPv6 addresses. It is standard practice to include both ip4 and ip6 mechanisms within the same SPF TXT record.
Sender Policy Framework (SPF) is a crucial email authentication standard that helps protect your domain from being used for phishing and spoofing. It works by creating a DNS record that lists which IP addresses are permitted to send email on behalf of your domain. The ip4 and ip6 mechanisms are the most direct way to do this, specifying exact IPv4 and IPv6 addresses.
If you use different services to send emails, and some use IPv4 while others use IPv6, you will need to list them all in your single SPF record to ensure legitimate emails are properly authenticated.
Combining ip4 and ip6 mechanisms is straightforward. You simply list them one after another, separated by spaces. All SPF mechanisms and modifiers are placed within a single string in your DNS TXT record.
Here is an example of a valid SPF record that authorizes one IPv4 address and one IPv6 address:
v=spf1 ip4:192.0.2.1 ip6:2001:db8::1 -all
Let's break this down:
You can add multiple addresses of each type and also specify IP ranges using CIDR notation, for instance ip4:192.0.2.0/24. As noted by URIports, the maximum CIDR length for IPv4 is 32 and for IPv6 is 128.
While combining IP versions is simple, there are critical rules for SPF records that you must follow to ensure your email authentication works correctly.
In conclusion, using both ip4 and ip6 in your SPF record is a fundamental part of creating a complete and accurate email authentication policy. Just remember to keep all your rules in a single record and stay within the protocol's limits.