imshoaibf
Posts: 1
Joined: Sun May 14, 2017 8:42 pm

take input from mail and mark as read

Postby imshoaibf » Sun May 14, 2017 8:50 pm

#!/usr/bin/env python
import feedparser
import RPi.GPIO as GPIO
import time
from time import sleep
USERNAME = "*******@gmail.com"
PASSWORD = "*******"
response = feedparser.parse("https://" + USERNAME + ":" + PASSWORD + "@mail.google.com/gmail/feed/atom")
unread_count = int(response["feed"]["fullcount"])
x=response['items'][0].title
print x

if x=='1':
print " unlocked"
can someone help modify this code so as it takes input from only unread mails and mark that mail as read. or can someone give an idea on how to do that
User avatar
elParaguayo
Posts: 1813
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: take input from mail and mark as read

Postby elParaguayo » Mon May 15, 2017 11:41 am

This thread may be of some use to you (the first few posts are less relevant...).

In short, you can't change read status of emails with feedparser so you need to do it a different way.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.
Davies
Posts: 117
Joined: Sat Apr 04, 2015 4:24 pm

Re: take input from mail and mark as read

Postby Davies » Tue May 16, 2017 3:31 pm

i use pop3 which "marks" emails as read after a successful grab and popconn.quit.
apparently it doesnt make a mark that can be seen but is aware of emails already read.
you could also delete the messages which have been read..

Code: Select all

import poplib
from email import parser

frm = "your_email"
password = "your_password"

def mail_connection():
    pop_conn = poplib.POP3_SSL('pop.gmail.com')
    pop_conn.user(frm)
    pop_conn.pass_(password)
    print 'dialled in'
    return pop_conn


def fetch_mail(delete_after=False):
    print 'checking mail'
    pop_conn = mail_connection()
    messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
    messages = ["\n".join(mssg[1]) for mssg in messages]
    messages = [parser.Parser().parsestr(mssg) for mssg in messages]
    if delete_after == True:
        delete_messages = [pop_conn.dele(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
    pop_conn.quit()
    return messages


def get_messages():
    messages = fetch_mail()
    attachments = []
    for message in messages:
        if message['subject'] == 'email_your_looking_for':
            do_stuff
i used to have do_stuff before the pop_conn.quit but any error in my do_stuff caused the email to NOT be "marked as read" as the pop_conn.quit was never reached.. this way seems to work better... though im just about to post on here as im having some issues downloading from my emails.. works sometimes not others.