Publish by sending the report by e-mail

def send_report(self, report_parts):
        """
        Publish by sending the report by e-mail
        """
        logger.info('Creating an email message')
        report_parts = sorted(
            filter(lambda x: x.fmt in self.formats, report_parts),
            key=lambda x: self.formats.index(x.fmt)
        )
        fmtname = '{0}-{1}.{2}' if len(report_parts) > 1 else '{0}.{2}'
        root_part = MIMEMultipart('mixed')
        root_part.preamble = 'This is a multi-part message in MIME format.'

        logger.debug('Creating the text/"text_type" parts')
        for i, text_part in enumerate(report_parts):
            attachment_name = fmtname.format(socket.gethostname(), i, text_part.ext)
            attach_part = MIMEText(text_part.text, text_part.ext, 'utf-8')
            attach_part.add_header('Content-Disposition', 'attachment', filename=attachment_name)
            root_part.attach(attach_part)

        if self.rawlogs:
            out = BytesIO()
            do_chunked_gzip(self.rawfh, out, filename=u'raw.log.gz')
            out.seek(0, os.SEEK_END)
            size = out.tell()

            if size > self.rawlogs_limit:
                logger.warning('%d is over the defined max of %r', size, self.rawlogs_limit)
                logger.warning('Not attaching the raw logs')
            else:
                logger.debug('Creating the application/x-gzip part')
                attach_part = MIMEBase('application', 'x-gzip')
                attach_part.set_payload(out.getvalue())

                from email.encoders import encode_base64

                logger.debug('Encoding the gzipped raw logs with base64')
                encode_base64(attach_part)
                attach_part.add_header('Content-Disposition', 'attachment', filename='raw.log.gz')
                root_part.attach(attach_part)

        if self.gpg_encrypt:
            import gpgme
            try:
                if self.gpg_keyringdir and os.path.exists(self.gpg_keyringdir):
                    logger.debug('Setting keyring dir to %r', self.gpg_keyringdir)
                    os.environ['GNUPGHOME'] = self.gpg_keyringdir

                cleartext = BytesIO(root_part.as_string().encode())
                ciphertext = BytesIO()

                ctx = gpgme.Context()
                ctx.armor = True

                if self.gpg_recipients:
                    recipients = [ctx.get_key(recipient) for recipient in self.gpg_recipients]
                else:
                    recipients = []
                    for key in ctx.keylist():
                        for subkey in key.subkeys:
                            if subkey.can_encrypt:
                                logger.debug('Found can_encrypt key = %d', subkey.keyid)
                                recipients.append(key)
                                break

                signers = [ctx.get_key(signer) for signer in self.gpg_signers]
                if signers:
                    logger.info('Encrypting and signing the report')
                    ctx.signers = signers
                    ctx.encrypt_sign(recipients, gpgme.ENCRYPT_ALWAYS_TRUST, cleartext, ciphertext)
                else:
                    logger.info('Encrypting the report')
                    ctx.encrypt(recipients, gpgme.ENCRYPT_ALWAYS_TRUST, cleartext, ciphertext)

                logger.debug('Creating the MIME envelope for PGP')

                gpg_envelope_part = MIMEMultipart('encrypted')
                gpg_envelope_part.set_param('protocol', 'application/pgp-encrypted', header='Content-Type')
                gpg_envelope_part.preamble = 'This is an OpenPGP/MIME encrypted message (RFC 2440 and 3156)'

                gpg_mime_version_part = MIMEBase('application', 'pgp-encrypted')
                gpg_mime_version_part.add_header('Content-Disposition', 'PGP/MIME version identification')
                gpg_mime_version_part.set_payload('Version: 1')

                gpg_payload_part = MIMEBase('application', 'octet-stream', name='encrypted.asc')
                gpg_payload_part.add_header('Content-Disposition', 'OpenPGP encrypted message')
                gpg_payload_part.add_header('Content-Disposition', 'inline', filename='encrypted.asc')
                gpg_payload_part.set_payload(ciphertext.getvalue())

                gpg_envelope_part.attach(gpg_mime_version_part)
                gpg_envelope_part.attach(gpg_payload_part)

                # envelope becomes the new root part
                root_part = gpg_envelope_part

            except ImportError:
                logger.error('Need crypto libraries for gpg_encrypt.')
                logger.error('Install pygpgme for GPG encryption support.')
                logger.error('Not mailing the report out of caution.')
                return

        # Define headers
        root_part['Date'] = formatdate()
        root_part['From'] = self.email_address
        root_part['To'] = ', '.join(self.mailto)
        root_part['Subject'] = '{0} system events: {1}'.format(
                socket.gethostname(), time.strftime('%c', time.localtime())
        )
        root_part['Message-Id'] = make_msgid()
        root_part['X-Mailer'] = u'{0}-{1}'.format(package_name, __version__)
        
        mail_message(self.smtp_server, root_part.as_string(), self.email_address, self.mailto)
        print('Mailed the report to: {0}'.format(','.join(self.mailto)))