def send_email(to, subject, text, **params):
    """Send an outgoing email with the given parameters.

    This function assumes your system has a valid MTA (Mail Transfer Agent)
    or local SMTP server. This function will first try a local SMTP server
    and then the system's MTA (/usr/sbin/sendmail) connection refused.

    :param to: A list of recipient email addresses.
    :type to: list

    :param subject: The subject of the email.
    :type subject: str

    :param test: The text of the email.
    :type text: str

    :param params: An optional set of parameters. (See below)
    :type params; dict

    Optional Parameters:
    :cc: A list of Cc email addresses.
    :bcc: A list of Cc email addresses.
    :files: A list of files to attach.
    :sender: A custom sender (From:).
    """

    # Default Parameters
    cc = params.get("cc", [])
    bcc = params.get("bcc", [])
    files = params.get("files", [])
    sender = params.get("sender", "root@localhost")

    recipients = list(chain(to, cc, bcc))

    # Prepare Message
    msg = MIMEMultipart()
    msg.preamble = subject
    msg.add_header("From", sender)
    msg.add_header("Subject", subject)
    msg.add_header("To", ", ".join(to))
    cc and msg.add_header("Cc", ", ".join(cc))

    # Attach the main text
    msg.attach(MIMEText(text))

    # Attach any files
    [msg.attach(mimify_file(filename)) for filename in files]

    # Contact local SMTP server and send Message
    try:
        smtp = SMTP()
        smtp.connect()
        smtp.sendmail(sender, recipients, msg.as_string())
        smtp.quit()
    except SocketError as e:
        if e.args[0] == ECONNREFUSED:
            p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
            p.communicate(msg.as_string())
        else:
            raise
Send an outgoing email with the given parameters.

    This function assumes your system has a valid MTA (Mail Transfer Agent)
    or local SMTP server. This function will first try a local SMTP server
    and then the system's MTA (/usr/sbin/sendmail) connection refused.

    :param to: A list of recipient email addresses.
    :type to: list

    :param subject: The subject of the email.
    :type subject: str

    :param test: The text of the email.
    :type text: str

    :param params: An optional set of parameters. (See below)
    :type params; dict

    Optional Parameters:
    :cc: A list of Cc email addresses.
    :bcc: A list of Cc email addresses.
    :files: A list of files to attach.
    :sender: A custom sender (From:).
    
recipes/Python/578472_Sending_Email_in_Python/recipe-578472.py