【问题标题】:Sending email via django on windows在 Windows 上通过 django 发送电子邮件
【发布时间】:2015-03-12 02:47:56
【问题描述】:

呃,到目前为止,我已经尝试了很多方法,我已经能够在网上找到,但无济于事。我的主要目标是从私有 LAN 上的基于 django 的 Web 应用程序发送电子邮件。我不在乎它是如何发生的...smtp 或 win32com 和前景...任何东西,只要它有效。

在 django shell 中使用 django 默认设置和此代码:

from django.core.mail import send_mail
send_mail('subject','message','myemail@email.com',['myemail@email.com'])

我收到此错误:

Traceback (most recent call last):
    File "<console>", line 1, in <module>
    File "C:\Python27_32\lib\site-packages\django\core\mail\__init__.py", line 50, in send_mail
        connection=connection).send()
    File "C:\Python27_32\lib\site-packages\django\core\mail\message.py", line 274, in send
        return self.get_connection(fail_silently).send_messages([self])
    File "C:\Python27_32\lib\site-packages\django\core\mail\backends\smtp.py", line 87, in send_messages
        new_conn_created = self.open()
    File "C:\Python27_32\lib\site-packages\django\core\mail\backends\smtp.py", line 48, in open
        local_hostname=DNS_NAME.get_fqdn())
    File "C:\Python27_32\lib\smtplib.py", line 250, in __init__
        (code, msg) = self.connect(host, port)
    File "C:\Python27_32\lib\smtplib.py", line 311, in connect
        (code, msg) = self.getreply()
    File "C:\Python27_32\lib\smtplib.py", line 359, in getreply
        + str(e))
SMTPServerDisconected: Connection unexpectedly closed: [Errno 10057] A request to send or 
receive data was disallowed because the socket is not connected and (when sending on a 
datagram socket using a sendto call) no address was supplied

深入研究代码后,我在 smtplib 中插入了一条打印语句,显示地址为('localhost', 25)。所以我不知道为什么它说没有提供地址。那时我发现了django-smtp-ssl.py 并在安装后将以下内容添加到我的设置中:

EMAIL_USE_TLS = True #also tried 1
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_HOST_USER = 'myemail@email.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 465

然后执行相同的代码,我现在得到:

Traceback (most recent call last):
    File "<console>", line 1, in <module>
    File "C:\Python27_32\lib\site-packages\django\core\mail\__init__.py", line 50, in send_mail
        connection=connection).send()
    File "C:\Python27_32\lib\site-packages\django\core\mail\message.py", line 274, in send
        return self.get_connection(fail_silently).send_messages([self])
    File "C:\Python27_32\lib\site-packages\django\core\mail\backends\smtp.py", line 87, in send_messages
        new_conn_created = self.open()
    File "C:\Python27_32\lib\site-packages\django_smtp_ssl.py", line 12, in open
        local_hostname=DNS_NAME.getfqdn())
    File "C:\Python27_32\lib\smtplib.py", line 777, in __init__
        SMTP.__init__(self, host, port, local_hostname, timeout)
    File "C:\Python27_32\lib\smtplib.py", line 250, in __init__
        (code, msg) = self.connect(host, port)
    File "C:\Python27_32\lib\smtplib.py", line 311, in connect
        (code, msg) = self.getreply()
    File "C:\Python27_32\lib\smtplib.py", line 355, in getreply
        line = self.file.readline()
    File "C:\Python27_32\lib\smtplib.py", line 186, in readline
        chr = self.sslobj.read(1)
    File "C:\Python27_32\lib\ssl.py", line 160, in read
        return self._sslobj.read(len)
AttributeError: 'NoneType' object has no attribute 'read'

我还尝试在使用 Apache 的生产环境中通过 Outlook 和 win32com 发送邮件。我收到了几个不同的例外尝试,例如:'Server execution failed'Call was rejected by callee'。虽然它可以在 django shell 中工作(当然)。

有没有人看到这些错误并找到解决方法?感谢您的帮助!

【问题讨论】:

    标签: python django email outlook smtp


    【解决方案1】:

    这是一种变通方法,简直就是伪劣,但它会完成工作,直到出现更好的解决方案。

    首先,我创建了一个custom command,可以从manage.py 调用它,因为它不是从 Apache 服务器执行的(这进一步告诉我系统没有将 Apache 识别为自动生成电子邮件的有效用户,当然有一种告诉系统“没关系”的方法)。

    之后,我在 Windows 任务计划程序中创建了一个每 30 分钟运行一次的任务(这足以实现目标)。行动:

    Start a program  C:\python27_32\python.exe C:\www\myproject\manage.py send_email
    

    myapp/management/commands/send_email.py

    from django.core.management.base import BaseCommand
    from myapp.util.gen_email import AutoEmail #custom code to generate email via win32com
    from myapp.models import MyModel
    
    class Command(BaseCommand):
        def __init__(self, *args, **kwargs):
            super(Command, self).__init__(*args, **kwargs)
            items = MyModel.objects.filter(status__in=['status_a','status_b'])
            if items:
                self.send_proc_email()
    
        def send_proc_email(self):
            subject = 'There are new items to process'
            to = 'someemail@email.com'
            message = ''
            email = AutoEmail(subject, message, to)
    
        def handle(self, *args, **kwargs):
            pass
    

    远非最佳,我知道,但嘿,如果它有效......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 2012-07-20
      • 2017-06-16
      • 2014-12-10
      • 1970-01-01
      相关资源
      最近更新 更多