'Attach a file with the given filename and content. The filename can
be omitted and the mimetype is guessed, if not provided.
If the first parameter is a MIMEBase subclass, insert it directly
into the resulting message attachments.
For a text/* mimetype (guessed or specified), when a bytes object is
specified as content, decode it as UTF-8. If that fails, set the
mimetype to DEFAULT_ATTACHMENT_MIME_TYPE and don\'t decode the content.'

def attach(self, filename=None, content=None, mimetype=None):

    if isinstance(filename, MIMEBase):
        assert (content is None)
        assert (mimetype is None)
        self.attachments.append(filename)
    else:
        assert (content is not None)
        if (not mimetype):
            (mimetype, _) = mimetypes.guess_type(filename)
            if (not mimetype):
                mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
        (basetype, subtype) = mimetype.split('/', 1)
        if (basetype == 'text'):
            if isinstance(content, bytes):
                try:
                    content = content.decode()
                except UnicodeDecodeError:
                    mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
        self.attachments.append((filename, content, mimetype))
