views:

78

answers:

1

I'm completely new to programming and I'm trying to build an autorespoder to send a msg to a specific email address.

Using an if statement, I can check if there is an email from a certain address in the inbox and I can send an email, but if there are multiple emails from that address, how can I make a for loop to send an email for every email from that specific address.

I tried to do use this as a loop:

for M.search(None, 'From', address) in M.select(): 

but I get the error: "can't assign to function call" on that line

+1  A: 

As you claim to be new to programming, my best advice is: Always read the documentation.

And maybe you should read a tutorial first.


The documentation provides an example:

import getpass, imaplib

M = imaplib.IMAP4()
M.login(getpass.getuser(), getpass.getpass())
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    print 'Message %s\n%s\n' % (num, data[0][3])
M.close()
M.logout()

Have you tried?


Regarding your code:

When you define a for loop, it should be like:

for x in some_data_set:

x is a variable, that holds the value of one item at a time (and is accessible only in the for loop body (with one exception, but this is not important here)).

What you are doing is not related to the imaplib module but just wrong syntax.

Btw. .select() selects a mailbox and only returns the number of messages in the mailbox. I.e. just a scalar value, no sequence you could iterate over:

IMAP4.select([mailbox[, readonly]])
Select a mailbox. Returned data is the count of messages in mailbox (EXISTS response). The default mailbox is 'INBOX'. If the readonly flag is set, modifications to the mailbox are not allowed.

(This is indeed related to imaplib module ;))

Felix Kling