How to properly hash body and headers with PHP for dkim?

百般思念 提交于 2019-12-24 08:55:08

问题


I've gone through Eric Vyncke's PHP dkim script and copied how he was doing hashes for DKIM headers set in emails

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

Would be used as so:

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

However when I validate the email message it tells me that the hash doesn't match.

$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))).';';//...

I'm not sure what I'm missing? How do I properly hash the body and headers so I can set the DKIM values for the header properly?


回答1:


Here is example functions how to send emails with dkim and php mail() function:

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

Works on Gmail.com and multipart/mixed messages with attachments!

<?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();


来源:https://stackoverflow.com/questions/44809574/how-to-properly-hash-body-and-headers-with-php-for-dkim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!