Loginauth / emailx.py
Gregniuki's picture
Update emailx.py
fe5eb9d
raw
history blame
1.57 kB
#emailx.py
import os
import secrets
from mailjet_rest import Client
# Load Mailjet API keys from environment variables
MAILJET_API_KEY = os.environ['MAILJET_API_KEY']
MAILJET_API_SECRET = os.environ['MAILJET_API_SECRET']
# Create a Mailjet client
mailjet = Client(auth=(MAILJET_API_KEY, MAILJET_API_SECRET), version='v3.1')
def send_verification_email(to_email, reset_link):
# Create the email message
email_data = {
'Messages': [
{
"From": {
"Email": "[email protected]",
"Name": "Your Name"
},
"To": [
{
"Email": to_email,
"Name": "User Name"
}
],
"Subject": "Reset or verifie",
"HTMLPart": f'Please click <a href="{reset_link}">here</a> to reset password or verifie your email.'
}
]
}
# Send the email
try:
response = mailjet.send.create(data=email_data)
if response.status_code == 200:
print("Password reset email sent successfully.")
else:
print("Failed to send password reset email.")
except Exception as e:
print(str(e))
def generate_verification_token(email):
# Generate a random token
token = secrets.token_urlsafe(32)
# You can associate this token with the provided email address in your database
# For example, store it in a verification_tokens table with an expiration date
return token