signed-mail.phps 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * This example shows signing a message and then sending it via the mail() function of PHP.
  4. *
  5. * Before you can sign the mail certificates are needed.
  6. *
  7. *
  8. * STEP 1 - Creating a certificate:
  9. * You can either use a self signed certificate, pay for a signed one or use free alternatives such as StartSSL/Comodo etc.
  10. * Check out this link for more providers: http://kb.mozillazine.org/Getting_an_SMIME_certificate
  11. * In this example I am using Comodo.
  12. * The form is directly available via https://secure.comodo.com/products/frontpage?area=SecureEmailCertificate
  13. * Fill it out and you'll get an email with a link to download your certificate.
  14. * Usually the certificate will be directly installed into your browser (FireFox/Chrome).
  15. *
  16. *
  17. * STEP 2 - Exporting the certificate
  18. * This is specific to your browser, however, most browsers will give you the option to export your recently added certificate in PKCS12 (.pfx)
  19. * Include your private key if you are asked for it.
  20. * Set up a password to protect your exported file.
  21. *
  22. * STEP 3 - Splitting the .pfx into a private key and the certificate.
  23. * I use openssl for this. You only need two commands. In my case the certificate file is called 'exported-cert.pfx'
  24. * To create the private key do the following:
  25. *
  26. * openssl pkcs12 -in exported-cert.pfx -nocerts -out cert.key
  27. *
  28. * Of course the way you name your file (-out) is up to you.
  29. * You will be asked for a password for the Import password. This is the password you just set while exporting the certificate into the pfx file.
  30. * Afterwards, you can password protect your private key (recommended)
  31. * Also make sure to set the permissions to a minimum level and suitable for your application.
  32. * To create the certificate file use the following command:
  33. *
  34. * openssl pkcs12 -in exported-cert.pfx -clcerts -nokeys -out cert.crt
  35. *
  36. * Again, the way you name your certificate is up to you. You will be also asked for the Import Password.
  37. * To create the certificate-chain file use the following command:
  38. *
  39. * openssl pkcs12 -in exported-cert.pfx -cacerts -out certchain.pem
  40. *
  41. * Again, the way you name your chain file is up to you. You will be also asked for the Import Password.
  42. *
  43. *
  44. * STEP 3 - Code
  45. */
  46. //Import the PHPMailer class into the global namespace
  47. use PHPMailer\PHPMailer\PHPMailer;
  48. require '../vendor/autoload.php';
  49. //Create a new PHPMailer instance
  50. $mail = new PHPMailer;
  51. //Set who the message is to be sent from
  52. //IMPORTANT: This must match the email address of your certificate.
  53. //Although the certificate will be valid, an error will be thrown since it cannot be verified that the sender and the signer are the same person.
  54. $mail->setFrom('from@example.com', 'First Last');
  55. //Set an alternative reply-to address
  56. $mail->addReplyTo('replyto@example.com', 'First Last');
  57. //Set who the message is to be sent to
  58. $mail->addAddress('whoto@example.com', 'John Doe');
  59. //Set the subject line
  60. $mail->Subject = 'PHPMailer mail() test';
  61. //Read an HTML message body from an external file, convert referenced images to embedded,
  62. //Convert HTML into a basic plain-text alternative body
  63. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  64. //Replace the plain text body with one created manually
  65. $mail->AltBody = 'This is a plain-text message body';
  66. //Attach an image file
  67. $mail->addAttachment('images/phpmailer_mini.png');
  68. //Configure message signing (the actual signing does not occur until sending)
  69. $mail->sign(
  70. '/path/to/cert.crt', //The location of your certificate file
  71. '/path/to/cert.key', //The location of your private key file
  72. //The password you protected your private key with (not the Import Password!
  73. //May be empty but the parameter must not be omitted!
  74. 'yourSecretPrivateKeyPassword',
  75. '/path/to/certchain.pem' //The location of your chain file
  76. );
  77. //Send the message, check for errors
  78. if (!$mail->send()) {
  79. echo "Mailer Error: " . $mail->ErrorInfo;
  80. } else {
  81. echo "Message sent!";
  82. }
  83. /**
  84. * REMARKS:
  85. * If your email client does not support S/MIME it will most likely just show an attachment smime.p7s which is the signature contained in the email.
  86. * Other clients, such as Thunderbird support S/MIME natively and will validate the signature automatically and report the result in some way.
  87. */
  88. ?>