Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
How to send Email without using settings.py ? (smtp parameters in database)

I would like to be able to send emails with django but without using the email parameters in settings.py. (EMAIL_HOST, EMAIL_USE_TLS, EMAIL_HOST_PASSWORD, etc ...)

These parameters are stored in db because they can be different depending on the user. How to use these parameters in base to send emails and not those in settings.py ?

class EmailThread(threading.Thread):
    """
    Class email (Thread)
    """
    def __init__(self, subject, html_content, recipient_list):
        self.subject = subject
        self.recipient_list = recipient_list
        self.html_content = html_content
        threading.Thread.__init__(self)

    def run (self):
        msg = EmailMessage(self.subject,
                           self.html_content,
                           settings.EMAIL_HOST_USER,
                           self.recipient_list)
        msg.content_subtype = "html"
        msg.send()

def send_html_mail(subject, html_content, recipient_list):
    """
    send an email asynchronous
    """
    EmailThread(subject, html_content, recipient_list).start()

I can get the parameters using: email_params = EmailParameter.objects.get(user=request.user)

class EmailParameter(models.Model):
    email_use_tls = models.BooleanField(_("email use tls"), default=True)
    email_use_ssl = models.BooleanField(_("email use tls"), default=False)
    email_host = models.URLField(_("email host"), max_length=200)
    email_host_user = models.CharField(_("email host user"), max_length=200)
    email_host_password = models.CharField(_("email host password"), max_length=200)
    email_port = models.PositiveIntegerField(_("email port"))
    default_from_email = models.EmailField(_("default from email"), max_length=200)
    signature = models.TextField(_("signature"))
    user = models.ForeignKey(
        User,
        verbose_name = _("user"),
        related_name = "user_email_parameter",
        on_delete=models.CASCADE
    ) 
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Just move all necessary parameters inside class instance and use it instead of settings.XXX:

class EmailThread(threading.Thread):
    def __init__(self, subject, html_content, recipient_list, email_host_user):
        self.subject = subject
        self.recipient_list = recipient_list
        self.html_content = html_content
        # set parameter to instance
        self.email_host_user = email_host_user
        threading.Thread.__init__(self)

    def run (self):
        msg = EmailMessage(self.subject,
                           self.html_content,
                           # use instance parameter
                           self.email_host_user,
                           self.recipient_list)
        msg.content_subtype = "html"
        msg.send()

# add email_host_user to be able set it when call mail send process
def send_html_mail(subject, html_content, recipient_list, email_host_user):
    EmailThread(subject, html_content, recipient_list, email_host_user).start()

Now you able to send mail with custom parameters:

e_h_u = EmailParameter.objects.get(user=request.user).email_host_user
send_html_mail(subject, html_content, recipient_list, e_h_u)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!