发送邮件可以在manage.py shell中运行,但不能在运行的应用程序

时间:2017-10-31 04:19:12

标签: python django email mod-wsgi

当通过视图提交评论时,我尝试使用django.core.mail.mail_admins(Django 1.11)向网站管理员发送电子邮件。我的Comment模型上有一个名为send_notification_email的方法:

from django.db import models
from django.core.mail import mail_admins

class Comment(models.Model):
    comment = models.TextField()
    time_received = models.DateTimeField()

    def send_notification_email(self):
        mail_admins(
            f'New Comment (id = {self.id})',
            f'Comment {self.id}, received at {self.time_received}: {self.comment}',
        )

视图创建Comment的实例,将其保存到数据库,然后调用send_notification_email()。当发生这种情况时,我收到以下错误:

Traceback (most recent call last):
  File "/home/conda/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/home/conda/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/conda/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/conda/lib/python3.6/contextlib.py", line 52, in inner
    return func(*args, **kwds)
  File "/home/radio-website/main/views.py", line 56, in comments
    )
  File "/home/radio-website/main/models.py", line 33, in send_notification_email
    [settings.SERVER_EMAIL],
  File "/home/conda/lib/python3.6/site-packages/django/core/mail/__init__.py", line 56, in send_mail
    fail_silently=fail_silently,
  File "/home/conda/lib/python3.6/site-packages/django/core/mail/__init__.py", line 36, in get_connection
    klass = import_string(backend or settings.EMAIL_BACKEND)
  File "/home/conda/lib/python3.6/site-packages/django/utils/module_loading.py", line 20, in import_string
    module = import_module(module_path)
  File "/home/conda/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/conda/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 4, in <module>
    import ssl
  File "/home/conda/lib/python3.6/ssl.py", line 101, in <module>
    import _ssl             # if we can't import it, let the error propagate
ImportError: /home/conda/lib/python3.6/lib-dynload/_ssl.cpython-36m-x86_64-linux-gnu.so: undefined symbol: SSL_get0_alpn_selected

使用Conda安装Python。服务器通过Apache 2.4托管mod_wsgi,以守护进程模式运行。

manage.py shell中运行类似的代码按预期工作。例如,

$ python manage.py shell
>>> from main.models import Comment
>>> c = Comment(id = 0, comment = 'foo')
>>> c.send_notification_email()

成功向管理员用户发送电子邮件。我已经检查过这样做&#34;手动&#34;以root用户身份运行,以及Apache运行应用程序的www-data用户。从正常的Python解释器会话或import ssl运行时,import _sslmanage.py shell都可以正常工作。

我觉得很奇怪,这可以在shell中运行,但在应用程序实际运行时却不行。这个错误看起来很奇怪而且模糊 - 我的Python安装可能有问题吗?

0 个答案:

没有答案