Sending Encrypted Emails Using Python

Posted in Web Development on May 2018

As internet data mining and security concerns continue to burgeon, privacy often feels relinquished. Seeing how online privacy is relatively absent, I started contemplating a project to encrypt messages. Knowing Python is a multifaceted language, I wanted to see what could be done regarding encryption. Before going straight into sending encrypted emails, I created a mechanism to encrypt and decrypt content first.

Three things to note: for the first portion I used Trinket, as I did with a previous post about Creating a Text-Based Adventure and Quiz Game in Python, to allow easier coding and follow-along. Second, I didn’t want to install external modules. Lastly, it should be known that this technique is relatively secure, but, like most encryption, if someone really wants to decode, they can.

Encrypting and Decrypting

Complete code:

Understanding the Code

Starting at line one, declare variable result and assign it an empty string. For line three, create a choice variable and assign it an input. This particular choice is asking the user to enter either a numerical 1 or 2.

result = ""

choice = input("\nEnter 1 to Encrypt or 2 to Decrypt:\n>>")

Lines five through ten use an if statement to determine the user’s choice. Starting at line eight, the code becomes a bit trickier. range() reads a string, chr() converts an integer into a character, ord() converts a character into an integer, and - 4 subtracts four. Meanwhile, ASCII is utilized to formulate a numerical representation. For example, if the character is “A”, you’ll get “=” as the result. If you encrypt “Derek”, the output will display “@anag”. To make the concept easier to understand, if the code is changed to - 1 and inputting “B”, the output will be “A”.

if choice == "1":
    msg = input("\nEnter the message to encrypt:\n>>")

    for i in range(0, len(msg)):
         result = result + chr(ord(msg[i]) - 4)
    print(result)

If the user chooses to decrypt, which will come into play when receiving encrypted emails, lines twelve through seventeen does the complete opposite by adding four.

elif choice == "2":
    msg = input("\nEnter the message to decrypt:\n>>")

    for i in range(0, len(msg)):
        result = result + chr(ord(msg[i]) + 4)
    print(result)

Finally, lines nineteen and twenty utilize an else statement to prevent incorrect inputs.

Sending Encrypted Emails

There’s a plethora of examples regarding sending emails using Python. This example uses Gmail, and it should be noted that Google’s security and authentication might unexpectedly change. In order for this to work, you’ll need to allow less secure apps. For this section, I couldn’t use Trinket because it doesn’t allow the smtplib module.

Complete code:

#Your Gmail account
import smtplib

#SMTP session for Gmail
s = smtplib.SMTP('smtp.gmail.com', 587)

#Start TLS for security
s.starttls()

#Your Gmail authentication
s.login("your@gmail.com", "your-password")

#Message to be sent
message = ""

enc = input("\nEnter the message to encrypt:\n>>")

for i in range(0, len(enc)):
    message = message + chr(ord(enc[i]) - 4)
print(message)

#Send the mail
s.sendmail("your@gmail.com", "where-to-send@gmail.com", message)

#Terminate
s.quit()

Dissecting the Code

Python has a native library and luckily smtplib is included, so no need to install an external module. Lines one through eleven are mainly for authentication to your Gmail account. You’ll be specifying Gmail’s SMTP server and port (587) for accessing, which will be on line five. On line eleven, your Google login information is required.

#Your Gmail account
import smtplib

#SMTP session for Gmail
s = smtplib.SMTP('smtp.gmail.com', 587)

#Start TLS for security
s.starttls()

#Your Gmail authentication
s.login("your@gmail.com", "your-password")

The message variable will output content, which, in this case, the encrypted text will be displayed to the end-user. The code is roughly the same as the first section, but with minor changes. For simplicity, the if and else statements have been removed. Decrypting is also removed and msg is now enc.

#Message to be sent
message = ""

enc = input("\nEnter the message to encrypt:\n>>")

for i in range(0, len(enc)):
    message = message + chr(ord(enc[i]) - 4)
print(message)

The last bit of code is specifying your email account and receiver of your encrypted message, while the final line terminates the session.

#Send the mail
s.sendmail("your@gmail.com", "where-to-send@gmail.com", message)

#Terminate
s.quit()

Conclusion

Encrypting and decrypting using Python can actually be very useful for an assortment of projects. Sending emails using Python can be rather useful as well. Using this technique can be a fun way to communicate with people or if you’re simply paranoid about security. By default Gmail doesn’t allow less secure apps, so sending encrypted emails to random accounts is fruitless.

Back To Top