简体   繁体   English

如何使用python迭代收件箱中的每封电子邮件?

[英]How do you iterate through each email in your inbox using python?

I'm completely new to programming and I'm trying to build an autorespoder to send a msg to a specific email address. 我是编程新手,我正在尝试构建一个autorespoder来将msg发送到特定的电子邮件地址。

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. 使用if语句,我可以检查收件箱中是否有来自某个地址的电子邮件,我可以发送电子邮件,但是如果该地址有多封电子邮件,我该如何制作for循环来为每个地址发送电子邮件来自该特定地址的电子邮件

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 但我得到错误:“无法分配给该函数调用”

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循环时 ,它应该是:

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)). x是一个变量,它一次保存一个项的值(并且只能在for循环体中访问(有一个例外,但这在这里并不重要))。

What you are doing is not related to the imaplib module but just wrong syntax. 你在做什么与imaplib模块无关,只是错误的语法。

Btw. 顺便说一句。 .select() selects a mailbox and only returns the number of messages in the mailbox . .select()选择邮箱,仅返回邮箱中的邮件数 Ie just a scalar value, no sequence you could iterate over: 即只是一个标量值,没有可以迭代的序列:

IMAP4.select([mailbox[, readonly]]) IMAP4.select([mailbox [,readonly]])
Select a mailbox. 选择一个邮箱。 Returned data is the count of messages in mailbox (EXISTS response). 返回的数据是邮箱中的邮件计数(EXISTS响应)。 The default mailbox is 'INBOX'. 默认邮箱是“INBOX”。 If the readonly flag is set, modifications to the mailbox are not allowed. 如果设置了只读标志,则不允许对邮箱进行修改。

(This is indeed related to imaplib module ;)) (这确实与imaplib模块有关;))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何访问gmail收件箱并使用python管理电子邮件? - How do you accsess to a gmail inbox and manage emails using python? 在 Python 中,如何遍历 CSS 页码? - In Python, How do you iterate through CSS page numbers? 你如何使用python 2.7迭代一个gzipped回车文件? - How do you iterate through a gzipped carriage-return file using python 2.7? 我如何使用Selenium Python遍历每个Google搜索页面,但是没有发生 - How do I iterate through each google search page using Selenium Python,but its not happening 你如何遍历列表中的文件? - How do you iterate through files in a list? 您如何遍历 pandas Dataframe 中的组,对每个组进行操作,然后将值分配给原始 Dataframe? - How do you iterate through groups in a pandas Dataframe, operate on each group, then assign values to the original dataframe? 我如何使用python迭代json对象 - how do i iterate through json objects using python 即使一条语句失败,如何遍历您的 python 脚本? - How to iterate through your python script even if one statement failed? 如何使用python窗口将屏幕的可裁剪png图像保存到桌面? - How do you save a croppable png image of your screen to your desktop using python windows? 如何遍历通过函数传递的参数列表? - How do you iterate through the list of parameters passed through a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM