python django 发送和接收电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/730573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:45:52  来源:igfitidea点击:

django to send AND receive email?

pythondjangoemailpop3django-email

提问by Jiaaro

I have gotten quite familiar with django's email sending abilities, but I havn't seen anything about it receiving and processing emails from users. Is this functionality available?

我已经非常熟悉 django 的电子邮件发送功能,但我没有看到它接收和处理来自用户的电子邮件。这个功能可用吗?

A few google searches have not turned up very promising results. Though I did find this: Receive and send emails in python

一些谷歌搜索并没有出现非常有希望的结果。虽然我确实找到了这个:Receive and send emails in python

Am I going to have to roll my own? if so, I'll be posting that app faster than you can say... whatever you say.

我必须自己滚动吗?如果是这样,我会以比您说的更快的速度发布该应用程序……无论您说什么。

thanks, Jim

谢谢,吉姆

update: I'm not trying to make an email server, I just need to add some functionality where you can email an image to the site and have it pop up in your account.

更新:我不是要制作电子邮件服务器,我只需要添加一些功能,您可以通过电子邮件将图像发送到网站并在您的帐户中弹出。

采纳答案by tghw

There's an app called jutda-helpdeskthat uses Python's popliband imaplibto process incoming emails. You just have to have an account somewhere with POP3 or IMAP access.

有一个称为应用程序jutda-服务支持使用Python的poplibimaplib来处理传入的电子邮件。您只需要在某处拥有一个可以访问 POP3 或 IMAP 的帐户。

This is adapted from their get_email.py:

这是改编自他们的get_email.py

def process_mail(mb):
    print "Processing: %s" % q
    if mb.email_box_type == 'pop3':
        if mb.email_box_ssl:
            if not mb.email_box_port: mb.email_box_port = 995
            server = poplib.POP3_SSL(mb.email_box_host, int(mb.email_box_port))
        else:
            if not mb.email_box_port: mb.email_box_port = 110
            server = poplib.POP3(mb.email_box_host, int(mb.email_box_port))
        server.getwelcome()
        server.user(mb.email_box_user)
        server.pass_(mb.email_box_pass)

        messagesInfo = server.list()[1]

        for msg in messagesInfo:
            msgNum = msg.split(" ")[0]
            msgSize = msg.split(" ")[1]
            full_message = "\n".join(server.retr(msgNum)[1])

            # Do something with the message

            server.dele(msgNum)
        server.quit()

    elif mb.email_box_type == 'imap':
        if mb.email_box_ssl:
            if not mb.email_box_port: mb.email_box_port = 993
            server = imaplib.IMAP4_SSL(mb.email_box_host, int(mb.email_box_port))
        else:
            if not mb.email_box_port: mb.email_box_port = 143
            server = imaplib.IMAP4(mb.email_box_host, int(mb.email_box_port))
        server.login(mb.email_box_user, mb.email_box_pass)
        server.select(mb.email_box_imap_folder)
        status, data = server.search(None, 'ALL')
        for num in data[0].split():
            status, data = server.fetch(num, '(RFC822)')
            full_message = data[0][1]

            # Do something with the message

            server.store(num, '+FLAGS', '\Deleted')
        server.expunge()
        server.close()
        server.logout()

mbis just some object to store all the mail server info, the rest should be pretty clear.

mb只是一些存储所有邮件服务器信息的对象,其余的应该很清楚。

You'll probably need to check the docs on popliband imaplibto get specific parts of the message, but hopefully this is enough to get you going.

您可能需要查看文档poplibimaplib获取消息的特定部分,但希望这足以让您继续前进。

回答by Steve Smith

I know this question is pretty old now but just thought I'd add for future reference that you might want to give http://cloudmailin.coma go. We have quite a few django users using the system and it should be a little simpler than the proposed solution.

我知道这个问题现在已经很老了,但只是想我会添加以供将来参考,您可能想试一试 http://cloudmailin.com。我们有相当多的 django 用户使用该系统,它应该比建议的解决方案简单一些。

回答by David Z

Django is really intended as a web server (well, as a framework that fits into a web server), not as an email server. I suppose you could put some code into a Django web application that starts up an email server, using the kind of code shown in that question you linked to, but I really wouldn't recommend it; it's an abuse of the capabilities of dynamic web programming.

Django 的真正目的是作为一个网络服务器(嗯,作为一个适合网络服务器的框架),而不是一个电子邮件服务器。我想您可以将一些代码放入启动电子邮件服务器的 Django Web 应用程序中,使用您链接到的那个问题中显示的那种代码,但我真的不推荐它;这是对动态网络编程能力的滥用。

The normal practice is to have separate email and web servers, and for that you would want to look into something like Sendmail or (better yet) Postfix. For POP3 you would also need something like Dovecot or Courier, I think. (It's certainly possible to have the email server notify your web application when emails are received so it can act on them, if that's what you want to do.)

通常的做法是拥有单独的电子邮件和 Web 服务器,为此您可能需要研究 Sendmail 或(更好)Postfix 之类的东西。对于 POP3,我认为您还需要 Dovecot 或 Courier 之类的东西。(当然可以让电子邮件服务器在收到电子邮件时通知您的 Web 应用程序,以便它可以对它们采取行动,如果您想这样做的话。)

EDIT: in response to your comments: yes you are trying to make (or at least use) an email server. An email server is just a program that receives emails (and may be capable of sending them as well, but you don't need that).

编辑:回应您的评论:是的,您正在尝试制作(或至少使用)电子邮件服务器。电子邮件服务器只是一个接收电子邮件的程序(并且可能也能够发送它们,但您不需要它)。

You could definitely write a small email server in Python that just receives these emails and saves the images to the filesystem or a database or whatever. (Might be worth asking a new question, about) But don't make it part of your Django web app; keep it as its own separate program.

您绝对可以用 Python 编写一个小型电子邮件服务器,它只接收这些电子邮件并将图像保存到文件系统或数据库或其他任何地方。(可能值得提出一个新问题,关于)但不要让它成为你的 Django 网络应用程序的一部分;将其作为自己的独立程序保留。