# Path: cus_exception.py
# class CusException(Exception):
#     def __init__(self, name, error_msg):
#         super(CusException, self).__init__(name, error_msg)
#         self.name = name
# 
#         if type(error_msg) == CusException:
#             self.error_msg = error_msg.error_msg
#         else:
#             self.error_msg = error_msg
# 
#         self.error_time = str(datetime.datetime.now())


import smtplib
import logging
import config
from cus_exception import CusException
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


def send_email(to_email, subject, body):
    try:
        logging.debug('send_email start...')

        msg = MIMEMultipart()
        msg['From'] = config.self_email
        msg['To'] = to_email
        msg['Subject'] = subject

        msg.attach(MIMEText(body, 'plain'))

        if config.email_type == 'gmail':  # gmail send
            server = smtplib.SMTP('smtp.gmail.com:587')
        elif config.email_type == 'qq':  # qq send
            server = smtplib.SMTP_SSL('smtp.qq.com', 465)
        else:  # default gmail
            server = smtplib.SMTP('smtp.gmail.com:587')

        server.set_debuglevel(1)
        server.ehlo()
        server.starttls()
        server.login(config.self_email, config.self_password)
        server.sendmail(config.self_email, to_email, msg.as_string())
        server.quit()
        logging.debug('send_email success...')
        return True
    except Exception, e:
        logging.exception('send_email exception msg:%s' % e)

# Path: cus_exception.py
# class CusException(Exception):
#     def __init__(self, name, error_msg):
#         super(CusException, self).__init__(name, error_msg)
#         self.name = name
# 
#         if type(error_msg) == CusException:
#             self.error_msg = error_msg.error_msg
#         else:
#             self.error_msg = error_msg
# 
#         self.error_time = str(datetime.datetime.now())


# Path: jd/send_email.py
import smtplib
import logging
import config
from cus_exception import CusException
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart



def send_email(to_email, subject, body):
    try:
        logging.debug('send_email start...')

        msg = MIMEMultipart()
        msg['From'] = config.self_email
        msg['To'] = to_email
        msg['Subject'] = subject

        msg.attach(MIMEText(body, 'plain'))

        if config.email_type == 'gmail':  # gmail send
            server = smtplib.SMTP('smtp.gmail.com:587')
        elif config.email_type == 'qq':  # qq send
            server = smtplib.SMTP_SSL('smtp.qq.com', 465)
        else:  # default gmail
            server = smtplib.SMTP('smtp.gmail.com:587')

        server.set_debuglevel(1)
        server.ehlo()
        server.starttls()
        server.login(config.self_email, config.self_password)
        server.sendmail(config.self_email, to_email, msg.as_string())
        server.quit()
        logging.debug('send_email success...')
        return True
    except Exception, e:
        logging.exception('send_email exception msg:%s' % e)

        raise CusException('send_email', 'send_email error msg:%s' % e)