Reading unread emails using python script - python

I am trying to read all the unread emails from the gmail account.
The above code is able to make connection but is unable to fetch the emails.
I want to print the content of each email.
I am getting the error as can't concat int to bytes.
code:
import smtplib
import time
import imaplib
import email
def read_email_from_gmail():
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('my_mail','my_pwd')
mail.select('inbox')
result, data = mail.search(None, 'ALL')
mail_ids = data[0]
id_list = mail_ids.split()
first_email_id = int(id_list[0])
latest_email_id = int(id_list[-1])
for i in range(latest_email_id,first_email_id, -1):
result, data = mail.fetch(i, '(RFC822)' )
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
email_subject = msg['subject']
email_from = msg['from']
print ('From : ' + email_from + '\n')
print ('Subject : ' + email_subject + '\n')
print(read_email_from_gmail())
error:
Traceback (most recent call last):
File "C:/Users/devda/Desktop/Internship/access_email.py", line 32, in <module>
print(read_email_from_gmail())
File "C:/Users/devda/Desktop/Internship/access_email.py", line 20, in read_email_from_gmail
result, data = mail.fetch(i, '(RFC822)' )
File "C:\Users\devda\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 529, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File "C:\Users\devda\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 1191, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Users\devda\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 956, in _command
data = data + b' ' + arg
TypeError: can't concat int to bytes
>>>
I followed the tutorial from here
What I want to do is to extract content from email which is shown in image

I had to make a few changes to your code in order to get it to work on Python 3.5.1. I have inlined comments below.
# no need to import smtplib for this code
# no need to import time for this code
import imaplib
import email
def read_email_from_gmail():
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('my_mail','my_pwd')
mail.select('inbox')
result, data = mail.search(None, 'ALL')
mail_ids = data[0]
id_list = mail_ids.split()
first_email_id = int(id_list[0])
latest_email_id = int(id_list[-1])
for i in range(latest_email_id,first_email_id, -1):
# need str(i)
result, data = mail.fetch(str(i), '(RFC822)' )
for response_part in data:
if isinstance(response_part, tuple):
# from_bytes, not from_string
msg = email.message_from_bytes(response_part[1])
email_subject = msg['subject']
email_from = msg['from']
print ('From : ' + email_from + '\n')
print ('Subject : ' + email_subject + '\n')
# nothing to print here
read_email_from_gmail()
Maybe submit a bug report to the author of that blog.

This is what worked for me:
It stores the from address, subject, content(text) into a file of all unread emails.
code:
import email
import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
(retcode, capabilities) = mail.login('mymail','mypassword')
mail.list()
mail.select('inbox')
n=0
(retcode, messages) = mail.search(None, '(UNSEEN)')
if retcode == 'OK':
for num in messages[0].split() :
print ('Processing ')
n=n+1
typ, data = mail.fetch(num,'(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
original = email.message_from_bytes(response_part[1])
# print (original['From'])
# print (original['Subject'])
raw_email = data[0][1]
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
for part in email_message.walk():
if (part.get_content_type() == "text/plain"): # ignore attachments/html
body = part.get_payload(decode=True)
save_string = str(r"C:\Users\devda\Desktop\Internship\Dumpemail_" + str('richboy') + ".txt" )
myfile = open(save_string, 'a')
myfile.write(original['From']+'\n')
myfile.write(original['Subject']+'\n')
myfile.write(body.decode('utf-8'))
myfile.write('**********\n')
myfile.close()
else:
continue
typ, data = mail.store(num,'+FLAGS','\\Seen')
print (n)

Related

Python script only downloading 3 emails

I am trying to write a script that will scan through all emails from the past year. I can only get it to download the top 3. The tutorial I am following doesn't explain anywhere I can change the number of emails it downloads. Here's the code so far.
# user credentials
email_user = input('Email: ')
email_pass = input('Password: ')
# connect to imap
mail = imaplib.IMAP4_SSL("imap.gmail.com",993)
#login
mail.login(email_user, email_pass)
#select folder
mail.select("INBOX","SPAM")
#filter emails by header with receipt or invoice
type, data = mail.search(None, '(SUBJECT "Invoice")')
mail_ids = data[0]
id_list = mail_ids.split()
for num in data[0].split():
typ, data = mail.fetch(num, '(RFC822)' )
raw_email = data[0][1]
# converts byte literal to string removing b''
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
# downloading attachments
for part in email_message.walk():
# this part comes from the snipped I don't understand yet...
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
if bool(fileName):
filePath = os.path.join('/Users/benbuechler/Desktop/Keepr Receipt Storage', fileName)
if not os.path.isfile(filePath) :
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
subject = str(email_message).split("Subject: ", 10)[1].split("\nTo:", 10)[1]
print('Downloaded "{file}" from email titled "{subject}"'.format(file=fileName, subject=subject))
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1].decode('utf-8'))
email_subject = msg['subject']
email_from = msg['from']
print ('From : ' + email_from + '\n')
print ('Subject : ' + email_subject + '\n')
print(msg.get_payload(decode=True))

"TypeError: can't concat int to bytes" imaplib error

I am creating something so when it receives an email depending on what is the subject line it does a function now the code I have works in python 2.7 and runs in 3.7 but gives one error "TypeError: can't concat int to bytes" I've haven't tried much because I couldn't find anything online and im new to python please let me know
import imaplib
import email
from time import sleep
from ssh import myRoomOff, myRoomOn
count = 0
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('EMAIL', 'PASSWORD')
mail.list()
mail.select('inbox')
#need to add some stuff in here
while True:
mail.select('inbox')
typ, data = mail.search(None, 'UNSEEN')
ids = data[0]
id_list = ids.split()
#get the most recent email id
latest_email_id = int( id_list[-1] )
#iterate through 15 messages in decending order starting with latest_email_id
#the '-1' dictates reverse looping order
for i in range( latest_email_id, latest_email_id-1, -1 ):
typ, data = mail.fetch(i, '(RFC822)' )
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_bytes(response_part[1])
varSubject = msg['subject']
count += 1
print(str(count) + ", " + varSubject)
if varSubject == "+myRoom":
myRoomOn()
elif varSubject == "-myRoom":
myRoomOff()
else:
print("I do not understand this email!")
pass
sleep(2)
error
Traceback (most recent call last):
File "/Users/danielcaminero/Desktop/alexaCommandThing/checkForEmail.py", line 28, in <module>
typ, data = mail.fetch(i, '(RFC822)' )
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 548, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 1230, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 988, in _command
data = data + b' ' + arg
TypeError: can't concat int to bytes
The first argument of mail.fetch() must be a string, but range() yields an int.
So an string-cast would fix it:
typ, data = mail.fetch(str(i), '(RFC822)')
Not a necessary change to fix it, but a more pythonic way to reverse a list is to use list slicing [::-1]. You can save some lines and the range() call.
Furthermore your indent is wrong, you handle only the data from the last iteration.
...
id_list = ids.split()
for i in id_list[::-1]:
typ, data = mail.fetch(i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_bytes(response_part[1])
varSubject = msg['subject']
count += 1
print(str(count) + ", " + varSubject)
if varSubject == "+myRoom":
myRoomOn()
elif varSubject == "-myRoom":
myRoomOff()
else:
print("I do not understand this email!")
pass
(Here you don't need the string cast, because id_list is a list of byte-string. And a byte-string is a valid value for fetch() too.)

Read gmail and printing it [duplicate]

This question already has an answer here:
How to read email using Python 3
(1 answer)
Closed 3 years ago.
I want to only print the sender name and message of a received gmail in Python. I tried the code given below. Please help me with that.
import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('raskarakash2017#gmail.com', '02426236032')
mail.list()
mail.select('inbox')
typ, data = mail.search(None, 'ALL')
for num in data[0].split():
typ, data = mail.fetch(num, '(RFC822)')
print ('Message %s\n%s\n' % (num, data[0][1]))
mail.close()
mail.logout()
This code prints the whole information of the gmail, but I don't need that.
I think you are looking for it:
import imaplib
import email
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('raskarakash2017#gmail.com', '02426236032')
mail.list()
mail.select('inbox')
typ, data = mail.search(None, 'ALL')
for num in data[0].split():
typ, data = mail.fetch(num, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
varSubject = msg['subject']
varFrom = msg['from']
#remove the brackets around the sender email address
varFrom = varFrom.replace('<', '')
varFrom = varFrom.replace('>', '')
#add ellipsis (...) if subject length is greater than 35 characters
if len( varSubject ) > 35:
varSubject = varSubject[0:32] + '...'
print '[' + varFrom.split()[-1] + '] ' + varSubject
mail.close()
mail.logout()
Details are here : link

'str' object has no attribute 'message_from_bytes'

I have a piece of code to get emails from messages from my inbox (gmail). Getting emails work correct when I print email_from but I would like to do some operation on data split name and email etc. but then code broke after print first loop step and I got the error:
Traceback (most recent call last):
File "C:\Users\loc\Desktop\extract_gmail.py", line 24, in <module>
email_message_raw = email.message_from_bytes(data[0][1])
AttributeError: 'str' object has no attribute 'message_from_bytes'
Can you give me some advice how to solve this problem?
Code:
import imaplib
import email
from email.header import Header, decode_header, make_header
# Connection settings
HOST = 'imap.gmail.com'
USERNAME = '***'
PASSWORD = '***'
m = imaplib.IMAP4_SSL(HOST, 993)
m.login(USERNAME, PASSWORD)
m.select('INBOX')
result, data = m.uid('search', None, "ALL")
if result == 'OK':
for num in data[0].split()[:5]:
result, data = m.uid('fetch', num, '(RFC822)')
if result == 'OK':
# Get raw message
email_message_raw = email.message_from_bytes(data[0][1])
# Decode headers
email_from = str(make_header(decode_header(email_message_raw['From'])))
# Print each name and email
name, email = email_from.split('<')
email.replace(">", "")
print(name + "|" + email)
# When i swap to just print email_from then works
# print(email_from)
# Close server connection
m.close()
m.logout()
In your code you replaced the email variable..
Try this...
import imaplib
import email
from email.header import Header, decode_header, make_header
# Connection settings
HOST = 'imap.gmail.com'
USERNAME = '***'
PASSWORD = '***'
m = imaplib.IMAP4_SSL(HOST, 993)
m.login(USERNAME, PASSWORD)
m.select('INBOX')
result, data = m.uid('search', None, "ALL")
if result == 'OK':
for num in data[0].split()[:5]:
result, data = m.uid('fetch', num, '(RFC822)')
if result == 'OK':
# Get raw message
email_message_raw = email.message_from_bytes(data[0][1])
# Decode headers
email_from = str(make_header(decode_header(email_message_raw['From'])))
# Print each name and email
name, email_addr = email_from.split('<')
email_addr.replace(">", "")
print(name + "|" + email_addr)
# When i swap to just print email_from then works
# print(email_from)
# Close server connection
m.close()
m.logout()
I had the same error [happily solved], my mistake was in the shebang. That should point to python3.
#! /usr/bin/env python3

Unable to retrieve gmail messages from any folder other than inbox (Python3 issue)

Update: my code works under python 2.6.5 but not python 3 (I'm using 3.4.1).
I'm unable to search for messages in the "All Mail" or "Sent Mail" folders - I get an exception:
imaplib.error: SELECT command error: BAD [b'Could not parse command']
my code:
import imaplib
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail#gmail.com","mypassword")
m.select("[Gmail]/All Mail")
using m.select("[Gmail]/Sent Mail") doesn't work either.
But reading from the inbox works:
import imaplib
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail#gmail.com","mypassword")
m.select("inbox")
...
I used the mail.list() command to verify the folder names are correct:
b'(\\HasNoChildren) "/" "INBOX"',
b'(\\Noselect \\HasChildren) "/" "[Gmail]"',
b'(\\HasNoChildren \\All) "/" "[Gmail]/All Mail"',
b'(\\HasNoChildren \\Drafts) "/" "[Gmail]/Drafts"',
b'(\\HasNoChildren \\Important) "/" "[Gmail]/Important"',
b'(\\HasNoChildren \\Sent) "/" "[Gmail]/Sent Mail"',
b'(\\HasNoChildren \\Junk) "/" "[Gmail]/Spam"',
b'(\\HasNoChildren \\Flagged) "/" "[Gmail]/Starred"',
b'(\\HasNoChildren \\Trash) "/" "[Gmail]/Trash"'
I'm following the solutions from these questions, but they don't work for me:
imaplib - What is the correct folder name for Archive/All Mail in Gmail?
I cannot search sent emails in Gmail with Python
Here is a complete sample program that doesn't work on Python 3:
import imaplib
import email
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail#gmail.com","mypassword")
m.select("[Gmail]/All Mail")
result, data = m.uid('search', None, "ALL") # search all email and return uids
if result == 'OK':
for num in data[0].split():
result, data = m.uid('fetch', num, '(RFC822)')
if result == 'OK':
email_message = email.message_from_bytes(data[0][1]) # raw email text including headers
print('From:' + email_message['From'])
m.close()
m.logout()
The following exception is thrown:
Traceback (most recent call last):
File "./eport3.py", line 9, in <module>
m.select("[Gmail]/All Mail")
File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 682, in select
typ, dat = self._simple_command(name, mailbox)
File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 1134, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 965, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: SELECT command error: BAD [b'Could not parse command']
Here's the corresponding Python 2 version that works:
import imaplib
import email
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail#gmail.com","mypassword")
m.select("[Gmail]/All Mail")
result, data = m.uid('search', None, "ALL") # search all email and return uids
if result == 'OK':
for num in data[0].split():
result, data = m.uid('fetch', num, '(RFC822)')
if result == 'OK':
email_message = email.message_from_string(data[0][1]) # raw email text including headers
print 'From:' + email_message['From']
m.close()
m.logout()
As it's mentioned in this answer:
Try using m.select('"[Gmail]/All Mail"'), so that the double quotes get transmitted.
I suspect imaplib is not properly quoting the string, so the server gets what looks like two arguments: [Gmail]/All, and Mail.
And it works in python v3.4.1
import imaplib
import email
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail#gmail.com","mypassword")
m.select('"[Gmail]/All Mail"')
result, data = m.uid('search', None, "ALL") # search all email and return uids
if result == 'OK':
for num in data[0].split():
result, data = m.uid('fetch', num, '(RFC822)')
if result == 'OK':
email_message = email.message_from_bytes(data[0][1]) # raw email text including headers
print('From:' + email_message['From'])
m.close()
m.logout()

Categories