quinta-feira, 6 de outubro de 2011

smtplib - Enviando E-mails com anexo

def email():
    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email.Utils import formatdate
    from email import Encoders
    import os
    user = "user@email.com" 
    password = "password" 
    subject = "email teste" 
    fromaddr = 'from@email.com' 
    toaddrs = 'to@email.com' 
    msg = MIMEMultipart()
    msg['Date'] = formatdate( localtime = True )
    
    msg['Subject'] = 'subject'

    msg.attach( MIMEText( 'oi' ) )
    part = MIMEBase( 'application', "octet-stream" )
    open( 'teste.txt', 'w' ).write( 'teste')
    part.set_payload( open( 'teste.txt', "rb" ).read() )
    Encoders.encode_base64( part )
    part.add_header( 'Content-Disposition', 'attachment; filename="teste.txt"' )
    msg.attach( part )
        
    servidor = smtplib.SMTP( 'smtp.domain:port' )
    servidor.ehlo()  
    servidor.starttls()  
    servidor.ehlo()  
    servidor.login( user, password )  
    servidor.sendmail( fromaddr, toaddrs, msg.as_string() )
Agistech.com.br

Nenhum comentário:

Postar um comentário