這是一個已經在我的雷達一段時間,並無法找到一個明確的答案在這個線程在這個線程的任何地方的原始問題。我現在已經能夠實現用PHP/Pear發送DKIM簽名的SMTP電子郵件。以下是所需的步驟。
我找到了DKIM的修改版本從http://www.ra726.net/blog/2010/07/20/sending-email-to-gmail-from-php-without-being-marked-as-spam/(您可以通過http://www.ra726.net/php-dkim.zip下載)。如果你已經實現了DKIM並且只需要使它與SMP郵件一起工作,那麼你所需要的就是dkim.php文件,正如博客所說的那樣,該文件稍作修改以處理作爲數組傳遞的頭文件。在我的代碼中,我將其命名爲dkimNEW.php。
確保包含大多數標頭,以便MTA在簽名後不會修改消息。在我有限的研究中,最多添加的頭文件是Date和Message-ID頭文件,因此我的頭文件數組看起來像這樣:注意:我使用它來發送html電子郵件,並進行更改以適合!此外,添加您的域名作爲郵件ID的最後一部分
$headers = array(
'Subject' => $subject,
'From' => $from,
'To' => $to,
'MIME-Version' => '1.0',
'Date' => date('r'),
'Message-ID' => '<'.sha1(microtime(true)).'@yourdomain.com>',
'Content-Type' => 'text/html',
'Content-Transfer-Encoding' => 'quoted-printable',
); // end $headers
然後你會得到利用上述修改dkim.php簽署您的電子郵件和簽名添加到陣列頭,又名
require 'dkimNEW.php';
$dkim = AddDKIM($headers, $subject, $body);
$headers['DKIM-Signature'] = $dkim;
代碼的其餘部分是正常碼通過與PHP/SMTP梨發送電子郵件。完整的工作代碼是:
<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
// set all of the parameters
$subject = 'Test of DKIM';
$from = 'My Name <[email protected]>';
$to = 'First Recipient <[email protected]>';
$pbody ='<html><head></head><body><h1>Done! DKIM test</h1>Result, next?</body></html>';
$text = strip_tags($pbody);
// create the headers
$headers = array(
'Subject' => $subject,
'From' => $from,
'To' => $to,
'MIME-Version' => '1.0',
'Date' => date('r'),
'Message-ID' => '<'.sha1(microtime(true)).'@mydomain.com>',
'Content-Type' => 'text/html',
'Content-Transfer-Encoding' => 'quoted-printable',
); // end $headers
// create the message
$mime = new Mail_mime("\n");
$mime->setTXTBody($text);
$mime->setHTMLBody($pbody);
// always call these methods in this order
$body = $mime->get();
$headers = $mime->headers($headers);
require 'dkimNEW.php' ;
$dkim = AddDKIM($headers, $subject, $body);
$headers['DKIM-Signature'] = $dkim;
// create the smtp mail object
$smtp_params = array(
'host' => 'mail.mydomain.com',
'auth' => true,
'username' => 'myUserName',
'password' => 'myPassWord',
); // end $smtp_params
$smtp = Mail::factory('smtp', $smtp_params);
// send the message
$recipients = array('[email protected]', '[email protected]');
$mail = $smtp->send($recipients, $headers, $body);
?>
PS。以防萬一您沒有注意到,請用您自己的值替換!
因此,使DKIM與SMTP電子郵件(甚至PHP郵件)一起工作所需的一切就是確保您指定由MTA添加到您的電子郵件中的所有標題,然後簽署標題,郵件的主題和正文,並最終將您的標題包含在簽名部分中。
下投票 \t 下面是例子功能是如何發送電子郵件與DKIM和PHP mail()函數: https://github.com/breakermind/PHP-DKIM – 2017-09-26 12:07:48