1

I want to send verification email from different email per tenant but unable to override this. How can i set following parameter dynamically per tenant in setting.py file

EMAIL_BACKEND 
EMAIL_HOST 
EMAIL_USE_TLS 
EMAIL_PORT
EMAIL_HOST_USER
EMAIL_HOST_PASSWORD

ANYMAIL = {
"MAILJET_API_KEY":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"MAILJET_SECRET_KEY": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
}
EMAIL_BACKEND = "anymail.backends.xxx.EmailBackend"
DEFAULT_FROM_EMAIL = 'dynamic@gmail.com'
Deven
  • 11
  • 1
  • 3
  • I see you asked 10 months ago, if you want get clear answer then, be more specific about your question, like what packages you are using and what are you trying to accomplish step by step, and what did you do and where did you fail. just an advice :) – Ahmed Shehab Jan 27 '22 at 13:57

2 Answers2

0

I don't think it is possible in django ways if you are using multi-tenant package as it is just one app with different schemas, because django loads settings first and then other models follow. but just a gist of idea to work around this is to use environment variables like;

import os

DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL', 'dynamic@gmail.com')

this change still takes place once django reloaded.

dirty force reload settings like;

import django
import importlib
django.setup()
importlib.reload(importlib.import_module('yourappname.settings'))

however it will be global changes applied

Yet another advice, is to override the settings before your action inside your code. get the current tenant off the request and override settings manually for this action. check how to patch settings here How to Unit test with different settings in Django?

Keep trying it is not impossible but just not djangoish clean style.

more resources

https://github.com/edavis/django-override-settings https://www.djangosnippets.org/snippets/2437/

✅ UPDATE

you can handle it softly in a middleware and keep your code clean, check this answer Django multitenant: how to customize django setting "ACCOUNT_EMAIL_VERIFICATION" per tenant?

Hope it helps

Ahmed Shehab
  • 1,657
  • 15
  • 24
0

You can change environment settings dynamically:

import os

os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"

And another way is to create a function (for example in a class) that gets the parameter like tenant id; and sets or returns the email setting you need.

TechView
  • 3
  • 3