如何正确使用PHP为dkim散列正文和标题? [英] How to properly hash body and headers with PHP for dkim?

查看:143
本文介绍了如何正确使用PHP为dkim散列正文和标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了Eric Vyncke的PHP dkim脚本并复制了他为电子邮件中设置的DKIM标头设置了哈希值。



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

可以这样使用:

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

然而,当我验证电子邮件时,它告诉我该散列没有'

  $ headers。='DKIM-Signature:v = 1; A = RSA-SHA1; C =轻松/简单; Q = DNS / TXT; bh ='; // ... 

$ body ='测试PHP + SMTP认证邮件,3。';

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

我不确定我错过了什么?

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

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



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

 <?php 
//发送简单电子邮件,无DKIM显示
//你的电子邮件
$ name ='Name Jony';
$ sender ='sdsdso@domain.pl';
$ to ='ffffu@gmail.com';
$ subject ='简单的html消息与dkim';

$ headers =
'MIME-Version:1.0
From:'。$ name。'<'。$ sender。'>
Content-type:text / html;字符集= UTF8’ ;

$ message =
'< html>
< header>< / header>
< body>
您好,这是一个DKIM测试电子邮件
< / body>
< / html>';

//现在你会做(设置配置文件和DNS记录之后):
//确保换行符是CRLF格式 - 签署
$ message = preg_replace('/(?<!\r)\ n /',\r\\\
,$ message);
$ headers = preg_replace('/(?<!\r)\\\
/',\r\\\
,$ 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 = $签名 - > get_signed_headers($ to,$ subject,$ message,$ headers);

//发送电子邮件
ini_set('sendmail_from',$ sender);
回复邮件($ to,$ subject,$ message,$ signed_headers。$ headers);
die();


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?

解决方案

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

这篇关于如何正确使用PHP为dkim散列正文和标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆