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
Related
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))
I am trying to read an email in Gmail that has a specific subject and get the OTP value within the email. I am using imaplib
import imaplib
def get_CreateAccount_OTP(self, email_type):
gmail = imaplib.IMAP4_SSL("imap.gmail.com", 993)
gmail.login(self.gmail_username, self.gmail_password)
gmail.select('Inbox', readonly=True)
type, data = gmail.search(None, '(SUBJECT "Here\'s your Texas by Texas email verification.")')
I got the type returned as Ok, but the data as below
data = {list: 1} [b'']
0 = {bytes: 0} b''
__len__ = {int} 1
After that line, it's not going into the below "for loop"
for num in data[0].split():
typ, data = gmail.fetch(num, '(RFC822)')
raw_email = data[0][1]
raw_email_string = raw_email.decode('utf-8')
email_message = str(email.message_from_string(raw_email_string))
email_message_list = email_message.split('\n')
RE_TIME_STAMP_PATTERN = re.compile((r'\d{6}'))
for line in email_message_list:
print(line)
if 'Your sign-in verification code is ' in line:
self.OTP = re.findall(RE_TIME_STAMP_PATTERN, line)[0]
break
self.log.info("OTP:",self.OTP)
return self.OTP
Note: I am new to Python and learning it slowly. Please bare with my silly questions
Thanks in advance
I found the issue that the string has special char and the implib is not converting the char to Unicode. So I have to remove the word that has the special char in my string.
import imaplib
def get_CreateAccount_OTP(self, email_type):
subject="your Texas by Texas email verification."
gmail = imaplib.IMAP4_SSL("imap.gmail.com", 993)
gmail.login(self.gmail_username, self.gmail_password)
gmail.select('Inbox', readonly=True)
type, data = gmail.search(None, '(UNSEEN SUBJECT "%s")' % subject)
for num in data[0].split():
typ, data = gmail.fetch(num, '(RFC822)')
raw_email = data[0][1]
raw_email_string = raw_email.decode('utf-8')
email_message = str(email.message_from_string(raw_email_string))
email_message_list = email_message.split('\n')
RE_TIME_STAMP_PATTERN = re.compile((r'\d{6}'))
for line in email_message_list:
print(line)
if 'Your sign-in verification code is ' in line:
self.OTP = re.findall(RE_TIME_STAMP_PATTERN, line)[0]
break
self.log.info("OTP:",self.OTP)
return self.OTP
How can i read the body of any mail its not coming properly in this
manner
I tried this :
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login(email_user, email_pass)
mail.select('Inbox')
type, data = mail.search(None, 'ALL')
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)
subject = str(email_message).split("Subject: ", 1)[1].split("\nTo:", 1)[0]
#body = str(email_message).split("body: ", 1)[1].split("\nTo:", 1)[0]
print(email_message);
its showing
If you simply want to parse the email and access the body, then consider using mail-parser. It's a simple mail-parser that takes as input a raw email and generates a parsed object.
import mailparser
mail = mailparser.parse_from_file(f)
mail = mailparser.parse_from_file_obj(fp)
mail = mailparser.parse_from_string(raw_mail)
mail = mailparser.parse_from_bytes(byte_mail)
How to Use:
mail.body #use this to access the body contents
mail.to
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)
Here is my code:
conn = imaplib.IMAP4_SSL('imap.gmail.com')
conn.login('username', 'password')
conn.select()
typ, data = conn.search(None, "ALL")
parser1 = HeaderParser()
for num in data[0].split():
typ, data = conn.fetch(num, '(RFC822)')
header_data = str(data[1][0])
msg = email.message_from_string(header_data)
print(msg.keys())
print(msg['Date'])
Why am i getting "[]" for the printout of msg.keys() and "None" for the msg['Date']. No error messages. However, if i comment out the last 4 lines of code, and type print(data), then all the headers get printed? Im using python 3.4
conn.fetch returns tuples of message part envelope and data. For some reason -- I'm not sure why -- it may also return a string, such as ')'. So instead of hard-coding data[1][0], it's better (more robust) to just loop through the tuples in data and parse the message parts:
typ, msg_data = conn.fetch(num, '(RFC822)')
for response_part in msg_data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
For example,
import imaplib
import config
import email
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login(config.GMAIL_USER2, config.GMAIL_PASS2)
try:
conn.select()
typ, data = conn.search(None, "ALL")
print(data)
for num in data[0].split():
typ, msg_data = conn.fetch(num, '(RFC822)')
for response_part in msg_data:
if isinstance(response_part, tuple):
part = response_part[1].decode('utf-8')
msg = email.message_from_string(part)
print(msg.keys())
print(msg['Date'])
finally:
try:
conn.close()
except:
pass
finally:
conn.logout()
Much of this code comes from Doug Hellman's imaplib tutorial.