title: Send S/MIME encrypted mails using exchangelib in Python
body: <p>I'm trying to send manually encrypted S/MIME mails via the <a href="https://github.com/ecederstrand/exchangelib" rel="nofollow noreferrer">exchangelib</a> in Python. I can send simple emails and emails with attachments.</p>
  
  <p>I can also manually S/MIME encrypt data with OpenSSL. I can't seem to find an option for exchangelib to encrypt with S/MIME, so that has to be done manually.</p>
  
  <p>From what I've understood, the easiest way would be to add the appropriate mail headers and encrypt the message body manually for the recipient.</p>
  
  <p>So I believe the headers I have to add are:</p>
  
  <pre><code>MIME-Version: 1.0
  Content-Type: application/pkcs7-mime; name="smime.p7m"; smime-type=enveloped-data
  Content-Transfer-Encoding: base64
  Content-Disposition: attachment; filename="smime.p7m"
  Content-Description: S/MIME Encrypted Message
  </code></pre>
  
  <p>The actual mail body would the just be the S/MIME encrypted (base64 encoded) message including optional attachments (e.g. images).</p>
  
  <p>How can I add these <em>custom</em> mail headers with exchangelib in Python?</p>
  
  <p>I've also contacted the developers. More information on the <a href="https://github.com/ecederstrand/exchangelib/issues/331" rel="nofollow noreferrer">projects Github</a></p>