Add SPF records to all domain on Plesk server

Sender Policy Framework (SPF) is an email validation system designed to prevent email spam by detecting email spoofing, a common vulnerability, by verifying sender IP addresses. SPF allows administrators to specify which hosts are allowed to send mail from a given domain by creating a specific SPF record (or TXT record) in the Domain Name System (DNS). Mail exchangers use the DNS to check that mail from a given domain is being sent by a host sanctioned by that domain’s administrators.

Plesk stores all the information including DNS entries in database. So if you manually edit the zone files and add the SPF record it will be wiped out when DNS record is updated next time. So You will have to add the SPF records for the domains from the Plesk interface. For a few domain you can do that manually but if you have to add SPF records for all the domain then it’s going to be a problem. You can run the below commands to add SPF records to all the domain on the server.

First of all add the SPF entry in the database

mysql -u admin -p`cat /etc/psa/.psa.shadow` psa -e "select dns_zone_id,displayHost from dns_recs GROUP BY dns_zone_id ORDER BY dns_zone_id ASC;" | awk '{print "INSERT INTO dns_recs (type,host,val,time_stamp,dns_zone_id,displayHost,displayVal) VALUES ('\''TXT'\'','\''"$2"'\'','\''v=spf1 a mx ~all'\'',NOW(),"$1",'\''"$2"'\'','\''v=spf1 a mx ~all'\'');"}' | mysql -u admin -p`cat /etc/psa/.psa.shadow` psa

Then force plesk to update all the dns zone files.

mysql -Ns -uadmin -p`cat /etc/psa/.psa.shadow` -D psa -e 'select name from domains' | awk '{system("/usr/local/psa/admin/bin/dnsmng --update "$1)}'