Django built-in email sending mail

### Django built email to send mail 
#### 1, the relevant parameters in the first set settings.py file 
`` `Python 
STATIC_URL = '/ static /' 
# Set mail domain 
EMAIL_HOST = 'smtp.163.com' 
# Set the port number, digital 
EMAIL_PORT = 25 
# set sender's mail 
EMAIL_HOST_USER = '[email protected]' 
# set the sender authorization code 
EMAIL_HOST_PASSWORD = 'own authorization code' 
# whether to enable a secure link 
EMAIL_USER_TLS = True 

# this configuration above information, Django will automatically read, 
# using the account number and authorization code to login 
# If the login is successful, you can send the message 
`` ` 
#### 2, back views.py file, add the following code 
` `` Python 
# introduced means for transmitting the message 
from the django.core.mail the send_mail Import, send_mass_mail, EmailMultiAlternatives 
from Import Settings django.conf 

the send_mail ( 'on holiday notice Mid', 
        'Mid put three days off '
        '[email protected]', 
        [ '[email protected]'] 
        fail_silently = False) 
        # value of 1: 2 message header value: Value message owner 3: Sender value 4: To 5 values: If it fails, if an error is thrown 
iF RES == 1: 
    return HttpResponse ( 'e-mail sent successfully') 
the else: 
    return HttpResponse ( 'e-mail sending failed') 
`` ` 
#### 3, more letters were sent to several people 
` `` python 
message1 = ( 'Are you free this Sunday,' 
         'Kong Zaidong day tour will see the two children', 
         '[email protected]' 
         [ '[email protected]', '[email protected]']) 

message2 = ( 'Are you free this Sunday?', 
        'Kongzi Dong tour, see the two children, then Japan', 
         '[email protected]' 
         [ '[email protected]' '[email protected] ' 
the else:])
res = send_mass_email((message1, message2))
if res == 2:
   return HttpResponse ( 'multiple messages sent successfully')
   return HttpResponse ( 'failure to send multiple messages') 
`` ` 
#### 4, the use of advanced, multi-threaded asynchronous sending 
* utils add sendEmail.py file 

` `` Python 
Import Threading 
from django.core.mail Import send_mail 
from Import Settings django.conf 


class the SendMail (of the threading.Thread): 
    DEF the __init __ (Self, Subject, text, In Email, fail_silently = False): 
        self.subject = Subject 
        self.text = text 
        self.email = In Email 
        self.fail_silently = fail_silently 
        Threading the init __ .__ .thread (Self) 

    DEF RUN (Self): 
        send_mail ( 
            self.subject, 
            self.text, 
            settings.EMAIL_HOST_USER,
            [self.email],
            fail_silently=self.fail_silently
        )
```

*	调用方法

```python
send_mail = SendMail(subject,text,email)
send_mail.start()
```

Guess you like

Origin www.cnblogs.com/wrxblog/p/10945379.html