<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def _produce_output(report, failed, setup):
    '''
    Produce output from the report dictionary generated by _generate_report
    '''
    report_format = setup.get('report_format', 'yaml')

    log.debug('highstate output format: %s', report_format)

    if report_format == 'json':
        report_text = salt.utils.json.dumps(report)
    elif report_format == 'yaml':
        string_file = StringIO()
        salt.utils.yaml.safe_dump(report, string_file, default_flow_style=False)
        string_file.seek(0)
        report_text = string_file.read()
    else:
        string_file = StringIO()
        _generate_html(report, string_file)
        string_file.seek(0)
        report_text = string_file.read()

    report_delivery = setup.get('report_delivery', 'file')

    log.debug('highstate report_delivery: %s', report_delivery)

    if report_delivery == 'file':
        output_file = _sprinkle(setup.get('file_output', '/tmp/test.rpt'))
        with salt.utils.files.fopen(output_file, 'w') as out:
            out.write(salt.utils.stringutils.to_str(report_text))
    else:
        msg = MIMEText(report_text, report_format)

        sender = setup.get('smtp_sender', '')
        recipients = setup.get('smtp_recipients', '')

        host = setup.get('smtp_server', '')
        port = int(setup.get('smtp_port', 25))
        tls = setup.get('smtp_tls')
        username = setup.get('smtp_username')
        password = setup.get('smtp_password')

        if failed:
            subject = setup.get('smtp_failure_subject', 'Installation failure')
        else:
            subject = setup.get('smtp_success_subject', 'Installation success')

        subject = _sprinkle(subject)

        msg['Subject'] = subject
        msg['From'] = sender
        msg['To'] = recipients

        log.debug('highstate smtp port: %d', port)
        smtp = smtplib.SMTP(host=host, port=port)

        if tls is True:
            smtp.starttls()
            log.debug('highstate smtp tls enabled')

        if username and password:
            smtp.login(username, password)
            log.debug('highstate smtp authenticated')

        smtp.sendmail(
            sender,
            [x.strip() for x in recipients.split(',')], msg.as_string())
        log.debug('highstate message sent.')

        smtp.quit()