Repository: dotkom/onlineweb4

Issue:
Registering with an invalid email (according to smtplib) causes errors when sending emails
SMTPRecipientsRefused at /auth/register/
{u'5435435435@45435.4534': (501, '5.1.3 Bad recipient address syntax')}
Request Method: POST
Request URL:    http://moonshine.online.ntnu.no/auth/register/
Django Version: 1.6.1
Exception Type: SMTPRecipientsRefused
Exception Value:  
{u'5435435435@45435.4534': (501, '5.1.3 Bad recipient address syntax')}
Exception Location: /usr/lib/python2.7/smtplib.py in sendmail, line 733



Discussion:
But it's an email field...?

I bet the SMTPlib allows TLDs like .4534, but email field doesn't. Though, I don't know if smtplib.py maybe digs the domain to see if it is actually valid. Imagine if our SMTP server only served under our private network and we used a fucked up TLD. That is fucked up from smtplibs part.

If smtplib keeps some sort of list of TLDs to allow, then fuck that shit. I don't like it. If it actually checks if the domain resolves, then wow.

Anyways, if this is an exception that this shit will throw, we should catch it. We should kinda look into what exceptions the send_mail can throw and handle them properly.

some extra fucks and shits for good measure

:sheep: :point_left: :shit:

```
 This method may raise the following exceptions:

     SMTPHeloError          The server didn't reply properly to
                            the helo greeting.
     SMTPRecipientsRefused  The server rejected ALL recipients
                            (no mail was sent).
     SMTPSenderRefused      The server didn't accept the from_addr.
     SMTPDataError          The server replied with an unexpected
                            error code (other than a refusal of
                            a recipient).
```

smtplib.SMTPException is a general exception

But yeah, looks like smtplib is trying to send and if sending fails it throws an exception. 

Though, all emails sent from our interfaces are confirmed, so no domain errors should be possible. Though, the server can refuse a deleted account or something. We should try-catch all the send mails anyways.

This must also be fixed for auth recovery, not only reigister.

Traceback (most recent call last):

  File "/usr/local/pythonenv/onlineweb4/lib/python2.6/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, _callback_args, *_callback_kwargs)

  File "/var/websites/onlineweb4/apps/authentication/views.py", line 183, in recover
    send_mail(_(u'Gjenoppretning av passord'), email_message, settings.DEFAULT_FROM_EMAIL, [email.email,])

  File "/usr/local/pythonenv/onlineweb4/lib/python2.6/site-packages/django/core/mail/**init**.py", line 50, in send_mail
    connection=connection).send()

  File "/usr/local/pythonenv/onlineweb4/lib/python2.6/site-packages/django/core/mail/message.py", line 255, in send
    return self.get_connection(fail_silently).send_messages([self])

  File "/usr/local/pythonenv/onlineweb4/lib/python2.6/site-packages/django/core/mail/backends/smtp.py", line 95, in send_messages
    sent = self._send(message)

  File "/usr/local/pythonenv/onlineweb4/lib/python2.6/site-packages/django/core/mail/backends/smtp.py", line 113, in _send
    force_bytes(message.as_string(), charset))

  File "/usr/lib/python2.6/smtplib.py", line 709, in sendmail
    raise SMTPRecipientsRefused(senderrs)

SMTPRecipientsRefused: {u'raisanen@online.ntnu.no': (550, '5.1.1 raisanen@online.ntnu.no: Recipient address rejected: User unknown in local recipient table')}


Resolving patch:
diff --git a/apps/authentication/views.py b/apps/authentication/views.py
--- a/apps/authentication/views.py
+++ b/apps/authentication/views.py
@@ -96,8 +96,8 @@ def register(request):
 """) % (request.META['HTTP_HOST'], token)
                 try:
                     send_mail(_(u'Verifiser din konto'), email_message, settings.DEFAULT_FROM_EMAIL, [email.email,])
-                except SMTPException as e:
-                    messages.error(request, u'Det oppstod en kritisk feil, ' + e)
+                except SMTPException:
+                    messages.error(request, u'Det oppstod en kritisk feil, epostadressen er ugyldig!')
                     return redirect('home')
 
                 messages.success(request, _(u'Registreringen var vellykket. Se tilsendt epost for verifiseringsinstrukser.'))
diff --git a/apps/profiles/views.py b/apps/profiles/views.py
--- a/apps/profiles/views.py
+++ b/apps/profiles/views.py
@@ -338,7 +338,12 @@ def _send_verification_mail(request, email):
 kan dette gjøres ved å klikke på knappen for verifisering på din profil.
 """) % (request.META['HTTP_HOST'], token)
 
-    send_mail(_(u'Verifiser din epost %s') % email, email_message, settings.DEFAULT_FROM_EMAIL, [email,])
+    try:
+        send_mail(_(u'Verifiser din epost %s') % email, email_message, settings.DEFAULT_FROM_EMAIL, [email,])
+    except SMTPException:
+        messages.error(request, u'Det oppstod en kritisk feil, epostadressen er ugyldig!')
+        return redirect('home')
+
 
 @login_required
 def save_membership_details(request):


Test patch: