def send_email(email_conf, server_conf):
    """sends email notification of finished torrent"""
    # Send multipart message with text and html
    if email_conf.has_key("message"):
        # Send Multipart email
        if email_conf.has_key("message_html"):
            mime_message = MIMEMultipart("alternative")
            msg_plain = MIMEText(email_conf["message"].encode('utf-8'), "plain", _charset='utf-8')
            msg_html = MIMEText(email_conf["message_html"].encode('utf-8'), "html", _charset='utf-8')
            mime_message.attach(msg_plain)
            mime_message.attach(msg_html)
        else:
            # Send plain message
            mime_message = MIMEText(email_conf["message"].encode('utf-8'), "plain", _charset='utf-8')
    elif email_conf.has_key("message_html"):
        # Send html message
        mime_message = MIMEText(email_conf["message"].encode('utf-8'), "html", _charset='utf-8')
    else:
        log.warn("Email config must contain either 'message' or 'message_html'")
        return False

    mime_message["Subject"] = email_conf["subject"]
    mime_message["From"] = server_conf["from_address"]
    mime_message["To"] = email_conf["to_address"]

    port = smtplib.SMTP_PORT
    if len(server_conf["smtp_port"].strip()) > 0:
        try:
            port = int(server_conf["smtp_port"])
        except:
            pass
    try:
        mailServer = smtplib.SMTP(server_conf["smtp_server"], port)
    except Exception, e:
        log.error("There was an error sending the notification email: %s" % str(e))
        return False
                                                           server_conf["smtp_authentication"]))
    if server_conf["smtp_authentication"]:
        mailServer.ehlo()
        mailServer.starttls()
        mailServer.ehlo()
        try:
            mailServer.login(server_conf["smtp_username"], server_conf["smtp_password"])
        except smtplib.SMTPHeloError:
            log.warn("The server didn't reply properly to the helo greeting")
        except smtplib.SMTPAuthenticationError:
            log.warn("The server didn't accept the username/password combination")
    try:
        mailServer.sendmail(server_conf["from_address"], email_conf["to_address"], mime_message.as_string())
        mailServer.quit()
    except Exception, e:
        log.error("Sending email notification failed: %s", str(e))
        return False
    else:
        log.info("Sending email notification of finished torrent was successful")
    return True


def send_torrent_email(email_configurations, email_msg, subscription_data=None,
                       torrent_name_list=None, defered=False, callback_func=None, email_data={}):
    """Send email with optional list of torrents
    Arguments:
    email_configurations - the main email configuration of YARSS2
    email_msg - a dictionary with the email data (as saved in the YARSS config)
    torrents - a tuple containing the subscription data and a list of torrent names.
    """
    log.info("Sending email '%s'" % email_msg["name"])