2017-03-16 145 views
0

我一直在尋找很多在線,但我沒有找到答案,是否可以使用PHP發送加密電子郵件S/MIME?如果是,如何? (即時通訊使用CakePHP的2.x的)用PHP發送加密電子郵件S/MIME

非常感謝你提前

+1

是其可能 –

+1

您可以檢查此插件的CakePHP 2 https://github.com/LubosRemplik/CakePHP-Encrypt-Plugin – tarikul05

+0

謝謝,但這隻適用於pgp。 – rimba

回答

0

我設法找到一個解決這個使用PHPMailer的,它適用於常規PHP爲好。它會對郵件進行簽名和加密,我無法找到一種方式來處理PHPMailer(簽名和加密)簽名,所以我在class.phpmailer.php中添加了一些代碼。它仍然需要添加一些錯誤處理來防止加密錯誤,但目前爲止效果不錯。

爲CakePHP的2.X:

App::import('Vendor','PHPMailer/PHPMailerAutoload'); 

下載PHPMailer的,並把它添加到您的供應商目錄(PROJECT_NAME /應用/供應商)

在你的函數的開頭添加此行

從這裏它的PHP或CakePHP相同:

$mail = new PHPMailer(); 
$mail->setFrom('[email protected]', 'Intranet'); 
//Set who the message is to be sent to 
$mail->addAddress('[email protected]', 'Ricardo V'); 
//Set the subject line 
$mail->Subject = 'PHPMailer signing test'; 
//Replace the plain text body with one created manually 
$mail->Body = "some encrypted text..."; 
//Attach an image file 
$mail->addAttachment('D:/path_to_file/test.pdf'); 
$mail->sign( 
    'app/webroot/cert/cert.crt', //The location of your certificate file 
    'app/webroot/cert/private.key', //The location of your private key 
file 
    'password', //The password you protected your private key with (not 
    //the Import Password! may be empty but parameter must not be omitted!) 
    'app/webroot/cert/certchain.pem', //the certificate chain. 
    '1', //Encrypt the email as well, (1 = encrypt, 0 = dont encrypt) 
    'app/webroot/cert/rvarilias.crt'//The location of public certificate 
    //to encrypt the email with. 
); 
if (!$mail->send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 

然後,我們需要m AKE一些變化class.phpmailer.php

取代從2368行到2390搭配:

$sign = @openssl_pkcs7_sign(
        $file, 
        $signed, 
        'file://' . realpath($this->sign_cert_file), 
        array('file://' . realpath($this->sign_key_file), 
$this->sign_key_pass), 
        null, 
        PKCS7_DETACHED, 
        $this->sign_extracerts_file 
       ); 
       if ($this->encrypt_file == 1) { 
        $encrypted = tempnam(sys_get_temp_dir(), 'encrypted'); 
        $encrypt = @openssl_pkcs7_encrypt(
        $signed, 
        $encrypted, 
        file_get_contents($this->encrypt_cert_file), 
        null, 
        0, 
        1 
       ); 
       if ($encrypted) { 
        @unlink($file); 
        $body = file_get_contents($encrypted); 
        @unlink($signed); 
        @unlink($encrypted); 
        //The message returned by openssl contains both headers 
and body, so need to split them up 
        $parts = explode("\n\n", $body, 2); 
        $this->MIMEHeader .= $parts[0] . $this->LE . $this->LE; 
        $body = $parts[1]; 
       } else { 
        @unlink($file); 
        @unlink($signed); 
        @unlink($encrypted); 
        throw new phpmailerException($this->lang('signing') . 
openssl_error_string()); 
       } 
       } else { 

       if ($signed) { 
        @unlink($file); 
        $body = file_get_contents($signed); 
        @unlink($signed); 
        //The message returned by openssl contains both headers 
and body, so need to split them up 
        $parts = explode("\n\n", $body, 2); 
        $this->MIMEHeader .= $parts[0] . $this->LE . $this->LE; 
        $body = $parts[1]; 
      } else { 
        @unlink($file); 
        @unlink($signed); 
        throw new phpmailerException($this->lang('signing') . 
openssl_error_string()); 
      } 

        } 

      } 

然後尋找:

public function sign($cert_filename, $key_filename, $key_pass, 
$extracerts_filename = '') 
{ 
    $this->sign_cert_file = $cert_filename; 
    $this->sign_key_file = $key_filename; 
    $this->sign_key_pass = $key_pass; 
    $this->sign_extracerts_file = $extracerts_filename; 
} 

並改變它:

public function sign($cert_filename, $key_filename, $key_pass, 
$extracerts_filename = '', $and_encrypt ='0', $encrypt_cert = '') 
    { 
     $this->sign_cert_file = $cert_filename; 
     $this->sign_key_file = $key_filename; 
     $this->sign_key_pass = $key_pass; 
     $this->sign_extracerts_file = $extracerts_filename; 
     $this->encrypt_file = $and_encrypt; 
     $this->encrypt_cert_file = $encrypt_cert; 
    } 

尋找:

protected $sign_extracerts_file = ''; 

,並添加這些行後:

protected $encrypt_cert = ''; 
protected $and_encrypt = ''; 

隨着這些變化的PHPMailer你可以發送簽名郵件或簽名和加密的電子郵件。它也適用於附件。

我希望對某人有幫助。

*定期PHP只是不添加一行:

App::import('Vendor','PHPMailer/PHPMailerAutoload');