Exemple #1
0
 function CreateMessage()
 {
     $bConvert = false;
     if ($this->smimesign || $this->smimecrypt) {
         $msgdata = $this->_create_header(true);
         $msgdata .= $this->_create_body();
         $fileid = md5(uniqid(""));
         $newmsgfile = $this->temp_folder . "_attachments/" . $this->authuser . '_' . $fileid . "_newmsg.eml";
         $signfile = $this->temp_folder . "_attachments/" . $this->authuser . '_' . $fileid . "_signmsg.eml";
         $cryptfile = $this->temp_folder . "_attachments/" . $this->authuser . '_' . $fileid . "_cryptmsg.eml";
         if (save_file($newmsgfile, $msgdata)) {
             $headerInfo = $this->_get_header_info(true);
             if ($this->smimesign) {
                 $bRet = read_my_acitve_cert($cert, $key);
                 if ($bRet && openssl_pkcs7_sign($newmsgfile, $signfile, $cert, $key, $headerInfo)) {
                     $newmsgfile = $signfile;
                     $bConvert = true;
                 }
             }
             if ($this->smimecrypt) {
                 $arrAddress[] = $this->from;
                 for ($i = 0; $i < count($this->to); $i++) {
                     if (!in_array($this->to[$i][0], $arrAddress)) {
                         $arrAddress[] = $this->to[$i][0];
                     }
                 }
                 for ($i = 0; $i < count($this->cc); $i++) {
                     if (!in_array($this->to[$i][0], $arrAddress)) {
                         $arrAddress[] = $this->cc[$i][0];
                     }
                 }
                 for ($i = 0; $i < count($this->bcc); $i++) {
                     if (!in_array($this->to[$i][0], $arrAddress)) {
                         $arrAddress[] = $this->bcc[$i][0];
                     }
                 }
                 $bRet = read_all_receiver_cert($arrAddress, $arrCert);
                 if ($bRet && openssl_pkcs7_encrypt($newmsgfile, $cryptfile, $arrCert, $headerInfo)) {
                     $newmsgfile = $cryptfile;
                     $bConvert = true;
                 }
             }
             if ($bConvert) {
                 $msgdata = read_file($newmsgfile);
                 $msgdata = str_replace("\r\n", "\n", $msgdata);
                 $msgdata = str_replace("\n", "\r\n", $msgdata);
             }
         }
         @unlink($newmsgfile);
         @unlink($signfile);
         @unlink($cryptfile);
     }
     if (!$bConvert) {
         $msgdata = $this->_create_header();
         $msgdata .= $this->_create_body();
     }
     return $msgdata;
 }
Exemple #2
0
 /**
  * 创建邮件信息
  *
  * @return string 邮件内容
  */
 public function create_msg()
 {
     $msgdata = '';
     $bconvert = false;
     if ($this->smime_sign || $this->smime_crypt) {
         $msgdata = $this->_create_header(true);
         $msgdata .= $this->_create_body();
         $fileid = md5(uniqid());
         $new_msg_file = $this->temp_folder . "_attachments\\" . $this->auth_user . '_' . $fileid . "_newmsg.eml";
         $signfile = $this->temp_folder . "_attachments\\" . $this->auth_user . '_' . $fileid . "_signmsg.eml";
         $crypt_file = $this->temp_folder . "_attachments\\" . $this->auth_user . '_' . $fileid . "_cryptmsg.eml";
         if (file_put_contents($new_msg_file, $msgdata)) {
             $header_info = $this->_get_header_info(true);
             if ($this->smime_sign) {
                 $bret = read_my_acitve_cert($cert, $key);
                 if ($bret && openssl_pkcs7_sign($new_msg_file, $signfile, $cert, $key, $header_info)) {
                     $new_msg_file = $signfile;
                     $bconvert = true;
                 }
             }
             if ($this->smime_crypt) {
                 $arr_address[] = $this->from;
                 for ($i = 0; $i < count($this->to); $i++) {
                     if (!in_array($this->to[$i][0], $arr_address)) {
                         $arr_address[] = $this->to[$i][0];
                     }
                 }
                 for ($i = 0; $i < count($this->cc); $i++) {
                     if (!in_array($this->to[$i][0], $arr_address)) {
                         $arr_address[] = $this->cc[$i][0];
                     }
                 }
                 for ($i = 0; $i < count($this->bcc); $i++) {
                     if (!in_array($this->to[$i][0], $arr_address)) {
                         $arr_address[] = $this->bcc[$i][0];
                     }
                 }
                 $bret = read_all_receiver_cert($arr_address, $arr_cert);
                 if ($bret && openssl_pkcs7_encrypt($new_msg_file, $crypt_file, $arr_cert, $header_info)) {
                     $new_msg_file = $crypt_file;
                     $bconvert = true;
                 }
             }
             if ($bconvert) {
                 $msgdata = file_get_contents($new_msg_file);
                 $msgdata = str_replace("\r\n", "\n", $msgdata);
                 $msgdata = str_replace("\n", "\r\n", $msgdata);
             }
         }
         @unlink($new_msg_file);
         @unlink($signfile);
         @unlink($crypt_file);
     }
     if (!$bconvert) {
         $msgdata = $this->_create_header();
         $msgdata .= $this->_create_body();
     }
     return $msgdata;
 }