82 lines
3.5 KiB
Python
82 lines
3.5 KiB
Python
from djoser import email
|
|
from django.conf import settings
|
|
from django.core.mail import EmailMultiAlternatives
|
|
|
|
class CustomPasswordResetEmail(email.PasswordResetEmail):
|
|
|
|
template_name = None
|
|
|
|
def render(self, context=None):
|
|
|
|
context = self.get_context_data()
|
|
url = context['url']
|
|
user = context['user']
|
|
|
|
self.html_content = self.create_email_html(url, user)
|
|
self.plain_content = self.create_email_plain(url, user)
|
|
|
|
def get_context_data(self):
|
|
return super().get_context_data()
|
|
|
|
def get_subject(self):
|
|
"""
|
|
subject ถูกสร้างโดย Djoser ใน context['subject']
|
|
"""
|
|
context = self.get_context_data()
|
|
return context.get("subject", "Password Reset")
|
|
|
|
def send(self, to, *args, **kwargs):
|
|
|
|
self.render()
|
|
|
|
msg = EmailMultiAlternatives(
|
|
subject=self.get_subject(),
|
|
body=self.plain_content,
|
|
from_email=self.from_email,
|
|
to=to
|
|
)
|
|
if self.html_content:
|
|
msg.attach_alternative(self.html_content, "text/html")
|
|
|
|
# djcelery_email จะ Intercep เมธอด send() ของ EmailMultiAlternatives โดยอัตโนมัติ
|
|
return msg.send()
|
|
|
|
def create_email_html(self, url, user):
|
|
# ดึง context data อีกครั้งเพื่อเข้าถึง protocol และ domain
|
|
context = self.get_context_data()
|
|
full_reset_url = f"{context['protocol']}://{context['domain']}{url}" # สร้าง Full URL
|
|
|
|
return f"""
|
|
<!doctype html>
|
|
<html>
|
|
<body>
|
|
<p>สวัสดีคุณ <b>{user.username}</b>,</p>
|
|
<p>คุณได้ร้องขอการรีเซ็ตรหัสผ่าน โปรดคลิกลิงก์ด้านล่างเพื่อตั้งรหัสผ่านใหม่:</p>
|
|
|
|
<p><a href="{full_reset_url}" style="background-color: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;">รีเซ็ตรหัสผ่าน (คลิกที่นี่)</a></p>
|
|
|
|
<p>หากลิงก์ไม่สามารถคลิกได้ กรุณาคัดลอกลิงก์นี้ไปวางในเบราว์เซอร์: <b>{full_reset_url}</b></p>
|
|
<p>หากคุณไม่ได้ร้องขอ โปรดเพิกเฉยต่ออีเมลนี้</p>
|
|
<p>ขอบคุณครับ,<br/>ทีม DDO Console</p>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
def create_email_plain(self, url, user):
|
|
# ดึง context data อีกครั้งเพื่อเข้าถึง protocol และ domain
|
|
context = self.get_context_data()
|
|
full_reset_url = f"{context['protocol']}://{context['domain']}{url}" # ⬅️ สร้าง Full URL
|
|
|
|
return f"""
|
|
สวัสดี {user.username},
|
|
|
|
คุณได้ร้องขอการรีเซ็ตรหัสผ่าน กรุณาใช้ลิงก์ด้านล่างเพื่อตั้งรหัสผ่านใหม่:
|
|
|
|
{full_reset_url}
|
|
|
|
หากคุณไม่ได้ร้องขอ โปรดเพิกเฉยต่ออีเมลนี้
|
|
|
|
ขอบคุณครับ,
|
|
ทีม DDO Console
|
|
"""
|