Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

502 bad gateway #29

Closed
geweb7 opened this issue Sep 28, 2013 · 18 comments
Closed

502 bad gateway #29

geweb7 opened this issue Sep 28, 2013 · 18 comments

Comments

@geweb7
Copy link

geweb7 commented Sep 28, 2013

Sometimes I get 502 error.

What I see in logs:
File "/usr/lib/python2.6/logging/init.py", line 679, in handle
self.emit(record)
File "/django/utils/log.py", line 113, in emit
mail.mail_admins(subject, message, fail_silently=True, html_message=html_message)
File "/django/core/mail/init.py", line 98, in mail_admins
mail.send(fail_silently=fail_silently)
File "/django/core/mail/message.py", line 255, in send
return self.get_connection(fail_silently).send_messages([self])
File "/post_office/backends.py", line 43, in send_messages
headers=headers, priority=PRIORITY.medium)
File "/django/db/models/manager.py", line 149, in create
return self.get_query_set().create(**kwargs)
File "/django/db/models/query.py", line 416, in create
obj.save(force_insert=True, using=self.db)
File "/post_office/models.py", line 119, in save
self.full_clean()
File "/django/db/models/base.py", line 926, in full_clean
raise ValidationError(errors)
django.core.exceptions.ValidationError: {'from_email': [u'Enter a valid e-mail address.']}

I have regular settings.DEFAULT_FROM_EMAIL
and EMAIL_BACKEND = 'post_office.EmailBackend'

django 1.5

@geweb7
Copy link
Author

geweb7 commented Sep 28, 2013

I realized what was causing this error.
The email in settings.ADMINS was not owned by user with staff privelegies.

However it was very unclear. May be something can be done in such case to notify user what's wrong.

@selwin
Copy link
Collaborator

selwin commented Sep 28, 2013

I don't recall post-office checking whether the user has staff privileges. Are you sure you didn't mistype the email addresses in settings.ADMINS?

@geweb7
Copy link
Author

geweb7 commented Sep 28, 2013

Yes, I'm sure it was correct. After I changed superuser's email in the database there were no promlems anymore.
So may be it's nothing to deal with post-office and the issue can be closed.
Or may be make some checking so that such cases were more evident for debugging.

@selwin
Copy link
Collaborator

selwin commented Sep 28, 2013

I'm closing this issue since post-office doesn't do any permission checking.

@selwin selwin closed this as completed Sep 28, 2013
@geweb7
Copy link
Author

geweb7 commented Oct 2, 2013

Again I get this error on another project. Changing settings.ADMINS and DEFAULT_FROM_EMAIL won't help.

And this is something with post-office. When I remove
EMAIL_BACKEND = 'post_office.EmailBackend'
everything is ok.

@selwin
Copy link
Collaborator

selwin commented Oct 2, 2013

@russian-master what's the recipient's email address?

@geweb7
Copy link
Author

geweb7 commented Oct 2, 2013

I don't know exactly but I guess it is taken from settings.ADMINS. It's regular email address there.

I've noticed that such errors appears when it should render 404 error. And I have SEND_BROKEN_LINK_EMAILS=True
When I changed this setting to False - everything was ok.

@selwin
Copy link
Collaborator

selwin commented Oct 2, 2013

Mind sending me the contents of settings.ADMINS then? post-office regex to validate email addresses so it's likely that one of the emails in ADMINS don't pass that validation.

@selwin selwin reopened this Oct 2, 2013
@geweb7
Copy link
Author

geweb7 commented Oct 2, 2013

I checked https://docs.djangoproject.com/en/dev/ref/settings/#send-broken-link-emails and learned that recipient email is taken from settings.MANAGERS.
I have MANAGERS = ADMINS in my settings.py.
However django complains about "from_email" not recipient address.

@selwin
Copy link
Collaborator

selwin commented Oct 2, 2013

Oh my bad, I didn't properly read the traceback. Did you properly set DEFAULT_FROM_EMAIL? https://docs.djangoproject.com/en/dev/ref/settings/#default-from-email

It defaults to webmaster@localhost which is not a valid email (I think).

@geweb7
Copy link
Author

geweb7 commented Oct 2, 2013

I changed settings for test:

ADMINS = (
('admin', 'test@example.com'),
)
DEFAULT_FROM_EMAIL = 'test@example.com'
EMAIL_BACKEND = 'post_office.EmailBackend'
MANAGERS = ADMINS
SEND_BROKEN_LINK_EMAILS = True

result: 502 bad gateway and django.core.exceptions.ValidationError: {'from_email': [u'Enter a valid e-mail address.']} in the uwsgi log

@selwin
Copy link
Collaborator

selwin commented Oct 2, 2013

@russian-master can you test it again to make sure? I just tested it with test@example.com and it seems to pass validation just fine.

@geweb7
Copy link
Author

geweb7 commented Oct 2, 2013

I restarted the server to be sure. And still get this error. And not only for 404 but also for 500 error pages.

When I remove EMAIL_BACKEND = 'post_office.EmailBackend' I get my usual custom 500 html page displayed.
If I can test it in another way... let me know.

@selwin
Copy link
Collaborator

selwin commented Oct 2, 2013

Can you send me the latest traceback? If you can modify the source code, we can try to see what email it was trying to validate.

Change this line to:

raise ValidationError('%s is not a valid e-mail.' % value, code='invalid')

@geweb7
Copy link
Author

geweb7 commented Oct 2, 2013

it said:
django.core.exceptions.ValidationError: {'from_email': [u'root@localhost is not a valid e-mail.']}

I even searched through my project files but didn't find 'root@localhost'.
So probably it's django default and it doesn't use my settings file. (for any case: my settings.py is used in other cases ok - databases etc.).
I don't know why post-office doesn't use default_from_email from my settings.py

@selwin
Copy link
Collaborator

selwin commented Oct 2, 2013

Actually, post-office does use settings.DEFAULT_FROM_EMAIL: https://github.com/ui/django-post_office/blob/master/post_office/mail.py#L57 .

Does it only throw this error when you run the test?

@geweb7
Copy link
Author

geweb7 commented Oct 2, 2013

I dived into the django source and found this code in django\core\mail__init__.py:

mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection)

SERVER_EMAIL !!! Not DEFAULT_FROM_EMAIL

I set it in my settings.py and the problem went away.

But without post-office there were no problem to send emails - I guess django checks settings.SERVER_EMAIL and if it's not set it uses DEFAULT_FROM_EMAIL.
Probably post-office should mimic this behaviour.

@selwin
Copy link
Collaborator

selwin commented Oct 3, 2013

Ah ok, I didn't know about SERVER_EMAIL.

Well, we have two options for default email address, DEFAULT_FROM_EMAIL or SERVER_EMAIL, out of the two, I'd argue that DEFAULT_FROM_EMAIL is more well known.

About the fact that Django's email backend does not raise any errors when sending to root@localhost - I'd argue that it is in fact a non valid email address so it shouldn't have gone through.

I'm going to close this as a wont fix, thanks for bringing this up.

@selwin selwin closed this as completed Oct 3, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants