Ejemplo n.º 1
0
    def _add_image(self, data_img: List[bytes],
                   message: MIMEMultipart) -> None:
        """
        add images in email
        :param data_img: images content to add in the mail
        :param message: email to add images
        :return:None
        """
        if data_img is None:
            data_img = []
        for idx, img_bytes in enumerate(data_img):
            # set attachment mime
            filename = 'chart-image' + str(idx) + '.png'
            logger.debug("create mime for image " + filename)
            mime = MIMEBase('image', 'png', filename=filename)
            # add required header
            mime.add_header('Content-Disposition',
                            'attachment',
                            filename=filename)
            mime.add_header('X-Attachment-Id', str(idx))
            mime.add_header('Content-ID', '<' + str(idx) + '>')
            # put content  into MIMEBase object
            mime.set_payload(img_bytes)
            encoders.encode_base64(mime)
            # add MIMEBase to MIMEMultipart object
            message.attach(mime)

        logger.debug("add image in email DONE with " + str(len(data_img)) +
                     " images")
Ejemplo n.º 2
0
 def parse_response(self, response: Response) -> dict:
     """
     create a dict of the response, add a key 'content' for the bytes content value
     :param response:
     :return:dict
     """
     data = {"content": response.content}
     logger.debug("ChartBuilder parse_response")
     return data
 def __init__(self, abspath_filename: str) -> None:
     """
     create self.template from the template html file
     :param abspath_filename:absolute path of the template html file
     """
     logger.debug("Read template file html")
     with open(abspath_filename, 'r', encoding='utf-8') as template_file:
         template_file_content = template_file.read()
     logger.debug("Read template file html DONE")
     self.template = Template(template_file_content)
 def build_message(self, data: dict) -> str:
     """
     apply the data on the template
     Warning : the template html must use the variable with prefix data
     ex : <h1>{{data.title_var}</h1> -> my_dict = {'title_var':'title_value'}
     :param data: dict of the variable and value to apply
     :return:the message to send in an email
     """
     message = self.template.render(data=data)
     logger.debug("Build message result :" + message)
     return message
Ejemplo n.º 5
0
    def parse_response(self, response: Response) -> dict:
        """
        parse json response to dict
        :param response:
        :return:dict
        """
        data = json.loads(response.json())
        # same order between calls
        data['details'].sort(key=lambda x: x['trend_name'])
        logger.debug("IndicatorBuilder parse_response")
        logger.debug(data)

        return data
Ejemplo n.º 6
0
    def _server_email(self, message: MIMEMultipart) -> None:
        """
            use server smtp to send the email
            if is_secure_mode is True, use a ssl context to send the email
        :param message: the message to send
        :return: None
        """
        if self.is_secure_mode:
            # Create secure SSL context
            context = ssl.create_default_context()

            with smtplib.SMTP_SSL(self.smtp_host,
                                  self.smtp_port,
                                  context=context) as server:
                logger.debug("Connect to secure smtp server")
                server.login(self.secure_mode_login, self.secure_mode_pwd)
                logger.debug("send secure email from " + self.sender_email +
                             " to " + self.receiver_email)
                server.sendmail(self.sender_email, self.receiver_email,
                                message.as_string())
        else:
            with smtplib.SMTP(self.smtp_host, self.smtp_port) as server:
                logger.debug("send email from " + self.sender_email + " to " +
                             self.receiver_email)
                server.sendmail(self.sender_email, self.receiver_email,
                                message.as_string())
Ejemplo n.º 7
0
    def send_email(self, msg_html: str, data_img: List[bytes] = None) -> None:
        """
        Send email in multipart mode (plain + html) with images

        :param msg_html: html msg string to send
        :param data_img: images content to add in the mail
        :return:None
        """

        logger.debug("create mime email object")
        message = self._create_message_email(msg_html)

        logger.debug("add image in email")
        self._add_image(data_img, message)

        logger.debug("proceed to send email")
        self._server_email(message)