[Answered ]-How to send Email without using settings.py ? (smtp parameters in database)

1👍

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)
👤rzlvmp

Leave a comment