Send Encrypted Emails using Python and SMTP

Send Enrypted Emails Using Python and SMTP

How to send encrypted emails using Python, SMTP and a Gmail account?

Privacy is frequently lost as online data harvesting and security concerns grow. Because online privacy is so lacking, I began to consider a solution to encrypt messages. Because Python is such a versatile language, I wanted to see what I could achieve with encryption. I first devised a technique to encrypt and decrypt content before moving on to sending encrypted emails.

First and foremost, I did not wish to install any extra modules. Second, while this technique is relatively safe, like with most encryption, it can be decoded if someone really wants to.

Encrypting and Decrypting Emails Using Python and SMTP

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.

<code>result =""</code> <code>choice =input("\nEnter 1 to Encrypt or 2 to Decrypt:\n>>")</code>
Code language: HTML, XML (xml)

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)
Code language: PHP (php)

If the user decides to decrypt, which will be necessary when receiving encrypted emails, lines twelve through seventeen add four to the total.

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)
Code language: PHP (php)

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

Sending Encrypted Emails

There are numerous examples of sending emails using Python. This example uses Gmail, and it’s worth noting that Google’s security and authentication policies may change at any time. You’ll need to allow less secure apps in order for this to work. Trinket doesn’t support the smtplib module, therefore I couldn’t use it for this portion.

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()
Code language: PHP (php)

Dissecting the Code

There is no need to install an extra module because Python has a native library and smtplib is included. Lines one through eleven are primarily used for Gmail account authentication. On line five, you’ll specify Gmail’s SMTP server and port (587) for accessing Gmail. Your Google login information is required on line eleven.

#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")
Code language: PHP (php)

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)
Code language: PHP (php)

The final line of code specifies your email account as well as the recipient of your encrypted message, while the last line ends the session.

#Send the mail s.sendmail("your@gmail.com", "where-to-send@gmail.com", message) #Terminate s.quit()
Code language: PHP (php)

Conclusion

Python encryption and decryption can be quite handy for a variety of purposes. Python may also be used to send emails, which can be quite handy. This method can be used as a fun way to connect with others or if you’re just anxious about security. Because Gmail doesn’t allow less secure apps by default, it’s pointless to send encrypted emails to random accounts.

%d bloggers like this: