Busindre » Blog Archive » Enviar emails con Python

Enviar emails con Python

February 7th, 2007 by Busindre

Para poder enviar emails desde python, se requiere tener instalado sendmail y escribir un script muy simple que nos permitirá con solo ejecutarlo, el poder enviar correos electronicos. Cogemos el editor y creamos el script.py con el siguiente contenido, cada uno que de el valor a las variables emisor, receptor y mail que desee:

------------------------

import smtplib

def enviar():
  servidor.sendmail(emisor, receptor, mail)
if __name__=='__main__':
  servidor = smtplib.SMTP('127.0.0.1')
servidor.set_debuglevel(0)
print servidor.ehlo()
emisor = "busi@busindre.org"
receptor = "pivon@tangas.com"
mail = "Este es el cuerpo del mensaje ;D"
enviar()
servidor.quit()

------------------------

Ahora simplemente ejecutamos:

$ python script.py

Ahora si queremos ver el proceso de ejecución entero del script cambiamos el 0 por 1, quedando de esta forma:

servidor.set_debuglevel(1)

Y nos mostrara más información:

$ python script.py
send: 'ehlo [127.0.0.1]\r\n'
reply: '250-busipc\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 10240000\r\n'
reply: '250-ETRN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-8BITMIME\r\n'
reply: '250 DSN\r\n'
reply: retcode (250); Msg: busipc
PIPELINING
SIZE 10240000
ETRN
ENHANCEDSTATUSCODES
8BITMIME
DSN
(250, 'busipc\nPIPELINING\nSIZE 10240000\nETRN\nENHANCEDSTATUSCODES\n8BITMIME\nDSN')
send: 'mail FROM: size=25\r\n'
reply: '250 2.1.0 Ok\r\n'
reply: retcode (250); Msg: 2.1.0 Ok
send: 'rcpt TO: \r\n'
reply: '250 2.1.5 Ok\r\n'
reply: retcode (250); Msg: 2.1.5 Ok
send: 'data\r\n'
reply: '354 End data with .\r\n'
reply: retcode (354); Msg: End data with .
data: (354, 'End data with .')
send: 'Este es el cuerpo del mensaje ;D.\r\n'
reply: '250 2.0.0 Ok: queued as 93CFB4463F\r\n'
reply: retcode (250); Msg: 2.0.0 Ok: queued as 93CFB4463F
data: (250, '2.0.0 Ok: queued as 93CFB4463F')
send: 'quit\r\n'
reply: '221 2.0.0 Bye\r\n'
reply: retcode (221); Msg: 2.0.0 Bye

Posted in Programas |

One Response

  1. Tux Huellas » Enviar emails con Python Says:

    [...] http://www.busindre.com/enviar-emails-con-python/ Igual tambien te interesan.... Compilar aplicaciones en LinuxContactarTu correo del [...]

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.