Create email.py
Browse files
email.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import secrets
|
3 |
+
from mailjet_rest import Client
|
4 |
+
|
5 |
+
# Load Mailjet API keys from environment variables
|
6 |
+
MAILJET_API_KEY = os.environ.get("MAILJET_API_KEY")
|
7 |
+
MAILJET_API_SECRET = os.environ.get("MAILJET_API_SECRET")
|
8 |
+
|
9 |
+
# Create a Mailjet client
|
10 |
+
mailjet = Client(auth=(MAILJET_API_KEY, MAILJET_API_SECRET))
|
11 |
+
|
12 |
+
def send_verification_email(to_email, verification_token):
|
13 |
+
# Create the email message
|
14 |
+
email_data = {
|
15 |
+
'Messages': [
|
16 |
+
{
|
17 |
+
"From": {
|
18 |
+
"Email": "[email protected]",
|
19 |
+
"Name": "Your Name"
|
20 |
+
},
|
21 |
+
"To": [
|
22 |
+
{
|
23 |
+
"Email": to_email,
|
24 |
+
"Name": "User Name"
|
25 |
+
}
|
26 |
+
],
|
27 |
+
"Subject": "Verify Your Email",
|
28 |
+
"HTMLPart": f'Click <a href="https://yourapp.com/verify/{verification_token}">here</a> to verify your email.'
|
29 |
+
}
|
30 |
+
]
|
31 |
+
}
|
32 |
+
|
33 |
+
# Send the email
|
34 |
+
try:
|
35 |
+
response = mailjet.send.create(data=email_data)
|
36 |
+
if response.status_code == 200:
|
37 |
+
print("Verification email sent successfully.")
|
38 |
+
else:
|
39 |
+
print("Failed to send verification email.")
|
40 |
+
except Exception as e:
|
41 |
+
print(str(e))
|
42 |
+
|
43 |
+
def generate_verification_token(email):
|
44 |
+
# Generate a random token
|
45 |
+
token = secrets.token_urlsafe(32)
|
46 |
+
|
47 |
+
# You can associate this token with the provided email address in your database
|
48 |
+
# For example, store it in a verification_tokens table with an expiration date
|
49 |
+
|
50 |
+
return token
|