Home 0P3N Blog Using Python to Send Emails: Terminal Sendmail Command
Ready to Start Your Career?
Create Free Account
cavete s profile image
By: cavete
December 7, 2016

Using Python to Send Emails: Terminal Sendmail Command

By: cavete
December 7, 2016
cavete s profile image
By: cavete
December 7, 2016
python-scriptThis is a Python script I wrote to send emails to email accounts or cellular devices. It can be used to send a single message or multiple copies of the message to the recipient. It is a very crude and my first python script I work. The script uses the send email terminal command and prompts for the address of the receiver and sender. In order to send the email, you have to enter your username and password along with the mail server address.This being my first python script it took several tries to get it to flow and work the way I wanted and for it to fit the format of the send email command. The script and be modified to be used how you like and if you have and suggestions for the script fell free to suggest them. I wrote this script to learn more about python functions and how they interact with the terminal to complete tasks. This script also taught me how to give characters color and be bold while running in Terminal. All in all, this turned out to be a great learning experience since I learn better by doing rather than reading how to do it. I find it more exciting to just dive into a project and work out the mistakes along the way.The first part of the script explains some of the common Carriers in the USA to send emails as text to Cellular devices. Then the next part has all the questions to enter the required information to send a basic email with no attachment, header or subject. The last question to be asked is how many messages are to be sent? They will retain copies, so keep that in mind. If you entered the information correctly to access your email service provider's server, receiver, and your email accounts, the message will be sent. When entering the number of messages, I only tested up to 100 and they were sent to my Cellular device. I did this as a similar DOS attack for the specific phone number. I was also only able to test the script with Gmail since I only have yahoo and Gmail accounts, and yahoo would not authenticate at all even when just using the send email command and not the script. However, Gmail works.Since this was my first script, it took me several tries to get the input for a number of messages to send, then have the messages sent that many times. At first, I was trying to give the command to send the message its own name; however, it did not work when trying to send the message multiple times. So, I ended up putting the command with all the arguments from the questions asked into the "for I in range(x):" loop itself. That solved the problem of getting the message to send more than just once, all without having to re-enter the email address and the password as well.Here is the script I came up with:
#! /usr/bin/pythonimport osimport sysimport subprocessclass color:PURPLE='33[95m'CYAN='33[96m'DARKCYAN='33[36m'BLUE='33[94m'GREEN='33[92m'YELLOW='33[93m'RED='33[91m'BOLD='33[1m'UNDERLINE='33[4m'END='33[0m'print(color.CYAN + "To Send Emails to Email Accounts Or Cellular Devices")print("This Python Program Uses The sendemail Feature in Terminal" + color.END)print(color.BOLD + color.RED + "Written by Cavete <cavete87@gmail.com>nn" + color.END)print(color.BLUE +"Phone Carrier Emails For Mobile Numbers")print("AT&T [10 digit number]@txt.att.net")print("Boost [10 digit number]@myboostmobile.com")print("T-Mobile [10 digit number]@tmomail.net")print("US Cellular [10 digit number]@email.uscc.net")print("Verizon [10 digit number]@vtext.com")print("Virgin Mobile [10 digit number]@vmobl.com")print("Other Carriers From Around the World Can Be Found")print("At www.emailtextmessages.com And")print("At mfitsp.io/list-of-email-to-sms-gateways/")print("Remember SMS Messages Are No More Than 160 Characters Long")print("If Your Message Is Longer, It Will Be Converted To MMS Message")print("Author Takes No Responsibility For How Users Use The Python Script!")print("Use Responsibily Or Accept The Consequences Of Your Actions!n" + color.END)#Remove Comment If You Want To See The sendemail Functions#sndEmail=os.system("sendemail")toAddr=raw_input("Enter Email Address of Receiver: ")fromAddr=raw_input("Enter Your Email Address: ")userName=raw_input("Enter Username For Your Email Account: ")passWord=raw_input("Enter Password For Your Email Account: ")msg=raw_input("Enter Message: ")serverPort=raw_input("Enter Server And Port Number: EX: smtp.gmail.com:587 ")x=(input("Enter The Number of Message Copies To Be Sent: "))for i in range(x):print(subprocess.call(["sendemail", "-t", toAddr, "-f", fromAddr, "-s", serverPort, "-xu", userName, "-xp", passWord, "-m", msg]))`
Again feel free to comment and let me know if there are easier, more efficient, ways of doing the script, or what your opinions of the it are. I did have a great time learning to write and use this script.
Schedule Demo

Build your Cybersecurity or IT Career

Accelerate in your role, earn new certifications, and develop cutting-edge skills using the fastest growing catalog in the industry