Hi,
I'm trying to get a list of the subjects that I'm getting from emails in gmail.
I can get the subjects, but I can't seem to get them into a list. Here's my code so far:

#! usr/bin/env python

import imaplib
M=imaplib.IMAP4_SSL('imap.gmail.com', 993)
M.login('user@gmail.com','password')
M.select()
typ, emails = M.search(None, 'ALL')
subject = []
for email in emails[0].split():
	typ, subjects = M.fetch(email, '(BODY[HEADER.FIELDS (SUBJECT)])')
	print '%s' % subjects[0][1]
	subject = subjects[0][1]
M.close()
M.logout()
print subject

The first print statement prints the subjects, but once I've added them to list 'subject', and try to print it, it only prints the last subject. I've tried changing the following lines:

subject =  subjects[0][1]
to
subject += subjects[0][1]

but that adds the subjects to the list as individual characters, instead of a string.
I've been working on this one problem for over an hour now, and I know it's a very basic thing (at least I think it should be), yet I can't solve it.
Any help appreciated...

Instead of:
subject = subjects[0][1]
Try:
subject.append(subjects[0][1])

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.