# -*- coding: utf-8 -*-
"""
Created on Wed Feb 20 16:08:17 2019

@author: zhouyujiang

发送有附件的 邮件

"""




import os
import smtplib
# For guessing MIME type based on file name extension
import mimetypes
from email import encoders
#from email.message import Message
#from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
#from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#COMMASPACE = ', '

def send_file_email(file_path, data_str, email_group=[],
                    email_msg_body_str=None,
                    title_str=None,
                    cc_group=["litao@igengmei.com"], sender=None):
    directory = file_path
    # Create the enclosing (outer) message
    if email_msg_body_str == None:
        email_msg_body = '''{0}    '''.format(data_str=data_str)
    else:
        email_msg_body = email_msg_body_str
    outer = MIMEMultipart()
    if title_str == None:
        title = '-' + data_str
    else:
        title = title_str
    outer['Subject'] = title
    outer['To'] = ','.join(email_group)
    outer['Cc'] = ','.join(cc_group)
    if not sender:
        outer['From'] = 'litao@igengmei.com'
    else:
        outer['From'] = sender
    mail_service = 'smtp.exmail.qq.com'
    outer.attach(MIMEText(email_msg_body))

    if directory:
        for filename in os.listdir(directory):
            path = os.path.join(directory, filename)
            if not os.path.isfile(path):
                continue
            # Guess the content type based on the file's extension.  Encoding
            # will be ignored, although we should check for simple things like
            # gzip'd or compressed files.
            ctype, encoding = mimetypes.guess_type(path)
            if ctype is None or encoding is not None:
                # No guess could be made, or the file is encoded (compressed), so
                # use a generic bag-of-bits type.
                ctype = 'application/octet-stream'
            maintype, subtype = ctype.split('/', 1)
            #        if maintype == 'text':
            ##            with open(path,encoding='utf-8') as fp:
            ###                 Note: we should handle calculating the charset
            ##                msg = MIMEText(fp.read(), _subtype=subtype)
            #            continue
            if maintype == 'image':
                #            with open(path, 'rb') as fp:
                #                msg = MIMEImage(fp.read(), _subtype=subtype)
                continue
            elif maintype == 'audio':
                #            with open(path, 'rb') as fp:
                #                msg = MIMEAudio(fp.read(), _subtype=subtype)
                continue
            else:
                with open(path, 'rb') as fp:
                    msg = MIMEBase(maintype, subtype, charset='gb18030')
                    msg.set_payload(fp.read())
                # Encode the payload using Base64
                encoders.encode_base64(msg)
            # Set the filename parameter
            msg.add_header('Content-Disposition', 'attachment', filename=filename)
            outer.attach(msg)

    # Now send or store the message
    #    composed = outer.as_string()
    server = smtplib.SMTP_SSL(mail_service,465)
    server.login("litao@igengmei.com","Lemo1995")
    server.send_message(outer)
    server.quit()



if __name__ == '__main__':
    send_file_email("",'2019-02-21',
    sender="litao@igengmei.com",email_group=["litao@igengmei.com"],email_msg_body_str="test",title_str="test",cc_group=["litao@igengmei.com"])