<commit_before>from email.mime.application import MIMEApplication
from email.mime.audio import MIMEAudio
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib

from mako.template import Template


class EasyEmail(object):
    """
    This class aims to abstract sending emails from templates, for now it
    covers only mako templating system but jinja2 is on its way.

    Constructor arguments:

    ``to``

       Defaults to empty list, can be list of emails or string.

    ``sender``

       Defaults to empty string - this should contain email of the sender (from)

    ``subject``

       Defaults to empty string - should contain subject of the message
    """
    def __init__(self, to=[], sender='', subject=''):
        self.message = MIMEMultipart('alternative')
        self.subject = subject
        self.to = to
        self.sender = sender

    def attach_image(self, image_content, subtype=None):
        self.message.attach(MIMEImage(image_content, _subtype=subtype))

    def attach_binary(self, binary_data, subtype=None):
        self.message.attach(MIMEApplication(binary_data, _subtype=subtype))

    def attach_text(self, text, subtype=None):
        self.message.attach(MIMEText(text, _subtype=subtype))

    def attach_audio(self, audio_data, subtype=None):
        self.message.attach(MIMEAudio(audio_data, _subtype=subtype))

    def load_template(self, template_file_path, context, data_type='html',
                      ttype='mako'):
        if ttype == 'mako':
            with open(template_file_path, 'r') as template_file:
                template = Template(template_file.read()).render(**context)
                self.attach_text(template, data_type)

    def send(self, connection_type='smtp', authentication=False, **smtp_params):
        self._compose()
        smtp_connection = self._connect(
            connection_type, authentication, **smtp_params
        )
        smtp_connection.sendmail(
            from_addr = self.sender,
            to_addrs = self.to,
            msg = self.message.as_string()
        )
        smtp_connection.quit()

    def _connect(self, connection_type, authentication, **smtp_params):
        if connection_type == 'smtp':
            smtp_connection = smtplib.SMTP(
                smtp_params.get('host', 'localhost'),
                smtp_params.get('port', 25),
                smtp_params.get('local_hostname'),
                smtp_params.get('timeout')
            )
        elif connection_type == 'ssl':
            smtp_connection = smtplib.SMTP_SSL(
                smtp_params.get('host'),
                smtp_params.get('port'),
                smtp_params.get('local_hostname'),
                smtp_params.get('keyfile'),
                smtp_params.get('certfile'),
                smtp_params.get('timeout')
            )
        elif connection_type == 'tls':
            smtp_connection = smtplib.SMTP(
                smtp_params.get('host'),
                smtp_params.get('port'),
                smtp_params.get('local_hostname'),
                smtp_params.get('timeout')
            )
            smtp_connection.starttls(
                smtp_params.get('keyfile'),
                smtp_params.get('certfile')
            )
        if authentication:
            smtp_connection.login(
                smtp_params.get('login'),
                smtp_params.get('password')
            )
        return smtp_connection

    def _compose(self):
        self.message['Subject'] = self.subject
        self.message['From'] = self.sender
        if isinstance(self.to, list):
            self.message['To'] = ', '.join(self.to)
        else:
            self.message['To'] = self.to<commit_msg>Added much needed docstrings.<commit_after>from email.mime.application import MIMEApplication
from email.mime.audio import MIMEAudio
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib

from mako.template import Template


class EasyEmail(object):

    """Send prepared email using smtplib buildin capabilities.

    This class aims to abstract sending emails from templates, for now it
    covers only mako templating system but jinja2 is on its way.

    Usage::

        >>> email = EasyEmail()
        >>> email.to = ["roman<EMAIL>", "<EMAIL>"]
        >>> email.subject = "Enlarge your library for FREE!"
        >>> email.sender = "<EMAIL>"
        >>> email.send()

    or::

        >>> email = EasyEmail(
        ...             to="<EMAIL>",
        ...             subject="No",
        ...             sender="<EMAIL>"
        ...         )
        >>> email.send()

    :params to: Defaults to empty list, can be list of emails or string.
    :params sender: Defaults to empty string - this should contain email
        of the sender (from).
    :params subject: Defaults to empty string - should contain subject of
        the message.
    """

    def __init__(self, to=[], sender='', subject=''):
        self.message = MIMEMultipart('alternative')
        self.subject = subject
        self.to = to
        self.sender = sender

    def attach_image(self, image_content, subtype=None):
        """Attach image binary data to message.

        :params image_content: :py:class:`email.mime.image.MIMEImage`
            parameter `_imagedata`.
        :params _subtype: :py:class:`email.mime.image.MIMEImage`
            parameter `_subtype` (defaults to None).
        """
        self.message.attach(MIMEImage(image_content, _subtype=subtype))

    def attach_binary(self, binary_data, subtype=None):
        """Attach binary data to message.

        :params binary_data: :py:class:`email.mime.application.MIMEApplication`
            parameter `_data`.
        :params _subtype: :py:class:`email.mime.application.MIMEApplication`
            parameter `_subtype` (defaults to None).
        """
        self.message.attach(MIMEApplication(binary_data, _subtype=subtype))

    def attach_text(self, text, subtype=None):
        """Attach text data to message.

        :params text: :py:class:`email.mime.text.MIMEText`
            parameter `_text`.
        :params _subtype: :py:class:`email.mime.text.MIMEText`
            parameter `_subtype` (defaults to None).
        """
        self.message.attach(MIMEText(text, _subtype=subtype))

    def attach_audio(self, audio_data, subtype=None):
        """Attach text data to message.

        :params audio_data: :py:class:`email.mime.audio.MIMEAudio`
            parameter `_audiodata`.
        :params _subtype: :py:class:`email.mime.audio.MIMEAudio`
            parameter `_subtype` (defaults to None).
        """
        self.message.attach(MIMEAudio(audio_data, _subtype=subtype))

    def load_template(self, template_file_path, context, data_type='html',
                      ttype='mako'):
        """Load text template and attach to message.

        :params template_file_path: file path to template file.
        :params context: optional context for rendered template.
        :params data_type: mapped as `_subtype` in :func:`attach_text`.
        :params ttype: Template format type (defaults fo mako, for now can't
            be anything else).
        """

        if ttype == 'mako':
            with open(template_file_path, 'r') as template_file:
                template = Template(template_file.read()).render(**context)
                self.attach_text(template, data_type)

    def send(self, connection_type='smtp', authentication=False, **smtp_params):
        """Send prebuilded message.

        :params connection_type: string with type of connection. Possible
            choices are: `smtp` (default), `ssl` and `tls`.
        :params authentication: `boolean` (default False), set it to True if
            server that you use for sending uses authentication. If this
            parameter is set to True - you need to supply `login` and
            `password` keyword arguments.
        :params \*\*smtp_params: Additional parameters depending on
            `connection_type` chosen. For full list and description please go
            to :py:mod:`smtplib`.
        """

        self._compose()
        smtp_connection = self._connect(
            connection_type, authentication, **smtp_params
        )
        smtp_connection.sendmail(
            from_addr = self.sender,
            to_addrs = self.to,
            msg = self.message.as_string()
        )
        smtp_connection.quit()

    def _connect(self, connection_type, authentication, **smtp_params):
        """Create smtplib connection for different connection types.

        Returns active smtp connection that need to be closed after usage.

        :params connection_type: string with type of connection. Possible
            choices are: `smtp` (default), `ssl` and `tls`.
        :params authentication: `boolean` (default False), set it to True if
            server that you use for sending uses authentication. If this
            parameter is set to True - you need to supply `login` and
            `password` keyword arguments.
        :params \*\*smtp_params: Additional parameters depending on
            `connection_type` chosen. For full list and description please go
            to :py:mod:`smtplib`.
        """

        if connection_type == 'smtp':
            smtp_connection = smtplib.SMTP(
                smtp_params.get('host', 'localhost'),
                smtp_params.get('port', 25),
                smtp_params.get('local_hostname'),
                smtp_params.get('timeout')
            )

        elif connection_type == 'ssl':
            smtp_connection = smtplib.SMTP_SSL(
                smtp_params.get('host'),
                smtp_params.get('port'),
                smtp_params.get('local_hostname'),
                smtp_params.get('keyfile'),
                smtp_params.get('certfile'),
                smtp_params.get('timeout')
            )

        elif connection_type == 'tls':
            smtp_connection = smtplib.SMTP(
                smtp_params.get('host'),
                smtp_params.get('port'),
                smtp_params.get('local_hostname'),
                smtp_params.get('timeout')
            )
            smtp_connection.starttls(
                smtp_params.get('keyfile'),
                smtp_params.get('certfile')
            )

        if authentication:
            smtp_connection.login(
                smtp_params.get('login'),
                smtp_params.get('password')
            )

        return smtp_connection

    def _compose(self):
        """Compose message from object attributes.
        """
        self.message['Subject'] = self.subject
        self.message['From'] = self.sender
        if isinstance(self.to, list):
            self.message['To'] = ', '.join(self.to)
        else:
            self.message['To'] = self.to
{'fraction_non_alphanumeric': 0.10564729495483989, 'fraction_numerical': 0.0005473953106468388, 'mean_word_length': 2.190337601862631, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '730143', 'n_tokens_mistral': 3078, 'n_tokens_neox': 2957, 'n_words': 751}