使用PHP代码创建KEY时DKIM验证失败

问题描述 投票:0回答:0

当使用 PHP 代码创建 DKIM 密钥(私有、公共)时,DKIM 验证失败。

<?php

//Set these to match your domain and chosen DKIM selector
$domain = 'deepak.co.in';
$selector = 'cst';

//Private key filename for this selector
$privatekeyfile = $selector .'.' .$domain.'.pem';
//Public key filename for this selector
$publickeyfile = $selector . '_dkim_public.pem';

if (file_exists($privatekeyfile)) {
    echo "Using existing keys - if you want to generate new keys, delete old key files first.\n\n";
    $privatekey = file_get_contents($privatekeyfile);
    $publickey = file_get_contents($publickeyfile);
} else {
    //Create a 2048-bit RSA key with an SHA256 digest
    $pk = openssl_pkey_new(
        [
            'digest_alg' => 'rsa',
            'private_key_bits' => 2048,
            'private_key_type' => OPENSSL_KEYTYPE_RSA,
        ]
    );
    //Save private key

    var_dump($pk);
    //exit;
    openssl_pkey_export_to_file($pk, $privatekeyfile);
    //Save public key
    $pubKey = openssl_pkey_get_details($pk);
    $publickey = $pubKey['key'];
    file_put_contents($publickeyfile, $publickey);

    $privatekey = file_get_contents($privatekeyfile);
}
echo "<pre>"."Private key (keep this private!):\n\n" . $privatekey;
echo "\n\nPublic key:\n\n" . $publickey;

//Prepare public key for DNS, e.g.
//phpmailer._domainkey.example.com IN TXT "v=DKIM1; h=sha256; t=s; p=" "MIIBIjANBg...oXlwIDAQAB"...
$dnskey = "$selector._domainkey.$domain IN TXT";
//$dnsvalue = '"v=DKIM1; h=sha256; t=s; p=';
$dnsvalue = 'v=DKIM1; h=sha256; p=';
//Some DNS servers don't like ;(semi colon) chars unless backslash-escaped
//$dnsvalue2 = '"v=DKIM1\; h=sha256\; t=s\; p=';
$dnsvalue2 = 'v=DKIM1\; h=sha256\; p=';

//Strip and split the key into smaller parts and format for DNS
//Many DNS systems don't like long TXT entries
//but are OK if it's split into 255-char chunks
//Remove PEM wrapper
$publickey = preg_replace('/^-+.*?-+$/m', '', $publickey);
//Strip line breaks
$publickey = str_replace(["\r", "\n"], '', $publickey);
//Split into chunks
$keyparts = str_split($publickey, 253); //Becomes 255 when quotes are included
//Quote each chunk
foreach ($keyparts as $keypart) {
    //$dnsvalue .= '"' . trim($keypart) . '" ';
    //$dnsvalue2 .= '"' . trim($keypart) . '" ';

    $dnsvalue .=trim($keypart);
    $dnsvalue2 .= trim($keypart);
}
echo "\n\nDNS key:\n\n" . trim($dnskey);
echo "\n\nDNS value:\n\n" . trim($dnsvalue);

$first_part_str = substr($dnsvalue, 0,253);

$brk_created_dkim_using_first_part_str = explode($first_part_str, $dnsvalue);
echo "\n\n DKIM TO BE USE \n\n";
echo $to_use_dkim_2048_bit = '"' .$first_part_str. '""'.$brk_created_dkim_using_first_part_str[1].'"';

//echo "\n\nDNS value (with escaping):\n\n" . trim($dnsvalue2);
?>

以上代码参考自

https://github.com/PHPMailer/PHPMailer/blob/master/examples/DKIM_gen_keys.phps
我们只根据需要更改了很少的东西。

当从第三方网站创建 DKIM 密钥时-:

https://www.sparkpost.com/resources/tools/dkim-wizard/

DKIM 在邮箱中传递,但是当使用上面的脚本在邮箱中创建时显示

dkim=perm_fail
。哪里出了问题?

php dkim
© www.soinside.com 2019 - 2024. All rights reserved.