6

Can someone explain how to set up email verification for django rest auth ? My settings.py contains:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True   
ACCOUNT_USERNAME_REQUIRED = False

And I've added this line in my urls.py:

url(r'^accounts/', include('allauth.urls')),

But when I register with this endpoint: rest-auth/registration/, the email is not sent, but the user is created. What is the good configuration to send email confirmation? Do I have to have a SMTP server?

EDIT: (after @McAbra comment)

Obviously, this setting cannot send email:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

It only print the mail in the django console output. The settings given by @McAbra works well. But in a production environment, is it a good practice to send verification email with gmail? Have I just to create email such noreply.myapp@gmail.com?

EDIT 2:

My other problem is that the sent mail is empty. The template are presents in site-packages\allauth\templates\account\email. The email title is [http://localhost:8000/] but there is no content. I have no error in the python console. Any idea?

Solution for this point:

site-packages\allauth\templates\account\email files must be copied in your-app\templates\account\email

Ben
  • 3,972
  • 8
  • 43
  • 82
  • Just to make sure: you are not expecting a real email, right? If you are using the `EMAIL_BACKEND` you specified you'll get an "email" printed out in your console. – McAbra Oct 11 '16 at 18:26
  • I would like to set up a registration with verification by email. I'm looking for a solution to set my configuration to send a validation email to the user then proceed to the validation to activate the user account. – Ben Oct 11 '16 at 18:31
  • Ok, so you do get an email printed in your console but you want to send actual emails into a real mailbox? – McAbra Oct 11 '16 at 18:38
  • Yes that's it. But I've just checked, I don't have anything printed in my console. I will update my post. – Ben Oct 11 '16 at 18:40
  • Oh... and I think it's Django allauth not "rest auth". – McAbra Oct 11 '16 at 18:51
  • That solution is not the case - the template was most likely not accessible from it's original directory as you did not include `allauth` in your `INSTALLED_APPS`. – McAbra Oct 12 '16 at 16:34
  • I just checked, allauth is in my INSTALLED_APPS. – Ben Oct 12 '16 at 16:41

2 Answers2

3

You need to have a SMTP server if you want to send real emails. I like to use Gmail SMTP server. settings.py would be:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'gmail_username'
EMAIL_HOST_PASSWORD = 'password from https://security.google.com/settings/security/apppasswords'

Try from there. I'll try to help further if that doesn't help.

McAbra
  • 2,382
  • 2
  • 21
  • 29
  • Your solution works, but I had to change EMAIL_HOST_PASSWORD by simply the gmail account password. This url: `https://security.google.com/settings/security/apppasswords` return `"The setting you are looking for is not available for your account."` – Ben Oct 12 '16 at 11:56
  • And I have edited my post with a short question, maybe you can help me on this point too? :) – Ben Oct 12 '16 at 11:57
  • Is there any reason why it works only on localhost and not in my server (with the same settings)? – Ben Oct 13 '16 at 19:37
3

I had problems with sending emails and after that with the link in the email. You need to explicitly set the account confirm email view.

Below are my settings in settings.py

 ACCOUNT_AUTHENTICATION_METHOD = 'email'
 ACCOUNT_EMAIL_REQUIRED = True   
 ACCOUNT_USERNAME_REQUIRED = False
 ACCOUNT_EMAIL_VERIFICATION = 'mandatory'

in urls.py

  from allauth.account.views import confirm_email as allauthemailconfirmation

  urlpatterns  = [
  url(r'^api/rest-auth/account-confirm-email/(?P<key>[-:\w]+)/$',allauthemailconfirmation,
    name='account_confirm_email'),
  ]

You can use mailcatcher.me if you want to check emails locally.

sarc360
  • 349
  • 3
  • 5