signed-mail.phps 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. require '../PHPMailerAutoload.php';
  47. //Create a new PHPMailer instance
  48. $mail = new PHPMailer();
  49. //Set who the message is to be sent from
  50. //IMPORTANT: This must match the email address of your certificate.
  51. //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.
  52. $mail->setFrom('from@example.com', 'First Last');
  53. //Set an alternative reply-to address
  54. $mail->addReplyTo('replyto@example.com', 'First Last');
  55. //Set who the message is to be sent to
  56. $mail->addAddress('whoto@example.com', 'John Doe');
  57. //Set the subject line
  58. $mail->Subject = 'PHPMailer mail() test';
  59. //Read an HTML message body from an external file, convert referenced images to embedded,
  60. //Convert HTML into a basic plain-text alternative body
  61. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  62. //Replace the plain text body with one created manually
  63. $mail->AltBody = 'This is a plain-text message body';
  64. //Attach an image file
  65. $mail->addAttachment('images/phpmailer_mini.png');
  66. //Configure message signing (the actual signing does not occur until sending)
  67. $mail->sign(
  68. '/path/to/cert.crt', //The location of your certificate file
  69. '/path/to/cert.key', //The location of your private key file
  70. 'yourSecretPrivateKeyPassword', //The password you protected your private key with (not the Import Password! may be empty but parameter must not be omitted!)
  71. '/path/to/certchain.pem' //The location of your chain file
  72. );
  73. //Send the message, check for errors
  74. if (!$mail->send()) {
  75. echo "Mailer Error: " . $mail->ErrorInfo;
  76. } else {
  77. echo "Message sent!";
  78. }
  79. /**
  80. * REMARKS:
  81. * 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.
  82. * Other clients, such as Thunderbird support S/MIME natively and will validate the signature automatically and report the result in some way.
  83. */
  84. ?>