programacion:tutoriales:python:envio_smtp

No renderer 'odt' found for mode 'odt'

Uso de smtplib en Python

Minireceta : Ejemplo de conexión a servidor SMTP: muestra cómo enviar correos que incluyan, además, direcciones CC y BCC.

from smtplib import SMTP
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
 
to = 'direccion@destino.com'
cc = 'direccion_cc@destino.com'
bcc = 'direccion_bcc@destino.com'
 
msg = MIMEMultipart()
msg['To'] = to
msg['Cc'] = cc
msg['From'] = 'direccion@origen.com'
msg['Subject'] = 'Asunto de prueba'
text = MIMEText('Texto del mensaje')
text.add_header("Content-Disposition", "inline")
msg.attach(text)
 
s = SMTP('smtp.servidor.com')
s.sendmail(msg['From'], [to, cc, bcc], msg.as_string())
s.quit() 

Ejemplo 2:

import smtplib
 
def prompt(prompt):
    return raw_input(prompt).strip()
 
fromaddr = prompt("From: ")
toaddrs  = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"
 
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
       % (fromaddr, ", ".join(toaddrs)))
while 1:
    try:
        line = raw_input()
    except EOFError:
        break
    if not line:
        break
    msg = msg + line
 
print "Message length is " + repr(len(msg))
 
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()


<Volver a la sección de Tutoriales Python>

  • programacion/tutoriales/python/envio_smtp.txt
  • Última modificación: 25-09-2009 10:39
  • por sromero