2017-03-16 3 views
0

Je cherchais beaucoup en ligne mais je n'ai pas trouvé de réponse, est-il possible d'envoyer des emails cryptés S/MIME en utilisant PHP? si c'est le cas, comment? (Im en utilisant 2.x CakePHP)Envoi d'e-mails cryptés S/MIME avec PHP

Merci beaucoup à l'avance

+1

Oui il est possible –

+1

vous pouvez vérifier ces plug-ins pour CakePHP 2 https://github.com/LubosRemplik/CakePHP-Encrypt-Plugin – tarikul05

+0

Merci mais cela ne fonctionne qu'avec pgp. – rimba

Répondre

0

j'ai réussi à trouver une solution à cela en utilisant PHPMailer, il applique à PHP régulière ainsi. Il va signer et crypter l'email, je ne pouvais pas trouver un moyen de faire les deux avec PHPMailer (signer et crypter) que signer, donc j'ai ajouté du code à class.phpmailer.php. Il a encore besoin d'ajouter une gestion des erreurs en cas d'erreur de chiffrement, mais jusqu'à présent fonctionne bien.

pour CakePHP 2.x:

Télécharger PHPMailer et l'ajouter à votre dossier fournisseurs (project_name/app/fournisseur)

Ajoutez cette ligne au début de votre fonction:

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

de là est la même chose pour PHP ou 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!"; 
} 

Ensuite, nous avons besoin de m Ake quelques modifications à class.phpmailer.php

remplacent les lignes de 2368-2390 avec:

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

        } 

      } 

puis recherchez:

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; 
} 

et changer pour:

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; 
    } 

cherchez:

protected $sign_extracerts_file = ''; 

et ajouter ces lignes après: il

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

Avec ces changements phpmailer vous pouvez envoyer un e-mail signé ou un e-mail signé et crypté. Cela fonctionne aussi avec les pièces jointes.

J'espère que c'est utile pour quelqu'un.

* pour php régulière juste ne pas ajouter la ligne:

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