-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_emails.py
139 lines (118 loc) · 5.06 KB
/
send_emails.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Functions sending emails to users."""
import traceback
from django.core.mail import send_mail
from django.conf import settings
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.template.loader import render_to_string
from django_logger import logger
def get_protocol(request):
"""Return the HTTP or HTTPS based on
the protocol chosen in the request."""
if request.is_secure():
return 'https'
return 'http'
def send_welcome_email(user):
"""Send welcome email to new users."""
email_template = "emails/welcome_email.html"
subject = "Welcome to DelfiTLM!"
message = render_to_string(email_template, {
'username': user.username,
})
try:
send_mail(
subject=subject,
message=message,
from_email = None,
recipient_list = [user.email],
fail_silently=False,
)
except Exception as _: #pylint: disable=broad-except
logger.error("Email backend: %s", str(settings.EMAIL_BACKEND))
logger.error("Email host: %s", str(settings.EMAIL_HOST))
logger.error("Email port: %s", str(settings.EMAIL_PORT))
logger.error("Send_email failure:\n%s", traceback.format_exc())
def send_confirm_account_deleted_email(user):
"""Send a confirmation that the user account has been deleted."""
email_template = "emails/deleted_account_confirmation_email.html"
subject = "Account deletion"
message = render_to_string(email_template, {
'username': user.username,
})
try:
send_mail(
subject=subject,
message=message,
from_email = None,
recipient_list = [user.email],
fail_silently=False,
)
except Exception as _: #pylint: disable=broad-except
logger.error("Email backend: %s", str(settings.EMAIL_BACKEND))
logger.error("Email host: %s", str(settings.EMAIL_HOST))
logger.error("Email port: %s", str(settings.EMAIL_PORT))
logger.error("Send_email failure:\n%s", traceback.format_exc())
def _send_email_with_token(request, user, email_template, subject, email=None):
"""Helper method sending an email containing a verification token.
User for email verification and password reset."""
protocol = get_protocol(request)
current_site = get_current_site(request)
if email is None:
email = user.email
message = render_to_string(email_template, {
'user': user,
'email': email,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': default_token_generator.make_token(user),
'protocol': protocol
})
try:
send_mail(
subject=subject,
message=message,
from_email = None,
recipient_list = [email],
fail_silently=False,
)
except Exception as _: #pylint: disable=broad-except
logger.error("Email backend: %s", str(settings.EMAIL_BACKEND))
logger.error("Email host: %s", str(settings.EMAIL_HOST))
logger.error("Email port: %s", str(settings.EMAIL_PORT))
logger.error("Send_email failure:\n%s", traceback.format_exc())
def send_email_verification_registration(request, user):
"""Sends an email to verify the email address of a newly registered user."""
email_template = "emails/register_email_verification.html"
subject = "Verify your email"
_send_email_with_token(request, user, email_template, subject)
def send_email_change_request_confirmation(user):
"""Sends an email the current email address to confirm it should be changed."""
email_template = "emails/confirm_email_address_change.html"
subject = "Email address change requested"
try:
send_mail(
subject=subject,
message=render_to_string(email_template, {
'email': user.email
}),
from_email = None,
recipient_list = [user.email],
fail_silently=False,
)
except Exception as _: #pylint: disable=broad-except
logger.error("Email backend: %s", str(settings.EMAIL_BACKEND))
logger.error("Email host: %s", str(settings.EMAIL_HOST))
logger.error("Email port: %s", str(settings.EMAIL_PORT))
logger.error("Send_email failure:\n%s", traceback.format_exc())
def send_new_email_verification(request, user):
"""Upon email update request, sends an email the new email address to verify it."""
email_template = "emails/verify_new_email.html"
subject = "Verify your new email"
_send_email_with_token(request, user, email_template, subject, email=user.new_email)
def send_password_reset_email(request, user):
"""Sends a password reset email with a link to the reset form."""
email_template = "emails/password_reset_email.html"
subject = "Password Reset Requested"
_send_email_with_token(request, user, email_template, subject)