To configure SPF (Sender Policy Framework) and DKIM (DomainKeys Identified Mail) for your email server, follow these steps:
1. Configure SPF Record
SPF allows you to specify which mail servers are authorized to send email for your domain.
Step 1.1: Add an SPF Record
You need to create a DNS TXT record for your domain. This record specifies which servers can send emails on behalf of your domain.
Example:
Your SPF record would look like this:
v=spf1
: Indicates the version of SPF.ip4:YOUR_PUBLIC_IP
: Specifies the authorized server’s IP address.-all
: Indicates that all other servers are unauthorized.
Add this record to your DNS zone file:
- Log in to your DNS management console.
- Add a new TXT record:
- Name:
@
(or your domain nameportalapp.link
). - Value:
v=spf1 ip4:YOUR_PUBLIC_IP -all
. - TTL:
3600
seconds (or default).
- Name:
- Save the changes.
Step 1.2: Verify the SPF Record
Use an online SPF checker or run the following command to verify:
You should see your SPF record in the output.
2. Configure DKIM
DKIM uses cryptographic signatures to verify that emails have not been altered.
Step 2.1: Generate DKIM Keys
Use the opendkim
package to generate DKIM keys.
- Install opendkim:
- Generate a Key Pair: Replace
default
with a selector name of your choice.This will generate two files:
default.private
: Your private key (used by the server).default.txt
: The public key (added to your DNS).
- Move the Private Key: Move the private key to a secure location (e.g.,
/etc/opendkim/keys
):
Step 2.2: Configure OpenDKIM
Edit the OpenDKIM configuration files.
- Edit
/etc/opendkim.conf
: Add or modify the following lines: - Edit
/etc/default/opendkim
: Uncomment or set the following line to match the socket inopendkim.conf
: - Restart OpenDKIM:
Step 2.3: Add DKIM Record to DNS
- Open the
default.txt
file created earlier:It will look something like this:
- Add a DNS TXT record:
- Name:
default._domainkey
. - Value: Paste the entire value from
default.txt
. - TTL:
3600
.
- Name:
- Save the changes.
3. Test SPF and DKIM
After updating DNS records, verify SPF and DKIM using online tools:
- SPF Tester: https://mxtoolbox.com/spf.aspx
- DKIM Tester: https://mxtoolbox.com/dkim.aspx
4. Optional: Configure DMARC
DMARC allows you to specify how mail servers handle emails failing SPF or DKIM checks.
- Add a DNS TXT record for DMARC:
- Name:
_dmarc
. - Value:
- TTL:
3600
.
- Name:
- Save the changes.
5. Restart Postfix
Finally, restart Postfix to apply all changes:
Your server should now be configured to send emails with SPF, DKIM, and optionally DMARC validation. Let me know if you encounter any issues!