Suggestions on programming in Python an email simple client

Chris Angelico rosuav at gmail.com
Tue Feb 13 15:18:05 EST 2018


On Wed, Feb 14, 2018 at 7:05 AM, Maroso Marco <androidmaroso at gmail.com> wrote:
> 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.

Be aware that "from" addresses can easily be forged. I suggest
whitelisting a set of valid commands and executing only those. If you
want a REAL way to manage your home system remotely, I would recommend
SSH instead.

> 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.

That's an improvement, but since the password is being transmitted in
clear text, it won't prevent attacks. It does mean you won't try to
execute the subject lines of random spam, though, so accidents will be
reduced (or eliminated).

> 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?

Seems perfectly possible.

> 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

Seems like a reasonable start. So what's the code doing wrong, or
failing to do? Is it failing with an exception? Is it running
successfully, but not doing something you want it to do?

ChrisA



More information about the Python-list mailing list