6

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
mmbrian
  • 1,672
  • 2
  • 14
  • 26

3 Answers3

9

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.

Keith Irwin
  • 5,628
  • 22
  • 31
  • do you mean apache error logs? I cheched the one at /etc/httpd/logs/error_log and found this out: [error] [client 173.242.117.169] mod_wsgi (pid=11534): Exception occurred processing WSGI script '/var/www/algorithms.ir/algorithms_ir/apache/django.wsgi'., referer: http://.../accounts/register/ [Thu Oct 27 02:01:14 2011] [error] [client 173.242.117.169] IOError: failed to write data, referer: http://.../accounts/register/ – mmbrian Oct 27 '11 at 07:34
  • I've expanded the answer some with more details. The Apache log was not the one I was referring to. – Keith Irwin Oct 27 '11 at 17:03
  • thank you, sorry for the delay, I tried that but it didn't generate anything new at the end of the log file. guess that's not the problem – mmbrian Oct 29 '11 at 13:22
  • Sounds like not. Too bad. Sorry I couldn't be helpful then. – Keith Irwin Oct 29 '11 at 15:01
  • you were right, it was an SELinux issue, I could solve the problem by applying these commands: setsebool httpd_tmp_exec on setsebool httpd_can_network_connect on and now apache user has permission to send verification emails with my app. I found the solution here http://www.coverfire.com/archives/2010/06/13/djangomod_wsgi-on-fedora-12/ – mmbrian Nov 17 '11 at 08:18
  • Well, I'm glad you got it worked out. I realized that I should've also suggested turning off SELinux enforcement temporarily to see if that made the problem go away. So I'm amending my answer. I wonder why you didn't see anything in the audit log, though. Also, how about an upvote and an accept :) – Keith Irwin Nov 17 '11 at 18:57
  • o! just accepted it:) but I've only got 11 reputation so it doesn't let me vote up, sorry – mmbrian Nov 20 '11 at 17:10
1

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.

unni
  • 2,891
  • 4
  • 19
  • 22
  • sorry for my delay, the only thing happening in my view is adding a new inactive user to my database and then generating an activation message for the user and sending the email. I haven't configured the dns name of my server, I guess that's what causing the problem. for now I've decided to use a captcha verification app instead of email verification – mmbrian Oct 29 '11 at 13:32
0

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

juankysmith
  • 11,839
  • 5
  • 37
  • 62
  • I'm not using webfaction, and I have no problem sending emails with this configuration when I try send_mail after runnig "python manage.py shell".I configured DEFAULT_FROM_EMAIL and SERVER_EMAIL but still it gives the same error – mmbrian Oct 27 '11 at 07:43