2015-01-14 23 views
1

呃,我嘗試了很多方法,至今我都能在網上找到,但無濟於事。我的主要目標是從專用LAN上基於django的Web應用程序發送電子郵件。我不在乎它是如何發生的...... smtp或win32com和outlook ......任何東西,只要它有效。在windows上通過django發送電子郵件

使用Django默認設置和這個代碼在Django殼:

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

我得到這個錯誤:

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 print語句顯示地址爲('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 = '[email protected]' 
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' 

我人還試圖通過Outlook和win32com在發送郵件使用Apache的生產環境。我收到了幾個不同的例外,例如:'Server execution failed'Call was rejected by callee'。雖然它在django shell中工作(當然)。

有沒有人看到這些任何這些錯誤,並找到解決辦法?感謝您的任何幫助!

回答

0

它是解決方法,沒什麼缺點,但它會完成工作,直到更好的解決方案出現。

首先我創建了一個custom command可以從manage.py,因爲它沒有從Apache服務器(其中還告訴我,該系統無法識別Apache作爲自動郵件生成一個有效的用戶執行調用,肯定有辦法告訴系統「沒關係」)。

接下來,我在Windows任務計劃程序中創建了一個每30分鐘運行一次的任務(這將足以滿足目標)。操作:

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

的myapp /管理/命令/ 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 = '[email protected]' 
     message = '' 
     email = AutoEmail(subject, message, to) 

    def handle(self, *args, **kwargs): 
     pass 

遠不夠理想,我知道,但是,嘿,如果它工作...