forked from git-ankit/MovieRecommender
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
This module contains all the constants | ||
""" | ||
|
||
EMAIL_HTML_CONTENT = """ | ||
<html> | ||
<head></head> | ||
<body> | ||
<h1 style="color: #333333;">Movie Recommendations from PopcornPicks</h1> | ||
<p style="color: #555555;">Dear Movie Enthusiast,</p> | ||
<p style="color: #555555;">We hope you're having a fantastic day!</p> | ||
<div style="padding: 10px; border: 1px solid #cccccc; border-radius: 5px; background-color: #f9f9f9;"> | ||
<h2>Your Movie Recommendations:</h2> | ||
<h3>Movies Liked:</h3> | ||
<ul style="color: #555555;"> | ||
{} | ||
</ul> | ||
<h3>Movies Disliked:</h3> | ||
<ul style="color: #555555;"> | ||
{} | ||
</ul> | ||
<h3>Movies Yet to Watch:</h3> | ||
<ul style="color: #555555;"> | ||
{} | ||
</ul> | ||
</div> | ||
<p style="color: #555555;">Enjoy your movie time with PopcornPicks!</p> | ||
<p style="color: #555555;">Best regards,<br>PopcornPicks Team 🍿</p> | ||
</body> | ||
</html> | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
""" Module contains utility functions used for various purposes in the backend """ | ||
|
||
import logging | ||
import smtplib | ||
from email.mime.text import MIMEText | ||
from email.mime.multipart import MIMEMultipart | ||
import logging | ||
|
||
import constants as c | ||
|
||
|
||
def beautify_feedback_data(data): | ||
""" | ||
|
@@ -22,12 +27,11 @@ def beautify_feedback_data(data): | |
dislike.append(movie) | ||
|
||
# Create a category-dictionary of liked, disliked and yet to watch movies | ||
categorized_data_dict = {"Liked":like, "Disliked":dislike, "Yet to Watch":yet_to_watch} | ||
categorized_data_dict = {"Liked": like, | ||
"Disliked": dislike, "Yet to Watch": yet_to_watch} | ||
|
||
return categorized_data_dict | ||
|
||
|
||
|
||
def send_email_to_user(recipient_email, categorized_data): | ||
""" | ||
Utility function to send movie recommendations to user over email | ||
|
@@ -36,11 +40,11 @@ def send_email_to_user(recipient_email, categorized_data): | |
# Email configuration | ||
smtp_server = 'smtp.gmail.com' | ||
# Port for TLS | ||
smtp_port = 587 | ||
smtp_port = 587 | ||
sender_email = '[email protected]' | ||
|
||
# Use an app password since 2-factor authentication is enabled | ||
sender_password = 'uxnd shis sazo mstj' | ||
sender_password = 'uxnd shis sazo mstj' | ||
subject = 'Your movie recommendation from PopcornPicks' | ||
|
||
# Create the email message | ||
|
@@ -50,52 +54,27 @@ def send_email_to_user(recipient_email, categorized_data): | |
message['Subject'] = subject | ||
|
||
# Create the email message with HTML content | ||
html_content = """ | ||
<html> | ||
<head></head> | ||
<body> | ||
<h1 style="color: #333333;">Movie Recommendations from PopcornPicks</h1> | ||
<p style="color: #555555;">Dear Movie Enthusiast,</p> | ||
<p style="color: #555555;">We hope you're having a fantastic day!</p> | ||
<div style="padding: 10px; border: 1px solid #cccccc; border-radius: 5px; background-color: #f9f9f9;"> | ||
<h2>Your Movie Recommendations:</h2> | ||
<h3>Movies Liked:</h3> | ||
<ul style="color: #555555;"> | ||
{} | ||
</ul> | ||
<h3>Movies Disliked:</h3> | ||
<ul style="color: #555555;"> | ||
{} | ||
</ul> | ||
<h3>Movies Yet to Watch:</h3> | ||
<ul style="color: #555555;"> | ||
{} | ||
</ul> | ||
</div> | ||
<p style="color: #555555;">Enjoy your movie time with PopcornPicks!</p> | ||
<p style="color: #555555;">Best regards,<br>PopcornPicks Team 🍿</p> | ||
</body> | ||
</html> | ||
""".format('\n'.join(f'<li>{movie}</li>' for movie in categorized_data['Liked']), | ||
'\n'.join(f'<li>{movie}</li>' for movie in categorized_data['Disliked']), | ||
'\n'.join(f'<li>{movie}</li>' for movie in categorized_data['Yet to Watch'])) | ||
html_content = c.EMAIL_HTML_CONTENT.format('\n'.join(f'<li>{movie}</li>' for movie in categorized_data['Liked']), | ||
'\n'.join( | ||
f'<li>{movie}</li>' for movie in categorized_data['Disliked']), | ||
'\n'.join(f'<li>{movie}</li>' for movie in categorized_data['Yet to Watch'])) | ||
|
||
# Attach the HTML email body | ||
message.attach(MIMEText(html_content, 'html')) | ||
|
||
# Connect to the SMTP server | ||
try: | ||
server = smtplib.SMTP(smtp_server, smtp_port) | ||
# Start TLS encryption | ||
server.starttls() | ||
server.starttls() | ||
server.login(sender_email, sender_password) | ||
|
||
# Send the email | ||
server.sendmail(sender_email, recipient_email, message.as_string()) | ||
logging.info("Email sent successfully!") | ||
|
||
except Exception as e: | ||
logging.warning(f'Email could not be sent. Error: {str(e)}') | ||
logging.warning("Email could not be sent. Error: %s", str(e)) | ||
|
||
finally: | ||
server.quit() |