Preliminary discussion on Django thread sending mail

Source: Internet
Author: User

Recently has been tangled up in a mail-sending issue.

Under the local Linux build program, do not fill in the email settings can successfully send mail, the remote cloud server under the Linux environment is not sent. It's strange that a program built under Windows can't send registered mail.

The initial judgment is that there is no problem with the code, and the local Linux server is not necessarily consistent with the cloud server because it has been tinkering with other things before. Then only from the official documents to send the message to start, slowly find the reason.

 from Import Send_mail send_mail (u' mail header ', u' email content ' [email protected] ' ,    ['[email protected]'], Fail_silently=false)

In the test environment using the above configuration, you can send mail, it seems to have to turn over the source code to find the problem.

The original author uses the following method to send the message:

#!/usr/bin/env python#-*-coding:utf-8-*- fromDjango.core.mailImportEmailmessageImportThreadingclassEmailthread (Threading. Thread):"""send an account to activate the mail thread"""    def __init__(self,subject="', body="', From_email=none, to=None): Self.subject=subject Self.body=Body Self.from_email=From_email self.to=To self.fail_silently=True Threading. Thread.__init__(self)defRun (self): Msg_email=emailmessage (self.subject,self.body,self.from_email,self.to) Msg_email.content_subtype='HTML'        Try: Msg_email.send (self.fail_silently)exceptexception,e:#Logging error LogsLog = open ('Email_error.log','a') Log.write ('%s%s\n'% (Time.strftime ('%y-%m-%d%h:%m:%s'), E)) Log.close ()defSend_email (subject="', body="', From_email=none, to=[]):    """Send Message Method"""Email=Emailthread (Subject, body, from_email, to) Email.start () Email.join ()

Basically understand the meaning of the code, but after a few times to minimize the test, still can not send a successful registration mail. The simplest way to send mail is through the top, but the author uses threads, perhaps due to efficiency and production environment considerations. So I went online and looked for an example of a Django thread sending mail, and I really found it. is the code without indentation ... Is....

 fromDjango.core.mailImportemailmultialternatives fromDjango.templateImportLoader fromSettingsImportEmail_host_user fromDjango.httpImportHttpResponseImportThreadingclassMy_emailthread (Threading. Thread):def __init__(self, subject, body, From_email, recipient_list, fail_silently, html): Self.subject=subject Self.body=Body Self.recipient_list=recipient_list Self.from_email=From_email self.fail_silently=fail_silently self.html=HTML Threading. Thread.__init__(self)defRun (self): Msg=emailmultialternatives (Self.subject, Self.body, Self.from_email, self.recipient_list)ifself.html:msg.attach_alternative (self.body,self.html) msg.send (self.fail_silently)defMy_send_mail (Subject, body, from_email, to, Fail_silently=false, Html=none, *args, * *Kwargs): My_emailthread (Subject, body, from_email, to, fail_silently, HTML). Start ()defTest_mail (subject="', body="', From_email=none, to=[]):    #subject = U ' mail subject '    #to_mail_list = [' [email protected] ', ' [email protected] '    #BODY = loader.render_to_string (' mail_template.html ', {' email ': email, ' Date ':d atetime.today (),})    #Send_mail (subject,body,from_email,to_mail_list,html= "text/html")    #my_send_mail (U ' mail header ', u ' email content ', ' [email protected] ', [' [email protected] '],html= ' text/html ')My_send_mail (Subject,body,from_email,to, html="text/html")

Because, for example, I directly add the above code to the project. Add My_ in front of Send_mail and Emailthread in order to co-exist with the two-segment code, it is still safe to keep the original code intact until the entire project is operational.

After testing, directly into the parameters, send mail no problem. However, using the variable to apply the user information fails to send.

Then, find the upper view, control the variables to test, and see which variable is not replaced correctly. I've found a problem.

to = [User.username]     .... # Send_email (subject,body,from_email,to)Test_mail (subject,body,from_email,to)

Test_mail is called by the new thread that sent the message, Send_mail the original thread of the call was sent. In either case, the message cannot be sent because the to is not given the correct email address.

Send_email (U' mail header ', u' email content '[email protected]  ', ['[email protected]']

Such a display call, the original thread function processing method also worked correctly, it appears that the above two threads sent messages are available. The problem occurs on the variable.

So what is this user.username , continue to look for ... Is the user object that is used to introduce users

 from Import User

It appears that the problem is not on the message delivery method, but on the user object. As for why local Linux is not configured, it can be used directly ... (Ask the author, the author thinks it might be a Django-brought mail service, for the time being to put down this weird problem)

Starting tomorrow to understand the user side of Django, I feel that different platforms are unstable because the code is not strong enough ... See if there's a new discovery ~

Preliminary discussion on Django thread sending mail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.