【问题标题】:How to properly hash body and headers with PHP for dkim?如何使用 PHP for dkim 正确散列正文和标头?
【发布时间】:2017-06-28 18:00:17
【问题描述】:

我浏览了 Eric Vyncke 的 PHP dkim script 并复制了他如何为电子邮件中设置的 DKIM 标头进行哈希处理

$DKIM_bh = base64_encode(pack('H*', sha1($body)));

会这样使用:

'bh='.$DKIM_bh.';'//...

但是当我validate the email message 它告诉我哈希不匹配。

$headers .= 'DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/simple; q=dns/txt; bh=';//...

$body = 'Test PHP+SMTP authenticated email,3.';

bh='.base64_encode(pack('H*', sha1($body))).';';//...

我不确定我错过了什么? 如何正确地散列正文和标头,以便正确设置标头的 DKIM 值?

【问题讨论】:

    标签: php hash dkim


    【解决方案1】:

    以下是如何使用 dkim 和 php mail() 函数发送电子邮件的示例函数:

    https://github.com/breakermind/PHP-DKIM

    适用于 Gmail.com 和带附件的多部分/混合邮件!

    <?php
    // Send simple email without DKIM SIgning
    // YOUR E-MAIL
    $name = 'Name Jony';
    $sender = 'sdsdso@domain.pl';
    $to = 'ffffu@gmail.com';
    $subject = 'Simple html message with dkim';
    
    $headers =
    'MIME-Version: 1.0
    From: "'.$name.'" <'.$sender.'>
    Content-type: text/html; charset=utf8';
    
    $message =
    '<html>
        <header></header>
        <body>
            Hello, this a DKIM test e-mail
        </body>
    </html>';
    
    // NOW YOU WILL DO (after setting up the config file and your DNS records) :
    // Make sure linefeeds are in CRLF format - it is essential for signing
    $message = preg_replace('/(?<!\r)\n/', "\r\n", $message);
    $headers = preg_replace('/(?<!\r)\n/', "\r\n", $headers);
    
    require_once 'mail-signature.class.php';
    require_once 'mail-signature.config.php';
    
    $signature = new mail_signature(
        MAIL_RSA_PRIV,
        MAIL_RSA_PASSPHRASE,
        MAIL_DOMAIN,
        MAIL_SELECTOR
    );
    $signed_headers = $signature -> get_signed_headers($to, $subject, $message, $headers);
    
    // Send email
    ini_set('sendmail_from', $sender);
    echo mail($to, $subject, $message, $signed_headers.$headers);
    die();
    

    【讨论】:

    猜你喜欢
    • 2011-03-09
    • 2020-03-26
    • 2020-09-12
    • 2011-07-10
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多