.pem certificate Signed Email ?

Hi, i’m a great fan o this framework i use to be on spanish forums, and i have donde a couple of applications with it; but now i’ve encountered a “little big” problem.

I have an web app that sends email according to some factors, but i need the emails to be signed with .pem certificate and i’m going crazy searching for info about this but can’t find any.

I’m usign the standard Yii::$app->mailer->compose(), how can i set this to sign the emails?

Also saw this

$signer = \Swift_Signers_SMimeSigner::newInstance(‘cert/savocan-cert.pem’,‘savocan-key.pem’);

\Swift_SignedMessage::newInstance(‘ASUNTO’)->attachSigner($signer);

but can’t manage to make it work :(

Thanks everybody.

I know that this is not the right way to do this thing, but I do not know how to extend classes with private variables, so this work around. I did not try this, but think that this may work.

First, change the application component - mailer




  'components' => [

      ...

      'messageClass' => 'yii\swiftmailer\MyMessage',

      'mailer' => [

          'class' => 'yii\swiftmailer\Mailer',

          'transport' => [

              'class' => 'Swift_SmtpTransport',

              'host' => 'localhost',

              'username' => 'username',

              'password' => 'password',

              'port' => '587',

              'encryption' => 'tls',

          ],

      ],

      ...

  ],




Now, save the file ‘yii\swiftmailer\Message’ in a new name ‘yii\swiftmailer\MyMessage’. The only change in MyMessage compared to Message is that the following function is added.




	public function smimeSigner($pathToCertificate, $pathToPrivateKey) 

	{

		$smimeSigner = \Swift_Signers_SMimeSigner::newInstance();

		$smimeSigner->setSignCertificate($pathToCertificate, $pathToCertificate);

		$this->getSwiftMessage()->attachSigner($smimeSigner);


        return $this;

	}




Now you should be able to use the certificate and private key in the following way:




  Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])

      ->setFrom('from@domain.com')

      ->setTo($form->email)

      ->setSubject($form->subject)

      ->smimeSigner('path-to-certificate','path-to-private-key')

      ->send();



Thanks for the reply hrnair, seems closer to work, but i keep getting this "openssl_pkcs7_sign(): error getting private key"

i tried with absolute paths and relative, but keeps failing :P

Hi, this may be an issue with how swift mailer is accepting the private keys. There are some problem often in the way we save the keys created by putty. It is better to search for the solution in swift mailer forums.

You can go to the following file and see which function is creating the error based on the error description and work backwards.

vendor\swiftmailer\swiftmailer\lib\classes\Swift\Signers\SMimeSigner.php

Hi, i did find a workaround and finally get it to work by telling the path of the certificate in the SMimeSigner.php

Thanks a lot ;)