def _prepare_message(self, addresses, subject, contents, attachments, headers):
        if self.is_closed:
            raise YagConnectionClosed('Login required again')
        if isinstance(contents, str):
            contents = [contents]
        if isinstance(attachments, str):
            attachments = [attachments]

        # merge contents and attachments for now.
        if attachments is not None:
            for a in attachments:
                if not os.path.isfile(a):
                    raise TypeError("'{}' is not a valid filepath".format(a))
            contents = attachments if contents is None else contents + attachments

        has_included_images, content_objects = self._prepare_contents(contents)
        msg = MIMEMultipart()
        if headers is not None:
            # Strangely, msg does not have an update method, so then manually.
            for k, v in headers.items():
                msg[k] = v

        msg_alternative = MIMEMultipart('alternative')
        msg_related = MIMEMultipart('related')
        msg.attach(msg_alternative)
        self._add_subject(msg, subject)
        self._add_recipients_headers(msg, addresses)
        htmlstr = ''
        altstr = []
        if has_included_images:
            msg.preamble = "This message is best displayed using a MIME capable email reader."

        if contents is not None:
            for content_object, content_string in zip(content_objects,
                                                      contents):
                if content_object['main_type'] == 'image':
                    # aliased image {'path' : 'alias'}
                    if isinstance(content_string, dict) and len(content_string) == 1:
                        for key in content_string:
                            hashed_ref = str(abs(hash(key)))
                            alias = content_string[key]
                        # pylint: disable=undefined-loop-variable
                        content_string = key
                    else:
                        alias = os.path.basename(content_string)
                        hashed_ref = str(abs(hash(alias)))

                    # TODO: I should probably remove inline now that there is "attachments"
                    # if string is `inline`, inline, else, attach
                    # pylint: disable=unidiomatic-typecheck
                    if type(content_string) == inline:
                        htmlstr += '<img src="cid:{}" title="{}"/>'.format(hashed_ref, alias)
                        content_object['mime_object'].add_header(
                            'Content-ID', '<{}>'.format(hashed_ref))
                        altstr.append('-- img {} should be here -- '.format(alias))

                if content_object['encoding'] == 'base64':
                    email.encoders.encode_base64(content_object['mime_object'])
                    msg.attach(content_object['mime_object'])
                else:
                    content_string = content_string.replace('\n', '<br>')
                    htmlstr += '<div>{}</div>'.format(content_string)
                    altstr.append(content_string)

        msg_related.attach(MIMEText(htmlstr, 'html', _charset=self.encoding))
        msg_alternative.attach(MIMEText('\n'.join(altstr), _charset=self.encoding))
        msg_alternative.attach(msg_related)
        return msg
Prepare a MIME message
dataset/ETHPy150Open kootenpv/yagmail/yagmail/yagmail.py/SMTP._prepare_message