Esempio n. 1
0
def send(to =None, from_ ='*****@*****.**',
         att_path =None, att_file=None,subject =None,
         preambule ='Preambule', body =None, *agrs,**kw):


    # If the given recipient is empty, so take the default
    # recipient in  this current module
    
    to  = (to or recipients)


    # default  body
    td = datetime.now()
    body = body or \
    """
    'Bonjour Yatma <br/>
    Merci de deposer ces fichiers dans le FTP du client, le %s .<br/>
    L \'Informatique '
    """%str(td)

    # default subject
    subject = subject or\
    """
    FTP du %s
    """%str(td)

    
    # body of messages
    part1 = MIMEText(body, 'html')
    
    outer = MIMEMultipart()
    outer['To']      = ','.join(to)
    outer['From']    =from_
    outer['Subject'] =subject
    outer.preambule  =preambule       
	
    #
    files, is_att = [] , False
    if att_file and att_path:
	raise ValueError(
            """
            send mail accepte soit att_file or att_path
            pas les deux!
            """)

    if att_file:
	files =[att_file]
    if att_path:
	for f in os.listdir(att_path):
	    f = os.path.join(att_path, f)
	    print  f
	    if os.path.isfile(f):
	        files.append(f)
    # The email should  contain files attachments
    if len(files)>0:
	is_att= True
    #if is_att
    if is_att:
        for f in files:
            # guess the file type, see ptyhon 's doc 
            ctype,  encoding = mimetypes.guess_type(f)
            print ctype, encoding
            # if ctype is None and encoding is not None
            if ctype is None and encoding is not None:
                    ctype  ='application/octet-stream'
            if ctype is None:
                     ctype  ='application/octet-stream'   
            maintype, subtype = ctype.split('/', 1)
            msg =  mime_class(maintype,subtype, f)
            outer.attach(msg)
    outer.attach(part1)
    # send message now
    print  >> sys.stdout ,'Sending message to %r'%(to,)
    s = smtplib.SMTP(SMTP_PCCI_HOST,SMTP_PCCI_PORT)
    s.ehlo()
    # le serveur de messagerie ne supporte pas
    # ttls pour le moment on 
    #s.starttls()
    s.ehlo()
    #s.login(SMTP_PCCI_USER, SMTP_PCCI_PWD)
    s.sendmail(from_, to , outer.as_string())
    s.quit()
    print  >> sys.stdout ,'Terminate to send message'