|
| 1 | +import smtplib |
| 2 | +import ssl |
| 3 | +from email.mime.multipart import MIMEMultipart |
| 4 | +from email.mime.text import MIMEText |
| 5 | + |
1 | 6 | from django.conf import settings |
2 | | -from django.core.mail import EmailMultiAlternatives |
3 | 7 | from django.utils.crypto import get_random_string |
4 | 8 |
|
5 | 9 | from .models import EmailVerificationCode |
|
8 | 12 | def send_verification_email(email, code): |
9 | 13 | subject = " NextShape - Vérification de votre adresse email" |
10 | 14 | from_email = settings.DEFAULT_FROM_EMAIL |
11 | | - to_email = [email] |
12 | | - |
13 | 15 | text_content = f""" |
14 | 16 | Bonjour, |
15 | 17 |
|
@@ -40,9 +42,18 @@ def send_verification_email(email, code): |
40 | 42 | </html> |
41 | 43 | """ |
42 | 44 |
|
43 | | - msg = EmailMultiAlternatives(subject, text_content, from_email, to_email) |
44 | | - msg.attach_alternative(html_content, "text/html") |
45 | | - msg.send() |
| 45 | + msg = MIMEMultipart("alternative") |
| 46 | + msg["Subject"] = subject |
| 47 | + msg["From"] = from_email |
| 48 | + msg["To"] = email |
| 49 | + msg.attach(MIMEText(text_content, "plain")) |
| 50 | + msg.attach(MIMEText(html_content, "html")) |
| 51 | + |
| 52 | + context = ssl._create_unverified_context() |
| 53 | + with smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT) as server: |
| 54 | + server.starttls(context=context) |
| 55 | + server.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD) |
| 56 | + server.sendmail(from_email, [email], msg.as_string()) |
46 | 57 |
|
47 | 58 |
|
48 | 59 | def generate_and_send_verification_code(email): |
|
0 commit comments