send activate-email with django-registration
I am curious if there's a way t开发者_如何学Pythono send activated email with username, password by using django-registration. First I thought about modifying registration form but I need some example.
django-registration uses the following code, internally, to handle sending emails:
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email])
If you want to work, you will have to specify the value DEFAULT_FROM_EMAIL in your settings.py.
Also, note the following:
Mail is sent using the SMTP host and port specified in the EMAIL_HOST and EMAIL_PORT settings. The EMAIL_HOST_USER and EMAIL_HOST_PASSWORD settings, if set, are used to authenticate to the SMTP server, and the EMAIL_USE_TLS setting controls whether a secure connection is used.
So, to give an example, here's what I've used in a settings.py file to use a gmail account:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'my@gmail.com'
EMAIL_HOST_PASSWORD = 'my_emails_password'
django-registration should then be able to send emails.
EMAIL_PORT = 465 was working some year ago. Now you need to use port 587 with gmail. Reason: Django does not support SMTP with SSL from the beginning. Only STARTTLS command is supported AFTER plain-text connection is set up. Gmail, however, no longer supports this option on port 465.
Hope this will help someone. After troubleshooting for half an hour, I realised that I had to explicitly state this as well while using django-registration -
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
django-register-activate implemented a similar functionality as django-registration. Basically it provides code for user sign up, email verification and activation, user login and user logout.
If you need examples to implement your own code, you may check the views.py and forms.py in register_activate to get some hints.
Hope the answer helps. Let me know if you have further questions.
精彩评论