Suggestions on programming in Python an email simple client

Maroso Marco androidmaroso at gmail.com
Thu Feb 15 17:30:24 EST 2018


Il giorno martedì 13 febbraio 2018 21:06:19 UTC+1, Maroso Marco ha scritto:
> Hi,
> 
> what i'm trying to do is develop my own email client, but a simple one.
> 
> I just want it to connect to a specific email account and read the subject line of messages coming from a certain email address.
> 
> I then want it to be able to execute the command i wrote on the subject.
> 
> It would be nice if i could specify two parameters on the subject line
> 
> first : password;
> second : command;
> 
> The first parameter is the password and the second is the command to be executed in the local pc where this program is running.
> 
> The program will have to show only email coming from a specific account
> and if the mail is unread then it should read the subject line and find parameters.
> The first parameter must match my hardcoded password, and the second parameter must be executed on the local pc (example ... c:\windows\notepad.exe)
> 
> It must check for new emails at a given timing (example ...every 30 seconds )
>  (the timing can be hardcoded or specified in a separate file)
> 
> This is it.
> 
> Any suggestions?
> 
> I have started doing this (but i never programmed in python before):
> 
> 
> import imaplib
> import os
> import email
> import email.header
> 
> plusmail = "emailaddresstobechecked at gmail.com"
> googlepass = "mypassword"
> owner = "validsender at gmail.com"
> 
> M = imaplib.IMAP4_SSL('imap.gmail.com')
> M.login(plusmail, googlepass)
> M.select()
> 
> status, response = M.search(None, '(UNSEEN)')
> unread_msg_nums = response[0].split()
> 
> # Print the count of all unread messages
> print (len(unread_msg_nums))
> 
> # Print all unread messages from a certain sender of interest
> status, response = M.search(None, '(UNSEEN)', '(FROM "%s")' % (owner))
> unread_msg_nums = response[0].split()
> da = []
> for e_id in unread_msg_nums:
>     _, response = imap.fetch(e_id, '(BODY.PEEK[HEADER.FIELDS (From Subject)] RFC822.SIZE)')
>     da.append(response[0][1])
> # print (da)
> 
> # Mark them as seen
> for e_id in unread_msg_nums:
>     imap.store(e_id, '+FLAGS', '\Seen')
> 
> 
>     
> typ, data = M.search(None, 'From',(owner))
> for num in data[0].split():
>     typ, data = M.fetch(num, '(RFC822)')
>     print ('Message %s\n%s\n' % (num, data[0][1]))
> M.close()
> M.logout()
> 
> I know there are duplicates but i need to figure out how to build this correctly 
> 
> 
> Thanks in advance for any help given.

OK, i came up to this solution, but now i tried to compile and run as a service, but it doesnt seem to work, not fetching emails and reading comand.

# A SCRIPT FROM MININGFELLOWS By CrazyLion
import imaplib
import os
import email
import email.header
import time
import subprocess

def mailcontroller():
    
    # Set user, pass and allowed mail for giving commands
    plusmail = "mailmailaddress.com"
    googlepass = "thepassword"
    captain = "autorizedmail at gmail.com"
    
    # Set vars for IMAP access
    M = imaplib.IMAP4_SSL('imap.gmail.com')
    M.login(plusmail, googlepass)
    M.select()

    # Set search on UNSEEN messages
    status, response = M.search(None, '(UNSEEN)')
    unread_msg_nums = response[0].split()


    # Mark as read
    for e_id in unread_msg_nums:
        M.store(e_id, '+FLAGS', '\Seen')

    # cycle messages sent from autorized email address
    typ, data = M.search(None, 'From',(captain))

    for num in data[0].split():
        typ, data = M.fetch(num, '(RFC822)')
        msg = email.message_from_string(data[0][1])
        decode = email.header.decode_header(msg['Subject'])[0]
        subject = unicode(decode[0])
        comando = subject

        if googlepass in subject:
            
            # print 'Message %s: %s' % (num, subject)
            # Split subject line
            googlepass,comando = subject.split(";")
            # Execute command
            #os.system(comando)
            # Execute command with alternate method
            subprocess.call(comando)
            # Delete email
            M.store(num, '+FLAGS', '\\Deleted')

    M.close()
    M.logout()

    # Read ini file for timer settings
    timing = open('timing.ini', 'r').read()
    # Convert timer value from string to int
    time.sleep(int(timing))
    
while True:
    mailcontroller()




Any suggestions on how to compile it to run as a service.
I've tried to compile it in 32 and 64 bit version.
32 bit version runs ok, the 64 shuts after some seconds.

How to run comands like "net stop spooler" or "notepad" or "c:\file.bat" but as administrator (i mean with admin rights)

Tried to create a service based on my compiled version, it is in the processes
but it does not do anything.

Thanks really.







More information about the Python-list mailing list