I am trying to make EMAIL_HOST settings configurable within admin and I will create a model with required fields like:
But how can I use those fields in views using send_mail?
If you want to use send_mail
, you'll have to create your own email backend which uses your custom settings and then pass it to send_mail
in the connection
attribute.
This works for me
from django.core.mail import EmailMessage
from django.core.mail.backends.smtp import EmailBackend
config = Configuration.objects.get(**lookup_kwargs)
try:
backend = EmailBackend(
host=config.host,
port=config.port,
password=config.password,
username=config.username,
use_tls=config.use_tls,
fail_silently=config.fail_silently
)
mail = EmailMessage(
subject="subject",
body="body",
from_email=config.username,
to=["[email protected]"],
connection=backend,
)
mail.send()
except Exception as err:
print(err)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With