Securely Send Attachments via Python

Chad Sigler
Nov 8 · 2 min read

Encrypt attachments to a standard email using the Virtru SDK

Photo by pineapple L on Unsplash

Greetings! Recently someone had a question about sending standard emails but encrypting the attachments. Luckily I was able to offer a solution that required minimal modification of their existing Python mailer script by encrypting just the attachments using the Virtru SDK.

Goal

Send a normal email with an encrypted attachment that the recipient can open with minimal hassle and no additional requirements on their computer. The encrypted file will use the TDF as an HTML wrapper to ensure universal access to the encrypted file.

Requirements

Let's Dig Into the Code

Python Import

Import the required libraries to create an email and Virtru TDF.

Variable Definitions

Define all of the variables at the top of the python file for easy editing.

# Virtru Variables
virtru_appid = "appid"
virtru_owner = "sender@example.com"
# File Variables
file_name_tdf = "hello world.txt.tdf3.html"
file_path_plain="/tmp/helloworld.txt"
file_path_tdf="/tmp/helloworld.txt.tdf3.html"

Virtru Encryption

These will:

  • Create the Virtru SDK Client
  • Create the Virtru Encryption policy
  • Attach it to the Virtru SDK Client
  • Encrypt the requested File

SMTP

Using the smtplib library to create a multipart MIME message.

Steps:

  • Builds Message object (msg)
  • Sets Sender (From)
  • Sets Recipients (To, CC)
  • Sets Subject
  • Sets Body
  • Encodes the Body as base64
  • Attaches the Encrypted File (TDF)
  • Sets Header
  • Sets SmartHost
  • Enables TLS
  • Sends Message

Closing

Securing attachments to an already existing process can be a daunting task. Leveraging the Virtru SDK to encrypt just the attachments to existing workflows can be a very simple exercise, just a few lines of code. The encryption policy can later be edited to add users, remove users or revoke access to the file as a whole. Virtru also offers access to the Audit events to see if the file was ever accessed.

Virtru Technology Blog

Building developer tools in the interest of data protection and sharing.

Chad Sigler

Written by

Platform Team — Virtru

Virtru Technology Blog

Building developer tools in the interest of data protection and sharing.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade