Django dynamic mail tuning

I do not want to use the email settings fields in the settings.py file, I want to put them in the model.

class Configuration(models.Model): email_use_tls = models.BooleanField(_(u'EMAIL_USE_TLS'),default=True) email_host = models.CharField(_(u'EMAIL_HOST'),max_length=1024) email_host_user = models.CharField(_(u'EMAIL_HOST_USER'),max_length=255) email_host_password = models.CharField(_(u'EMAIL_HOST_PASSWORD'),max_length=255) email_port = models.PositiveSmallIntegerField(_(u'EMAIL_PORT'),default=587) .... 

What is the best practice for customizing the behavior of django.core.mail.send_mail? Should I copy send_mail code to my project? This is not what I want.

+6
source share
3 answers

Very interesting question. It looks like this is already implemented in the EmailMessage class.

First you need to set up an email backend

 from django.core.mail import EmailMessage from django.core.mail.backends.smtp import EmailBackend config = Configuration.objects.get(**lookup_kwargs) backend = EmailBackend(host=config.host, port=congig.port, username=config.username, password=config.password, use_tls=config.use_tls, fail_silently=config.fail_silently) 

Then just pass the connection to EmailMessage

 email = EmailMessage(subject='subj', body='body', from_email=from_email, to=to, connection=backend) 

Then send an email :)

 email.send() 

Ofc if you want html or attachment files to use EmailMultiAlternatives instead

+10
source

The answer above has a few errors (answer by Andrei Nelobin). No need to call get_connection(backend=backend) . You must pass the backend to the EmailMessage constructor, for example:

 backend = EmailBackend(host=config.host, port=congig.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) 

I can not add a comment that will respond and attach a new one to the publication. Please someone who has permissions, move it there or correct the answer.

+2
source

I have used this method for several years and love it. I just published a package ( django-des ) to accomplish this using the methods in other answers here.

This package sets up the model ( DynamicEmailConfiguration ) and uses django-solo to give you a pleasant editing experience in the Django admin. Then it provides an email backend that you can use that will pull the settings from this model in the same way as Andrei Nelubin recommended. It also gives you a small small test email in the upper right corner of the Django admin panel.

To install it:

  • Set dynamic Django email settings:

     $ pip install django-des 
  • Add it to your `INSTALLED_APPS`:

     INSTALLED_APPS = ( ... 'django_des', ... ) 
  • Add a dynamic email address for email in settings.py

     EMAIL_BACKEND = 'django_des.backends.ConfiguredEmailBackend' 
  • To enable test email support, add Django DES URL patterns:

     from django_des import urls as django_des_urls urlpatterns = [ ... url(r'^django-des/', include(django_des_urls)), ] 

Now you can visit 127.0.0.1:8000/admin/django_des/dynamicemailconfiguration/ and configure your email settings. You can also send a test letter.

Once all this is done, you can usually use send_mail .

0
source

All Articles