How To Add DomainKeys and SPF Records on CPanel Servers

This how to describe how to add domainkeys and SPF (Sender Policy Framework) records on CPanel Servers. Whenever you create a domain on CPanel server using WHM, it wont add domainkeys and SPF records for particular domains. To add domainkeys and SPF records please use following steps.

We consider here we have a domain sohailriaz.com with cpanel username sohailri

Please note on CPanel servers most of the work is done using cpanel username which then reflect setting on particular domains. If you have several domains / sub domains under one username, it will add domainkeys and SPF records for all domains / subdomains.

Execute the following command on your CPanel server using root.

1 DomainKeys

1.1 Add DomainKeys for single user

/usr/local/cpanel/bin/domain_keys_installer sohailri

where sohailri is a username

1.2 Add DomainKeys for all CPanel users.

If you wish to add DomainKeys for all Cpanel users on your server, execute following command.

ls /var/cpanel/users | while read a; do
/usr/local/cpanel/bin/domain_keys_installer $a
done

2 SPF (Sender Policy Framework)

2.1 Add SPF for single account.

/usr/local/cpanel/bin/spf_installer sohailri

2.2 Add SPF for all Cpanel accounts.

If you want to add SPF records for all cpanel accounts exist on your server then use the following command.

ls /var/cpanel/users | while read a; do
/usr/local/cpanel/bin/spf_installer $a
done

3 Enable DomainKeys and SPF for every new CPanel account.

If you want to add DomainKeys and SPF for every new account created on Cpanel, then do the following.

vi /scripts/postwwwacct

postwwwacct is a file which execute after wwwacct (used to create account), paste following lines in the file

#!/usr/bin/perl

my %OPTS = @ARGV;
$ENV{USER} = “$OPTS{‘user’}”;
system q(/usr/local/cpanel/bin/domain_keys_installer $USER);
system q(/usr/local/cpanel/bin/spf_installer $USER);

Happy Mailing 🙂

If you have any question / suggestion please comment.

Related Posts

27 thoughts on “How To Add DomainKeys and SPF Records on CPanel Servers

  1. The scripts which i used is developed by CPanel and they uses users as a input to install SPF or Domainkey for the specific user.
    You can see the /var/cpanel/users directory and check for any user for your specific hosting package. You should see some unique in that user file for that hosting package.
    Then you have to developed your own script which first take out the users name which are in any specific hosting package and then you can run above commands on those users to generate SPF and DomainKeys.

  2. admin said :
    Then you have to developed your own script which first take out the users name which are in any specific hosting package and then you can run above commands on those users to generate SPF and DomainKeys.

    thats exactly what im asked before 😀

    the main idea is, i want to execute other script [not spf-ing] while creating new hosting account. and the script only apply for specific hosting plan [package].

    1. is there any clue on what file i have to attach my script?
    2. what keyword i should use for comparison?

    thanks

  3. cat /var/cpanel/users | while read a; do /usr/local/cpanel/bin/domain_keys_installer $a; done

    The above command does not work with me:
    /var/cpanel/users: is a directory

    Any ideas?

  4. @Osama: Sorry for mistake the command is like this
    ls /var/cpanel/users | while read a; do /usr/local/cpanel/bin/domain_keys_installer $a; done
    ————-
    I have fixed in the blog too. Please use the command as above and go with post again, it will execute fine.
    ————–
    Regards,

  5. @Sohail: thanks for the correction. I had already ran it by using *for* instead of *while*.
    —–
    for user in ls /var/cpanel/users; do /usr/local/cpanel/bin/domain_keys_installer $user; done
    —-

  6. Great guide. Thank you for writing it, but the code provided to place in postwwwacct for adding domain keys and spf records for all newly created accounts did not work for me in CENTOS 5.5 x86_64

    The code below did work:

    #!/usr/bin/perl

    print “\nStarting install of Domainkeys\n”;
    my %OPTS = @ARGV;
    my $user = $OPTS{‘user’};
    system(“/usr/local/cpanel/bin/domain_keys_installer”,$user);
    system(“/usr/local/cpanel/bin/spf_installer”,$user);
    print “\nDone with install of Domainkeys\n”;exit;

    Also, after inserting the code you can test the syntax using this code:

    perl -c postwwwacct

  7. I added Mark’s suggested code to the postwwwacct file using vi, but upon testing with the perl -c command I got some compilation errors, plus there was already a lot of stuff in the file. Any suggestions?
    ————————————————————
    root@server [~]# vi /scripts/postwwwacct
    iptaglist = parsedipDOC.getElementsByTagName(‘ip’)
    iplist =[]

    q = 0
    while q /dev/null 2>&1″, shell=True)
    output = proc.communicate()

    #!/usr/bin/perl

    my %OPTS = @ARGV;
    $ENV{USER} = .$OPTS{.user.}.;
    system q(/usr/local/cpanel/bin/domain_keys_installer $USER);
    system q(/usr/local/cpanel/bin/spf_installer $USER);

  8. Top Half of postwwwacct file
    ———————————————–
    root@server [~]# vi /scripts/postwwwacct
    iptaglist = parsedipDOC.getElementsByTagName(‘ip’)
    iplist =[]

    q = 0
    while q < len(iptaglist):
    iplist.append(str(iptaglist[q].childNodes[0].toxml()))
    q = q + 1

    f = open('/var/cpanel/userdata/' + newuser + '/main')
    ydata = yaml.load(f)
    f.close()
    sublist = ydata['sub_domains']

  9. middle
    ——————————
    addondict = ydata[‘addon_domains’]
    parkedlist = ydata[‘parked_domains’]
    domainlist = ydata[‘main_domain’]
    domain = domainlist
    docroot, yip, alias = createvhosts.getvars(domain)
    if yip == iplist[0]:
    createvhosts.writeconfshared(newuser, domain, docroot, yip, alias)
    else:
    createvhosts.writeconfded(newuser, domain, docroot, yip, alias)

    proc = subprocess.Popen(“/etc/init.d/nginx restart > /dev/null 2>&1”, shell=True)
    output = proc.communicate()

  10. end
    ——————-

    #!/usr/bin/perl

    my %OPTS = @ARGV;
    $ENV{USER} = .$OPTS{.user.}.;
    system q(/usr/local/cpanel/bin/domain_keys_installer $USER);
    system q(/usr/local/cpanel/bin/spf_installer $USER);
    ——————————–
    I have also tried it taking out the #!/usr/bin/perl part. Still getting the following errors

    root@server [~]# perl -c /scripts/postwwwacct
    Semicolon seems to be missing at /scripts/postwwwacct line 6.
    Semicolon seems to be missing at /scripts/postwwwacct line 7.
    syntax error at /scripts/postwwwacct line 7, near “v ”
    /scripts/postwwwacct had compilation errors.

  11. Okay one problem I noticed for all others purposes..

    the ” and ‘ were replaced with .’s upon copy and pasting the code so watch to make sure the code doesn’t change. Still need help with the compilation error.

  12. i´m looking to add the script to ALL new domains, but i don´t have the /scripts/postwwwacct, i only have the /scripts/wwwacct and /scripts/wwwacct2

    How can i do that? i´m using cPanel 11.28.87

    Thanks

  13. will this update serials in DNS including cPanel clustered DNS nature? If not, what will you suggest?

  14. Just was searching for this automation. Thanks for posting it online.
    By the way, will adding SPF ensure that messages are not sent to JunkMail?

  15. Thanks for the great doc! Very helpful. One change:

    The cPanel command for domain keys has changed from:

    /usr/local/cpanel/bin/domain_keys_installer

    to

    /usr/local/cpanel/bin/dkim_keys_install

  16. Hi,
    Is there a way to sign outgoing emails with domainkeys in cpanel. My emails header always shows “DKIM: pass, SPF: pass, DMARC: pass. **domainkey signature: neutral (no signature)

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *