我正在使用Apache和mod_wsgi在Fedora服务器(Fedora版本15(Lovelock))上运行django网站。最近我尝试使用django-registration app(0.7版)添加注册系统,但不幸的是,当应用程序尝试向新注册的用户发送验证邮件时,我得到了“[Errno 13]权限被拒绝”。我已经将项目的设置文件配置为使用gmail帐户以这种方式发送电子邮件:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemailaddress@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
我在使用项目shell时手动导入send_mail时可以发送电子邮件。 我没有改变视图或模型中的任何内容。这是注册模板文件:
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form method="post" action="/accounts/register/">
{% csrf_token %}
{{ form }}
<input type="submit" value="Register"/>
</form>
{% endblock %}
这是我得到的错误:
[Errno 13] Permission denied
Request Method: POST
Request URL: http://myip/accounts/register/
Django Version: 1.3.1
Exception Type: error
Exception Value:
[Errno 13] Permission denied
Exception Location: /usr/lib64/python2.7/socket.py in create_connection, line 571
这是完整的追溯:
Traceback: File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/.../lib/registration/views.py" in register 148. new_user = form.save(profile_callback=profile_callback) File "/.../lib/registration/forms.py" in save 88. send_email = True) File "/.../lib/registration/models.py" in create_inactive_user 127. send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [new_user.email]) File "/usr/lib/python2.7/site-packages/django/core/mail/__init__.py" in send_mail 61. connection=connection).send() File "/usr/lib/python2.7/site-packages/django/core/mail/message.py" in send 251. return self.get_connection(fail_silently).send_messages([self]) File "/usr/lib/python2.7/site-packages/django/core/mail/backends/smtp.py" in send_messages 79. new_conn_created = self.open() File "/usr/lib/python2.7/site-packages/django/core/mail/backends/smtp.py" in open 42. local_hostname=DNS_NAME.get_fqdn()) File "/usr/lib64/python2.7/smtplib.py" in __init__ 239. (code, msg) = self.connect(host, port) File "/usr/lib64/python2.7/smtplib.py" in connect 295. self.sock = self._get_socket(host, port, self.timeout) File "/usr/lib64/python2.7/smtplib.py" in _get_socket 273. return socket.create_connection((port, host), timeout) File "/usr/lib64/python2.7/socket.py" in create_connection 571. raise err Exception Type: error at /accounts/register/ Exception Value: [Errno 13] Permission denied
答案 0 :(得分:9)
SELinux可能会阻止这种情况。我会检查这些日志,看看它是否拒绝你的许可。您可以在/var/log/audit/audit.log中找到主日志。尝试操作并查看它是否在日志末尾生成新消息。如果是这样,它是SELinux,它不允许发送电子邮件。如果这会产生拒绝,那么您需要更新SELinux策略,可能使用audit2allow。有关如何执行此操作的说明,请参阅SELinux CentOS Howto。 CentOS和Fedora非常接近,如果SELinux拒绝采取行动,那么其中列出的步骤应该有效。如果没有,那么显然,这将是无关紧要的。
您还可以尝试使用命令setenforce 0
临时禁用SELinux强制执行,并查看问题是否消失。无论是否这样做,最好使用setenforce 1
重新强制执行。
答案 1 :(得分:1)
这可能是IO错误。您是否在视图中执行任何打印语句或将内容写入文件?如果是这样,您可以拥有权限问题。请检查并回复。
答案 2 :(得分:0)
这些是您必须添加到settings.py中的行:
EMAIL_HOST = 'smtp.webfaction.com'
EMAIL_HOST_USER = '<mailbox>'
EMAIL_HOST_PASSWORD = '<password>'
DEFAULT_FROM_EMAIL = '<address>'
SERVER_EMAIL = '<address>'
在EMAIL_HOST_USER
中删除@ domain.com,因此它只是myemailaddress