I'm looking for one, specific message, and then, after found, I want to delete it from inbox. Just this one.
My code:
import email
import imaplib
def check_email(self, user, password, imap, port, message):
M = imaplib.IMAP4_SSL(imap, port)
M.login(user, password)
M.select()
type, message_numbers = M.search(None, '(ALL)')
subjects = []
for num in message_numbers[0].split():
type, data = M.fetch(num, '(RFC822)')
msg = email.message_from_bytes(data[0][1])
subjects.append(msg['Subject'])
if message in subjects:
M.store(num, '+FLAGS', '\\Deleted')
else:
raise FileNotFoundError('Ooops!')
M.close()
M.logout()
I want to find and delete only one mail by title, gven in the variable (message).
Can you help me?
You loop over all the messages, then delete the last one (which is what num ends up pointing to after the loop finishes) if any one of the messages has a subject which matches. You probably want to reindent the code so that the check takes place inside the loop, and probably abandon the rest of the loop once you found the one you want.
def check_email(self, user, password, imap, port, message):
M = imaplib.IMAP4_SSL(imap, port)
M.login(user, password)
M.select()
type, message_numbers = M.search(None, '(ALL)')
found = False
for num in message_numbers[0].split():
type, data = M.fetch(num, '(RFC822)')
msg = email.message_from_bytes(data[0][1])
# No need to collect all the subjects in a list
# Just examine the current one, then forget this message if it doesn't match
if message in msg['Subject']:
M.store(num, '+FLAGS', '\\Deleted')
found = True
break
# Don't raise an exception before cleaning up
M.close()
M.logout()
# Now finally
if not Found:
raise FileNotFoundError('Ooops!')
Related
I have a problem sending proactive messages using the Bot Framework with Python.
First what I need is to get the message body from Outlook, and then the bot must send that as a message to all the chats where it was added.
To do that, first I created a new file and called it Email.py.
To read every incoming message body I simply used while true: and time.sleep()
Here is my code example:
import imaplib, email, getpass
from email import policy
import json
import time
imap_host = 'outlook.office365.com'
imap_user = 'xx#xx.com'
# init imap connection
mail = imaplib.IMAP4_SSL(imap_host, 993)
rc, resp = mail.login(imap_user, 'xxxxxx')
while True:
# select only unread messages from inbox
mail.select('Inbox')
status, data = mail.search(None, '(UNSEEN)')
if not data[0].split():
time.sleep(120)
# Bot message variable
Message_for_bot = ''
# for each e-mail messages
for num in data[0].split():
# get a single message and parse it by policy.SMTP (RFC compliant)
status, data = mail.fetch(num, '(RFC822)')
email_msg = data[0][1]
email_msg = email.message_from_bytes(email_msg, policy=policy.SMTP)
# print only message parts that contain text data
for part in email_msg.walk():
if part.get_content_type() == "text/plain":
for line in part.get_content().splitlines():
Message_for_bot += '\n' + line
print(Message_for_bot)
After I successfully created a program to read and print all incoming messages, I tried to build my bot. I found a proactive message bot on the Internet and used it as an example.
First I thought to just run this file with os in the background, but then my bot wasn't running. So then I tried adding an async function in the bot file but it didn't work. My bot just ignores that function. (Then I found the async functions in activity_handler.py, but I didn't find any that could help me.)
Then I tried adding an on_message_activity function and thought maybe it will start working if I call the bot like "#bot hi" for example in Teams. For that idea I must always run the while cycle and never stop the bot, but then I just get a message, and if there's a new incoming message then the bot doesn't write it anymore, and it's not a solution because if the bot is used for multiple chats then it simply doesn't work this way.
Then I try include my code on on_members_added_activity it seems working on azure test in web chat perfectly, but in teams after 1-2 messages stopping to work.
my code
async def on_members_added_activity(
self, members_added: [ChannelAccount], turn_context: TurnContext
):
imap_host = 'outlook.office365.com'
imap_user = 'xxxxxx#xxxxxx.com'
# init imap connection
mail = imaplib.IMAP4_SSL(imap_host, 993)
rc, resp = mail.login(imap_user, 'xxxxxx')
while True:
# select only unread messages from inbox
mail.select('Inbox')
status, data = mail.search(None, '(UNSEEN)')
if not data[0].split():
time.sleep(5)
# Bot message variable
Message_for_bot = ''
# for each e-mail messages
for num in data[0].split():
# get a single message and parse it by policy.SMTP (RFC compliant)
status, data = mail.fetch(num, '(RFC822)')
email_msg = data[0][1]
email_msg = email.message_from_bytes(email_msg, policy=policy.SMTP)
# print only message parts that contain text data
for part in email_msg.walk():
if part.get_content_type() == "text/plain":
for line in part.get_content().splitlines():
Message_for_bot += '\n' + line
await turn_context.send_activity(f"{Message_for_bot}")
for member in members_added:
if member.id != turn_context.activity.recipient.id:
await turn_context.send_activity(
"bot starting work..."
)
So maybe it's possible to send a message to wherever the bot is added (it needs to get this information somehow, maybe it's kept in the bot memory) whenever Message_for_bot is not empty.
All help will be appreciated.
As we have discussed some logic has to change
Move your code out of the on_members_added_activity function
Use Proactive concept to send the message
-Vinoth
I am trying to read otp from mail and after that I want to delete that email from gmail option. I have no problem in reading email but I am not able to delete mail. I tried some code from stackoverflow. below is my code.
def getOtpMail(vEmail, vPaasword):
connection = imaplib.IMAP4_SSL(IMAP_URL) # stablish connection with IMAP server
try:
connection.login(vEmail, vPaasword) # Login with userid password
except Exception as e:
print(e)
return
loopLock = True
while loopLock:
# fetch
connection.select('"INBOX"', readonly=True)
retCode, messages = connection.search(None, '(UNSEEN)')
print(messages[0])
latest = int(messages[0].split()[-1])
res, msg = connection.fetch(str(latest), "(RFC822)")
for response in msg:
if isinstance(response, tuple):
print('\n------------email--------------\n')
msg = email.message_from_bytes(response[1])
if SENDER_NAME in msg['From'] and KEYWORD in msg['Subject']:
loopLock = False
# fetch required information
for part in msg.walk():
body = part.get_payload()
word_list = body.split()
index = word_list.index('verification')
otp = word_list[index + 3].strip('.')
#delete mail - below two line not working
connection.store(str(latest), '+FLAGS', '"[Gmail]/Trash"')
print(connection.expunge())
return otp
else:
continue
I read documentation and print connection.expunge() so I got response as ('NO', [b'EXPUNGE attempt on READ-ONLY folder (Failure)']) . I think issue I have to establish connection in WRITE mode. I am not sure about it.
In this issue, I opened mail box in readonly mode. Hence my program not able to write and store in IMAP server.
I changed
connection.select('"INBOX"', readonly=True)
to
connection.select('"INBOX"', readonly=False)
also I changed command type and flag type in store method -
connection.store(str(latest), '+FLAGS', '"[Gmail]/Trash"')
to
connection.store(str(latest), '+FLAGS', '\\Deleted')
.
my code look like this...
import imaplib
import email
obj = imaplib.IMAP4_SSL('imap.gmail.com','993')
obj.login('user','pass')
obj.select('inbox')
delete = []
for i in range(1, 10):
typ, msg_data = obj.fetch(str(i), '(RFC822)')
print i
x = i
for response_part in msg_data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
for header in [ 'subject', 'to', 'from', 'Received' ]:
print '%-8s: %s' % (header.upper(), msg[header])
if header == 'from' and '<sender's email address>' in msg[header]:
delete.append(x)
string = str(delete[0])
for xx in delete:
if xx != delete[0]:
print xx
string = string + ', '+ str(xx)
print string
obj.select('inbox')
obj.uid('STORE', string , '+FLAGS', '(\Deleted)')
obj.expunge()
obj.close()
obj.logout()
the error I get is
Traceback (most recent call last):
File "del_email.py", line 31, in <module>
obj.uid('STORE', string , '+FLAGS', '(\Deleted)')
File "C:\Tools\Python(x86)\Python27\lib\imaplib.py", line 773, in uid
typ, dat = self._simple_command(name, command, *args)
File "C:\Tools\Python(x86)\Python27\lib\imaplib.py", line 1088, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Tools\Python(x86)\Python27\lib\imaplib.py", line 918, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: UID command error: BAD ['Could not parse command']
I am looking for a way to delete multiple emails at once using imaplib or other module. I am looking for the simplest example to go off of. This example was given at this link here Using python imaplib to "delete" an email from Gmail? the last answer's example. I'ts not working correctly. I can however get the the 1st example to work to delete one email every time the script is ran. I'd rather try the doing it with a multiple than running the script several thousand times. my main goal is to delete multiple emails through imaplib any workarounds or other working modules or examples would be appreciated.
You might find this a bit easier using IMAPClient as it takes care of a lot more of low level protocol aspects for you.
Using IMAPClient your code would look something like:
from imapclient import IMAPClient
import email
obj = IMAPClient('imap.gmail.com', ssl=True)
obj.login('user','pass')
obj.select('inbox')
delete = []
msg_ids = obj.search(('NOT', 'DELETED'))
for msg_id in msg_ids:
msg_data = obj.fetch(msg_id, ('RFC822',))
msg = email.message_from_string(msg_data[msg_id]['RFC822'])
for header in [ 'subject', 'to', 'from', 'Received' ]:
print '%-8s: %s' % (header.upper(), msg[header])
if header == 'from' and '<senders email address>' in msg[header]:
delete.append(x)
obj.delete_messages(delete)
obj.expunge()
obj.close()
obj.logout()
This could be made more efficient by fetching multiple messages in a single fetch() call rather than fetching them one at a time but I've left that out for clarity.
If you're just wanting to filter by the sender's address you can get the IMAP server to do the filtering for you. This avoids the need to download the message bodies and makes the process a whole lot faster.
This would look like:
from imapclient import IMAPClient
obj = IMAPClient('imap.gmail.com', ssl=True)
obj.login('user','pass')
obj.select('inbox')
msg_ids = obj.search(('NOT', 'DELETED', 'FROM', '<senders email address>'))
obj.delete_messages(msg_ids)
obj.expunge()
obj.close()
obj.logout()
Disclaimer: I'm the author and maintainer of IMAPClient.
Initial post :
SyntaxError: '<sender's email address>'
# did you mean :
"<sender's email address>"
So I have this class that starts like this:
class emailreader():
def __init__(self, server, port, username, password):
self.imap_server = imaplib.IMAP4_SSL(server, int(port))
self.imap_server.login(username, password)
self.imap_server.select('INBOX')
def _get_raw_emails(self):
messages = []
typ, data = self.imap_server.search(None, 'UnSeen')
for num in data[0].split():
typ, data = self.imap_server.fetch(num, '(RFC822)')
messages.append(data[0][1])
return messages
It's working great for fetching messages like this:
mail = emailreader(server, port, username, password)
emails = mail._get_raw_emails()
But if I send a new message to the e-mail address I can't just run the last line again, the new mail won't show up until I delete the mail object and start all over again. Why is that? Do I have to reset the last search or something?
Edit: I think I found the solution. I had to do a imap_server.check() also...
I found the solution. I had to execute a method that's called check() from my imap object whenever new mails may have arrived. This is my "new" _get_raw_emails() method:
def _get_raw_emails(self):
messages = []
self.imap_server.check() # Fetch new mails since object is created.
typ, data = self.imap_server.search(None, 'UnSeen')
for num in data[0].split():
typ, data = self.imap_server.fetch(num, '(RFC822)')
messages.append(data[0][1])
return messages
It may be because I'm not a native english speaker, but when I looked att the documentation for imaplib it did'nt seem obvious what the check method did. It only says "Checkpoint mailbox on server." So I did'nt thought that it would solve my problem.
I'd like to be able to retrieve a users Google Talk Status Message with Python, it's really hard to find documentation on how to use some of the libraries out there.
I don't have anything to hand with xmpp installed, but here's some old code I had lying around that might help you. You'll want to update the USERNAME/PASSWORD to your own values for test purposes.
Things to note: users logged in to Google Talk get a random presence string on their userid: that doesn't matter if you are trying to get the status of some other user, but if you want to write some code so want to communicate with yourself you need to distinguish the user logged in from GMail or a GTalk client from the test program. Hence the code searches through the userids.
Also, if you read the status immediately after logging in you probably won't get anything. There's a delay in the code because it takes a little while for the status to become available.
"""Send a single GTalk message to myself"""
import xmpp
import time
_SERVER = 'talk.google.com', 5223
USERNAME = 'someuser#gmail.com'
PASSWORD = 'whatever'
def sendMessage(tojid, text, username=USERNAME, password=PASSWORD):
jid = xmpp.protocol.JID(username)
client = xmpp.Client(jid.getDomain(), debug=[])
#self.client.RegisterHandler('message', self.message_cb)
if not client:
print 'Connection failed!'
return
con = client.connect(server=_SERVER)
print 'connected with', con
auth = client.auth(jid.getNode(), password, 'botty')
if not auth:
print 'Authentication failed!'
return
client.RegisterHandler('message', message_cb)
roster = client.getRoster()
client.sendInitPresence()
if '/' in tojid:
tail = tojid.split('/')[-1]
t = time.time() + 1
while time.time() < t:
client.Process(1)
time.sleep(0.1)
if [ res for res in roster.getResources(tojid) if res.startswith(tail) ]:
break
for res in roster.getResources(tojid):
if res.startswith(tail):
tojid = tojid.split('/', 1)[0] + '/' + res
print "sending to", tojid
id = client.send(xmpp.protocol.Message(tojid, text))
t = time.time() + 1
while time.time() < t:
client.Process(1)
time.sleep(0.1)
print "status", roster.getStatus(tojid)
print "show", roster.getShow(tojid)
print "resources", roster.getResources(tojid)
client.disconnect()
def message_cb(session, message):
print ">", message
sendMessage(USERNAME + '/Talk', "This is an automatically generated gtalk message: did you get it?")