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.


Resolving patch:
diff --git a/apps/authentication/views.py b/apps/authentication/views.py
--- a/apps/authentication/views.py
+++ b/apps/authentication/views.py
@@ -2,6 +2,7 @@
 
 import uuid
 import re
+from smtplib import SMTPException
 
 from django.contrib import auth
 from django.contrib import messages
@@ -93,8 +94,11 @@ def register(request):
 Denne lenken vil være gyldig i 24 timer. Dersom du behøver å få tilsendt en ny lenke
 kan dette gjøres med funksjonen for å gjenopprette passord.
 """) % (request.META['HTTP_HOST'], token)
-
-                send_mail(_(u'Verifiser din konto'), email_message, settings.DEFAULT_FROM_EMAIL, [email.email,])
+                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)
+                    return redirect('home')
 
                 messages.success(request, _(u'Registreringen var vellykket. Se tilsendt epost for verifiseringsinstrukser.'))
 


Test patch: