How to set SMTP for customer in production?

323 views Asked by At

What is the best practice for setting customer's SMTP in Django?

Can I make them to set itself? I can't do that inside settings, otherwise they would have to tell me their password.

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '****@gmail.com'
EMAIL_HOST_PASSWORD = '*****'

Is there a way? Maybe allow them to set credentials through Django admin?

EDIT:

For now, the best way looks like to create a singleton object, let's call it config, to store credentials and register it in admin page to allow admins to set their credentials. Then, create one class for sending messages and do something like that:

backend = EmailBackend(host=config.host, port=config.port, username=config.username, 
                           password=config.password, use_tls=config.use_tls, fail_silently=config.fail_silently)
email = EmailMessage(subject='subj', body='body', from_email=from_email, to=to, 
             connection=backend)

like here https://stackoverflow.com/a/22287776/3371056

But maybe there is some pattern to do such thing differently.

1

There are 1 answers

0
Ykh On

if you want save it in database like a config,django-constance is recommended,the advantage is you can edit and only can edit(no add\delete interface) it by admin.

here is the code you get conf in views.py by django-constance if you use it:

from constance import LazyConfig
config = LazyConfig()


def put_value(key, value):
    setattr(config, key, value)


def get_value(key):
    return getattr(config, key)