呃,我尝试过很多方法,到目前为止我能在网上找到,但无济于事。我的主要目标是从私有LAN上的基于Django的Web应用程序发送电子邮件。我不在乎它是怎么发生的...... smtp或win32com和outlook ......任何东西,只要它有效。
在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中插入了一个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 = '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(当然)。
有没有人看到这些错误并找到解决方案?谢谢你的帮助!
答案 0 :(得分:0)
它的解决方法,简直就是粗制滥造,但它会完成工作,直到有更好的解决方案出现。
首先我创建了一个custom command,可以从manage.py
调用,因为它不是从Apache服务器执行的(这进一步告诉我系统不会将Apache识别为自动电子邮件的有效用户一代,当然有一种告诉系统的方式&#34;它没问题&#34;)。
之后,我在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 = 'someemail@email.com'
message = ''
email = AutoEmail(subject, message, to)
def handle(self, *args, **kwargs):
pass
我知道,这远非最佳,但如果有效的话,嘿......