from email.message import EmailMessage import smtplib import ssl from datetime import timedelta, datetime import jwt from django.utils import timezone from .models import UserProfile, Invitation from .constants import SECRET_KEY, PASSWORD, USER, EXPIRATION_PERIOD def generate_jwt_token(receiver_id, uuid): """Generates a jwt token to be sent in the link for the receiver. """ print(receiver_id, "RECEIVER ID") receiver_profile = UserProfile.objects.get(user_id=receiver_id) print(receiver_profile.user.email, "RECEIVER PROFILE") payload = { "uuid": uuid, "userId": receiver_profile.id, "userName": receiver_profile.full_name, "isInquirer": False, "isRespondent": True, "collegeName": receiver_profile.college_name, "userEmail": receiver_profile.user.email, "gradDate": receiver_profile.grad_date.isoformat(), "preferredLanguage": receiver_profile.preferred_language, "isLoggedIn": True, } token = jwt.encode(payload, SECRET_KEY, algorithm='HS256') return send_invitation_link(receiver_profile, token) def send_invitation_link(receiver_profile, token): """Send receiver link to join call. This link will contain encoded information that will allow the frontend to know that they are the receiver. """ subject = "[Bendwidth] Meeting invitation link." body = f"""
Your meeting is scheduled for (some date),
please use this link to join:
Join Chat
You have received an invitation for a study session in 10 min. Do you want to accept this invite?
Cheers,
Bendwidth Admin
""" email = EmailMessage() email['From'] = USER email['To'] = receiver.user.email email['Subject'] = "Bendwidth (Time-sensitive!)" email.set_content(body, subtype='html') context = ssl.create_default_context() with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp: try: smtp.login(USER, PASSWORD) smtp.sendmail(USER, receiver.user.email, email.as_string()) except Exception as e: return {"error": str(e)} return {"success": True}