+ /**
+ * Set the private key file and password to sign the message.
+ *
+ * @access public
+ * @param string $key_filename Parameter File Name
+ * @param string $key_pass Password for private key
+ */
+ public function DKIM_QP($txt) {
+ $tmp = '';
+ $line = '';
+ for ($i = 0; $i < strlen($txt); $i++) {
+ $ord = ord($txt[$i]);
+ if ( ((0x21 <= $ord) && ($ord <= 0x3A)) || $ord == 0x3C || ((0x3E <= $ord) && ($ord <= 0x7E)) ) {
+ $line .= $txt[$i];
+ } else {
+ $line .= "=".sprintf("%02X", $ord);
+ }
+ }
+ return $line;
+ }
+
+ /**
+ * Generate DKIM signature
+ *
+ * @access public
+ * @param string $s Header
+ */
+ public function DKIM_Sign($s) {
+ $privKeyStr = file_get_contents($this->DKIM_private);
+ if ($this->DKIM_passphrase != '') {
+ $privKey = openssl_pkey_get_private($privKeyStr, $this->DKIM_passphrase);
+ } else {
+ $privKey = $privKeyStr;
+ }
+ if (openssl_sign($s, $signature, $privKey)) {
+ return base64_encode($signature);
+ }
+ }
+
+ /**
+ * Generate DKIM Canonicalization Header
+ *
+ * @access public
+ * @param string $s Header
+ */
+ public function DKIM_HeaderC($s) {
+ $s = preg_replace("/\r\n\s+/", " ", $s);
+ $lines = explode("\r\n", $s);
+ foreach ($lines as $key => $line) {
+ list($heading, $value) = explode(":", $line, 2);
+ $heading = strtolower($heading);
+ $value = preg_replace("/\s+/", " ", $value) ; // Compress useless spaces
+ $lines[$key] = $heading.":".trim($value) ; // Don't forget to remove WSP around the value
+ }
+ $s = implode("\r\n", $lines);
+ return $s;
+ }
+
+ /**
+ * Generate DKIM Canonicalization Body
+ *
+ * @access public
+ * @param string $body Message Body
+ */
+ public function DKIM_BodyC($body) {
+ if ($body == '') return "\r\n";
+ // stabilize line endings
+ $body = str_replace("\r\n", "\n", $body);
+ $body = str_replace("\n", "\r\n", $body);
+ // END stabilize line endings
+ while (substr($body, strlen($body) - 4, 4) == "\r\n\r\n") {
+ $body = substr($body, 0, strlen($body) - 2);
+ }
+ return $body;
+ }
+
+ /**
+ * Create the DKIM header, body, as new header
+ *
+ * @access public
+ * @param string $headers_line Header lines
+ * @param string $subject Subject
+ * @param string $body Body
+ */
+ public function DKIM_Add($headers_line, $subject, $body) {
+ $DKIMsignatureType = 'rsa-sha1'; // Signature & hash algorithms
+ $DKIMcanonicalization = 'relaxed/simple'; // Canonicalization of header/body
+ $DKIMquery = 'dns/txt'; // Query method
+ $DKIMtime = time() ; // Signature Timestamp = seconds since 00:00:00 - Jan 1, 1970 (UTC time zone)
+ $subject_header = "Subject: $subject";
+ $headers = explode($this->LE, $headers_line);
+ foreach($headers as $header) {
+ if (strpos($header, 'From:') === 0) {
+ $from_header = $header;
+ } elseif (strpos($header, 'To:') === 0) {
+ $to_header = $header;
+ }
+ }
+ $from = str_replace('|', '=7C', $this->DKIM_QP($from_header));
+ $to = str_replace('|', '=7C', $this->DKIM_QP($to_header));
+ $subject = str_replace('|', '=7C', $this->DKIM_QP($subject_header)) ; // Copied header fields (dkim-quoted-printable
+ $body = $this->DKIM_BodyC($body);
+ $DKIMlen = strlen($body) ; // Length of body
+ $DKIMb64 = base64_encode(pack("H*", sha1($body))) ; // Base64 of packed binary SHA-1 hash of body
+ $ident = ($this->DKIM_identity == '')? '' : " i=" . $this->DKIM_identity . ";";
+ $dkimhdrs = "DKIM-Signature: v=1; a=" . $DKIMsignatureType . "; q=" . $DKIMquery . "; l=" . $DKIMlen . "; s=" . $this->DKIM_selector . ";\r\n".
+ "\tt=" . $DKIMtime . "; c=" . $DKIMcanonicalization . ";\r\n".
+ "\th=From:To:Subject;\r\n".
+ "\td=" . $this->DKIM_domain . ";" . $ident . "\r\n".
+ "\tz=$from\r\n".
+ "\t|$to\r\n".
+ "\t|$subject;\r\n".
+ "\tbh=" . $DKIMb64 . ";\r\n".
+ "\tb=";
+ $toSign = $this->DKIM_HeaderC($from_header . "\r\n" . $to_header . "\r\n" . $subject_header . "\r\n" . $dkimhdrs);
+ $signed = $this->DKIM_Sign($toSign);
+ return "X-PHPMAILER-DKIM: phpmailer.worxware.com\r\n".$dkimhdrs.$signed."\r\n";
+ }
+
+ protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body) {
+ if (!empty($this->action_function) && function_exists($this->action_function)) {
+ $params = array($isSent, $to, $cc, $bcc, $subject, $body);
+ call_user_func_array($this->action_function, $params);
+ }
+ }