Django: Set E-Mail settings in view

Django: Set E-Mail settings in view

django send_mail
django send email gmail
sendgrid django
django admin send email to users
django receive email
django email template
django custom email backend
send email in django rest framework

I am trying to send a email using SMTP from Google. I already get to send a email putting the setting variables in the file "" but I need to configure these variables in a view and set them using my "Config" model.

This is my model code:

class Config(models.Model):
    email_sender    = models.CharField(max_length = 100, default = '', blank = True)
    email_password  = models.CharField(max_length = 30, default = '', blank = True)
    smtp_server     = models.CharField(max_length = 100, default = '', blank = True)
    smtp_port       = models.PositiveIntegerField(default = 587, blank = True)

This is my view code:

def send_custom_mail(request):
    config = Config.objects.get(pk = 1)
    EMAIL_HOST = config.smtp_server
    EMAIL_PORT = config.smtp_port
    EMAIL_HOST_USER = config.email_sender
    EMAIL_HOST_PASSWORD = config.email_password
    EMAIL_USE_TLS = True
    subject = 'test'
    msg = '<p>Test: <strong>Mensaje</strong>></p>'
    mail_from = config.email_sender
    mail_to = ['{}'.format(email)]
    from django.core.mail import EmailMessage
    email2send  = EmailMessage(subject, msg, mail_from, to=mail_to)
    email2send.content_subtype = "html"  # Main content is now text/html

My problem: The email is not sent when I set the variables in the view.

I need to configure these variables dinamically so I can't write it in the file.

I'm a bit puzzled about having to override settings in a view; never seen a use case for it before.

Strategy I'd normally use would be to fall-back either to lower-level functions.

I'd suggest that you could achieve what you seem to need to by using the send_mail() function and get_connection().

Haven't tested this code, but think it would look like this:

from django.core import mail

connection = mail.get_connection(
    host = config.smtp_server,
    port = config.smtp_port,
    username = config.email_sender,
    password = config.email_password ,
    (... etc etc ...)

# Manually open the connection

# Send the message
subject = 'test'
msg = 'Test:Mensaje'
mail_to = ['']
email2send  = EmailMessage(
    msg, t

# Send the email using the custom connection

# Close the connection

(Note the connection=connection when the EmailMessage is first setup to send the email via the custom connection.)

Django Email Backends docs explain this pretty well, and this seems a more straight-forward way to do it than overriding settings.

Sending email | Django documentation, Learn to send Emails in Django by understanding its settings, There are some more settings for email but for sending an email with Gmail, this much setup is enough. We are importing that value in the view function. Django Mail Setup. Sending email using Django is pretty easy and require less configuration. In this tutorial, we will send email to provided email. For this purpose, we will use Google's SMTP and a Gmail account to set sender. Django provides built-in mail library django.core.mail to send email.

I found this solution, but I don't know if it is the best way to aproach this problem:

using this:

In my view I overrride the setting variables:

from django.core.mail import EmailMessage
from django.test.utils import override_settings

@override_settings(EMAIL_HOST = Config.objects.get(pk = 1).smtp_server)
@override_settings(EMAIL_HOST_USER = Config.objects.get(pk = 1).email_sender)
@override_settings(EMAIL_PORT = Config.objects.get(pk = 1).smtp_port)
def send_custom_mail(request):
subject = 'test'
    msg = 'Test:Mensaje'
    mail_to = ['']
    email2send  = EmailMessage(subject, msg, to=mail_to)
    email2send.content_subtype = "html"  # Main content is now text/html

in this aproach I use "django.test.utils" to put the decorator "@override_settings()" before the "send_custom_mail" function. It override the setting and let me send emails from a mail saved in a model called Config.

Actually, I'm not sure if it is the best aproach because the "django.test" module is meant to use in unit tests, but I wanted to share my solution.

How to Send Emails in Django, Setting up. Before actually send your email, your need to set up for it. So, let's add some lines to the file of your Django app. Sending email¶. Although Python provides a mail sending interface via the smtplib module, Django provides a couple of light wrappers over it. These wrappers are provided to make sending email extra quick, to help test email sending during development, and to provide support for platforms that can’t use SMTP.

def email_sending(mail_subject, message,to, mail_obj=None):
if not mail_obj:

        mail_obj = AdminEMailsModel.objects.filter(default_email=True)[0]

    except Exception as e:
            if not mail_obj:
                mail_obj= AdminEMailsModel.objects.all()[0]

        except Exception as e:
backend = EmailBackend(host=mail_obj.host_address, port=mail_obj.port_no,,
                       password=mail_obj.password, use_tls=mail_obj.use_tls, fail_silently=False)
email = EmailMultiAlternatives(
    mail_subject, message,, to=to, connection=backend

email.attach_alternative(message, 'text/html')


directly you can call this function whenever you need to send an email to register email if this code is usefull to you please give up vote

Send and Receive Emails in Django, the settings above are used by django to make sending emails through your email host server possible. gmail account with allow less secure app feature enabled and put In your view file your functions will look like this Since we are sending email in this view, we must import the send_mail() function from django.core.mail We then define our function-based view, contactview We create 3 variables, name, email, and comment, which will eventually contain the values of the form fields that the user enters in.

How to send emails with python django through google SMTP , Configuring Django to send emails. To configure you Django App, add the following parameters to your : EMAIL_HOST =� These lines are enclosed in the django.core.mail module that is based on smtplib. The message delivery is carried out via SMTP host, and all the settings are set by default: EMAIL_HOST: 'localhost' EMAIL_PORT: 25 EMAIL_HOST_USER: (Empty string) EMAIL_HOST_PASSWORD: (Empty string) EMAIL_USE_TLS: False EMAIL_USE_SSL: False

How to Send Email in a Django App, Inside the file, we configure the templates directory. At the end of the file, we provide the email configuration settings. These can be� Django enables the user to send an e-mail by using its all time ready and easy to use light engine. To make this feature happen, you are required to import smtplib as done in the case of Python. Now, to be able to send an e-mail, you need to make a few changes in your file where you will be setting the below provided options –

Django email tutorial - how to send emails in Django, To start sending e-mail, edit your project file and set the following options − Let's create a "sendSimpleEmail" view to send a simple e-mail. Django comes with a ready and easy-to-use light engine to send e-mail. Similar to Python you just need an import of smtplib. In Django you just need to import django.core.mail. To start sending e-mail, edit your project file and set the following options