Skip to content

Commit

Permalink
Image in mail added
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyShrestha committed Apr 24, 2024
1 parent efd81a3 commit 992158d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Binary file not shown.
19 changes: 12 additions & 7 deletions formatted_zone/expiry_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ def send_email(row):
app_password = 'oofzndilqxjbvjko' # Use the app password generated for Mail/Other
subject = 'SpicyBytes: Product Expiry Notification'

image_path3 = os.path.join(config_dir, "images/spicy_img3.jpg")
image_path1 = os.path.join(config_dir, "images/spicy_img1.jpg")

# Create SMTP connection
server = smtplib.SMTP(smtp_server, port)

item_names = row["product_name"]
expiry_dates = row["expected_expiry_date"]
customer_name = row['customer_name']

server.starttls()
server.login(sender_email, app_password)
Expand Down Expand Up @@ -109,9 +110,11 @@ def send_email(row):
</style>
</head>
<body>
<div class="container">
<center><img src='cid:image1' alt="SpicyBytes Logo" width="200" height="50"></center>
<h1>Grocery Item Expiry Notification</h1>
<p>Dear User,</p>
<p>Dear """+customer_name+""",</p>
<p>We would like to inform you that the following grocery items in your inventory are about to expire:</p>
<table>
Expand Down Expand Up @@ -147,11 +150,13 @@ def send_email(row):

# Attach image

if os.path.exists(image_path3):
with open(image_path3, 'rb') as img_file:
if os.path.exists(image_path1):
with open(image_path1, 'rb') as img_file:
image = MIMEImage(img_file.read())
image.add_header('Content-Disposition', 'attachment', filename=os.path.basename(image_path3))
image.add_header('Content-ID', '<image1>')
msg.attach(image)
# image.add_header('Content-Disposition', 'attachment', filename=os.path.basename(image_path1))
# msg.attach(image)

# Send email
server.sendmail(sender_email, receiver_email, msg.as_string())
Expand Down Expand Up @@ -188,12 +193,12 @@ def send_email(row):
# Read the Parquet file into a DataFrame
estimated_avg_expiry_df = spark.read.parquet(estimated_avg_expiry)

estimated_avg_expiry_df =estimated_avg_expiry_df.select("email_id", "product_name","purchase_date","avg_expiry_days","expected_expiry_date")
estimated_avg_expiry_df =estimated_avg_expiry_df.select("email_id", "customer_name", "product_name","purchase_date","avg_expiry_days","expected_expiry_date")
estimated_avg_expiry_df = estimated_avg_expiry_df.withColumn("remaining_days", datediff(col("expected_expiry_date"), current_date()))

estimated_avg_expiry_df = estimated_avg_expiry_df.filter((col("remaining_days") > 0) & (col("remaining_days") < days_considered))

mail_group_df = estimated_avg_expiry_df.groupBy("email_id").agg(
mail_group_df = estimated_avg_expiry_df.groupBy("email_id","customer_name").agg(
collect_list("product_name").alias("product_name"),
collect_list("expected_expiry_date").alias("expected_expiry_date")
)
Expand Down

0 comments on commit 992158d

Please sign in to comment.