[英]Sending Encrypted Email S/MIME with PHP
I have been looking a lot online but I didn't find an answer, is it possible to send encrypted emails S/MIME using PHP? 我一直在网上找很多东西,但是找不到答案,是否可以使用PHP发送加密的电子邮件S / MIME? if it is, how?
如果是,怎么办? (im using cakephp 2.x)
(我使用cakephp 2.x)
Thank you very much in advance 提前非常感谢你
I managed to find a solution to this using PHPMailer, It applies to regular PHP as well. 我设法找到使用PHPMailer的解决方案,它也适用于常规PHP。 It will sign and encrypt the email, I couldn't find a way to do both with PHPMailer (sign and encrypt) only sign so I added some code to class.phpmailer.php.
它将对电子邮件进行签名和加密,我找不到仅使用PHPMailer进行签名和签名的方法,因此我在class.phpmailer.php中添加了一些代码。 It stills need to add some error handling in case of an encryption error but so far works good.
它仍然需要在加密错误的情况下添加一些错误处理,但是到目前为止效果很好。
for CakePHP 2.x: 对于CakePHP 2.x:
Download PHPMailer and add it to your Vendors folder (project_name/app/vendor) 下载PHPMailer并将其添加到您的Vendors文件夹(project_name / app / vendor)
Add this line at the beginning of your function: 在函数的开头添加以下行:
App::import('Vendor','PHPMailer/PHPMailerAutoload');
From here its the same for PHP or CakePHP: 从这里开始,PHP或CakePHP都一样:
$mail = new PHPMailer();
$mail->setFrom('from_who@email', 'Intranet');
//Set who the message is to be sent to
$mail->addAddress('to_who@email', '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!";
}
Then we need to make some changes to class.phpmailer.php 然后我们需要对class.phpmailer.php进行一些更改
replace the lines from 2368 to 2390 with: 将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());
}
}
}
then look for: 然后寻找:
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;
}
and change it for: 并将其更改为:
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;
}
look for: 寻找:
protected $sign_extracerts_file = '';
and add these lines after it: 并在其后添加以下行:
protected $encrypt_cert = '';
protected $and_encrypt = '';
With these changes to phpmailer you can send a signed email or a signed and encrypted email. 通过对phpmailer的这些更改,您可以发送签名的电子邮件或签名和加密的电子邮件。 It works with attachments too.
它也适用于附件。
I hope it is help ful to somebody. 我希望这对某人有帮助。
*for regular php just don't add the line: *对于常规php,请不要添加以下行:
App::import('Vendor','PHPMailer/PHPMailerAutoload');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.