File size: 2,434 Bytes
699b778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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