@@ -1,35 +1,38 @@
#!/bin/sh
#!/bin/sh
domain="$1"
[ -z "$1" ] && exit
new_domain="$1"
mail_service_domain="$2"
# Check if both domains are provided
[ -z "$new_domain" ] || [ -z "$mail_service_domain" ] && { echo "Usage: $0 new_domain mail_service_domain"; exit 1; }
domain="$1"
subdom="mail"
subdom="mail"
maildomain="$subdom.$domain"
maildomain="$subdom.$mail_service_ domain"
# Add the domain to the valid postfix addresses.
grep -q "^mydestination.*$domain" /etc/postfix/main.cf ||
sed -i "s/^mydestination.*/&, $domain/" /etc/postfix/main.cf
# Add the new domain to the valid postfix addresses.
if ! grep -q "^mydestination.*$new_domain" /etc/postfix/main.cf; then
sed -i "s/^mydestination.*/&, $new_domain/" /etc/postfix/main.cf
fi
# Create DKIM for new domain.
mkdir -p "/etc/postfix/dkim/$domain"
opendkim-genkey -D "/etc/postfix/dkim/$domain" -d "$domain" -s "$subdom"
# Create DKIM for the new domain.
mkdir -p "/etc/postfix/dkim/$new_ domain"
opendkim-genkey -D "/etc/postfix/dkim/$new_ domain" -d "$new_ domain" -s "$subdom"
chgrp -R opendkim /etc/postfix/dkim/*
chgrp -R opendkim /etc/postfix/dkim/*
chmod -R g+r /etc/postfix/dkim/*
chmod -R g+r /etc/postfix/dkim/*
# Add entries to keytable and signing table.
# Add entries to keytable and signing table.
echo "$subdom._domainkey.$domain $domain:$subdom:/etc/postfix/dkim/$domain/$subdom.private" >> /etc/postfix/dkim/keytable
echo "*@$domain $subdom._domainkey.$domain" >> /etc/postfix/dkim/signingtable
echo "$subdom._domainkey.$new_ domain $new_ domain:$subdom:/etc/postfix/dkim/$new_ domain/$subdom.private" >> /etc/postfix/dkim/keytable
echo "*@$new_ domain $subdom._domainkey.$new_ domain" >> /etc/postfix/dkim/signingtable
systemctl reload opendkim postfix
systemctl reload opendkim postfix
# Print out DKIM TXT entry.
# Print out DKIM TXT entry.
pval="$(tr -d '\n' <"/etc/postfix/dkim/$domain/$subdom.txt" | sed "s/k=rsa.* \" p=/k=rsa; p=/;s/\"\s*\"//;s/\"\s*) .*//" | grep -o 'p=.*')"
pval="$(tr -d '\n' <"/etc/postfix/dkim/$new_ domain/$subdom.txt" | sed "s/k=rsa.*p=/k=rsa; p=/;s/\"\s*\"//g ;s/\"\s*.*//g " | grep -o 'p=.*')"
dkimentry="$subdom._domainkey.$domain TXT v=DKIM1; k=rsa; $pval"
dmarcentry="_dmarc.$domain TXT v=DMARC1; p=reject; rua=mailto:dmarc@$domain; fo=1"
spfentry="$domain TXT v=spf1 mx a:$maildomain -all"
mxentry="$domain MX 10 $maildomain 300 "
dkimentry="$subdom._domainkey.$new_ domain TXT v=DKIM1; k=rsa; $pval"
dmarcentry="_dmarc.$new_ domain TXT v=DMARC1; p=reject; rua=mailto:dmarc@$new_ domain; fo=1"
spfentry="$new_ domain TXT v=spf1 mx a:$maildomain -all"
mxentry="$new_ domain MX 10 $maildomain"
echo "$dkimentry
echo "$dkimentry
$dmarcentry
$dmarcentry
@@ -41,4 +44,4 @@ echo "$dkimentry
$dmarcentry
$dmarcentry
$spfentry
$spfentry
$mxentry"
$mxentry"
echo "They have also been stored in ~ /dns_emailwizard_added"
echo "They have also been stored in $HOME /dns_emailwizard_added"