“Permission denied” when trying to send verification email

左心房为你撑大大i 提交于 2020-01-03 10:44:39

问题


I'm running a django website on a fedora server (Fedora release 15 (Lovelock)) using Apache and mod_wsgi. recently I tried to add a registration system using the django-registration app (version 0.7), but unfortunately I get a "[Errno 13] Permission denied" when the app is trying to send verification email to a newly registered user. I've configured the setting file of my project to send emails using a gmail account this way:

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

I can send emails when I manually import send_mail while using project's shell. I haven't changed anything in the views or models. here is the registration template file:

{% extends "base.html" %}
{% load i18n %}

{% block content %}
<form method="post" action="/accounts/register/">
  {% csrf_token %}
  {{ form }}
  <input type="submit" value="Register"/>
</form>
{% endblock %}

and here's the error I get:

[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

this is the complete traceback:

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

回答1:


This may be being prevented by SELinux. I would check those logs and see if it's denying you the permission. You can probably find the primary log at /var/log/audit/audit.log Try the action and see if it generates a new message at the end of the log. If so, it's SELinux which is disallowing the email send. If that's generating the denial, then you'll need to update your SELinux policy, probably using audit2allow. For instructions on how to do that, see the SELinux CentOS Howto. CentOS and Fedora are close enough that the steps outlined there should work, if SELinux is what's denying the action. If not, then obviously, this would be irrelevant.

You can also try temporarily disabling SELinux enforcement by using the command setenforce 0 and see if the problem goes away. Whether it does or not, it's a good idea to use setenforce 1 to turn enforcement back on again.




回答2:


This could be an IO Error. Are you doing any print statements or writing stuff to a file in your view ? If so you can have permission issues. Please check and respond.




回答3:


These are the lines you have to add to your settings.py:

EMAIL_HOST = 'smtp.webfaction.com'
EMAIL_HOST_USER = '<mailbox>'
EMAIL_HOST_PASSWORD = '<password>'
DEFAULT_FROM_EMAIL = '<address>'
SERVER_EMAIL = '<address>'

delete @domain.com in EMAIL_HOST_USER so it will be just myemailaddress



来源:https://stackoverflow.com/questions/7912553/permission-denied-when-trying-to-send-verification-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!