Spaces:
Sleeping
Sleeping
from mailjet_rest import Client | |
import base64 | |
import pandas as pd | |
from io import BytesIO | |
import datetime | |
class Mail(): | |
def __init__(self, api_key, api_secret): | |
self.api_key = api_key | |
self.api_secret = api_secret | |
self.mailjet = Client(auth=(self.api_key, self.api_secret), version='v3.1') | |
def send_mail(self, fromName, fromMail, fromSubject, fromMsg, dataframe, forecast_dates, toMail): | |
Subject = 'Predictions are ready! - Infineon Product Demand Forecasting System' | |
TextPart = f'Greetings from {Subject}' | |
with BytesIO() as output: | |
with pd.ExcelWriter(output, engine='xlsxwriter') as writer: | |
dataframe.to_excel(writer, sheet_name='Sheet1', index=False) | |
output.seek(0) | |
excel_content = output.read() | |
# Encode Excel content to base64 | |
base64_content = base64.b64encode(excel_content).decode() | |
today = datetime.datetime.today().strftime("%d-%m-%Y") | |
data = { | |
'Messages': [ | |
{ | |
"From": { | |
"Email": "[email protected]", | |
"Name": "Me" | |
}, | |
"To": [ | |
{ | |
"Email": toMail, | |
"Name": "You" | |
} | |
], | |
"Subject": Subject, | |
"TextPart": TextPart, | |
"HTMLPart": f"Hello,<br/><br/>You can access the demand forecasting output for Infenion for the following dates <b>{forecast_dates[0]}</b> - <b>{forecast_dates[1]}</b> in the attachments<br/>If you have any problems, you can let us know via this e-mail address.<br/><br/>Have a good day,<br/><h3>Infineon AI Department</h3>", | |
'Attachments': [ | |
{ | |
'ContentType': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | |
'Filename': f'demand_predictions_{today}.xlsx', | |
'Base64Content': base64_content | |
} | |
] | |
} | |
] | |
} | |
result = self.mailjet.send.create(data=data) | |
return result.status_code |