1
|
|
|
import threading |
2
|
|
|
|
3
|
|
|
from django.conf import settings |
4
|
|
|
from django.core.mail import EmailMultiAlternatives |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
def send_email_to_list(subject, body, users_list, text_body, |
8
|
|
|
bcc_admins=True, bcc_managers=False): |
9
|
|
|
bcc = [] |
10
|
|
|
if bcc_admins: |
11
|
|
|
bcc += [email for name, email in settings.ADMINS] # @UnusedVariable |
12
|
|
|
|
13
|
|
|
if bcc_managers: |
14
|
|
|
bcc += [email for name, email in settings.MANAGERS] # @UnusedVariable |
15
|
|
|
from_user = 'PythonExpress <[email protected]>' |
16
|
|
|
email = EmailMultiAlternatives( |
17
|
|
|
subject, text_body, from_user, users_list, bcc) |
18
|
|
|
email.attach_alternative(body, "text/html") |
19
|
|
|
EmailThread(email).start() |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def send_email_to_id(subject, body, email_id, text_body, |
23
|
|
|
bcc_admins=True, bcc_managers=False): |
24
|
|
|
bcc = [] |
25
|
|
|
if bcc_admins: |
26
|
|
|
bcc += [email for name, email in settings.ADMINS] # @UnusedVariable |
27
|
|
|
|
28
|
|
|
if bcc_managers: |
29
|
|
|
bcc += [email for name, email in settings.MANAGERS] # @UnusedVariable |
30
|
|
|
|
31
|
|
|
from_user = 'PythonExpress <[email protected]>' |
32
|
|
|
email = EmailMultiAlternatives( |
33
|
|
|
subject, text_body, from_user, [email_id], bcc=bcc) |
34
|
|
|
email.attach_alternative(body, "text/html") |
35
|
|
|
EmailThread(email).start() |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
class EmailThread(threading.Thread): |
39
|
|
|
|
40
|
|
|
def __init__(self, email): |
41
|
|
|
self.email = email |
42
|
|
|
threading.Thread.__init__(self) |
43
|
|
|
|
44
|
|
|
def run(self): |
45
|
|
|
try: |
46
|
|
|
self.email.send() |
47
|
|
|
except Exception as e: |
48
|
|
|
print(e) |
49
|
|
|
|