libmail, es una clase que hice, para que el envio de emails, sea mas cómodo y fácil.
Liberado bajo la
siguiente licencia
Código python:
Ver original#!/usr/bin/env python
#coding: UTF-8
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
class Mail:
def __init__(self, host='localhost', user='', passwd='', name='', organization=''):
self.host = host
self.user = user
self.passwd = passwd
self.name = name
self.organization = organization
self.to_email = []
self.cc_email = []
self.bcc_email = []
self.mensaje = MIMEMultipart()
self.name = name
self.organization = organization
self.__set_headers()
def __set_headers(self):
self.mensaje.add_header("Content-Disposition", "inline")
self.mensaje.add_header("Reply-To", "%s <%s>" % (self.name, self.user))
self.mensaje.add_header("Return-Path", "%s <%s>" % (self.name, self.user))
self.mensaje.add_header("From", "%s <%s>" % (self.name, self.user))
self.mensaje.add_header("Organization", "%s" % (self.organization))
def set_subject(self, subject):
self.mensaje['Subject'] = subject
def add_to(self, emails):
self.to_email.extend(emails)
def add_cc(self, emails):
self.cc_email.extend(emails)
def add_bcc(self, emails):
self.bcc_email.extend(emails)
def clean_emails(self):
self.to_email = []
self.cc_email = []
self.bcc_email = []
def get_message(self, filename, coding="utf8"):
try:
f = open(filename, "r")
body = f.read()
except IOError, e:
print e
else:
f.close()
body = unicode(body, coding)
body = body.encode(coding)
text = MIMEText(body, 'plain', coding)
self.mensaje.attach(text)
def attach(self, filename, mimetype='application'):
try:
f = open(filename, "rb")
if mimetype == 'image':
contenido = MIMEImage(f.read())
else:
contenido = MIMEApplication(f.read())
contenido.add_header('Content-Disposition',
'attachment; filename = "%s"' % (filename.split('/')[-1]))
self.mensaje.attach(contenido)
except IOError, e:
print e
else:
f.close()
def get_email_list(self):
email_list = []
if self.to_email:
email_list.extend(self.to_email)
else:
email_list.append('')
if self.cc_email:
email_list.extend(self.cc_email)
else:
email_list.append('')
email_list.extend(self.bcc_email)
return email_list
def connect(self):
self.mailServer = smtplib.SMTP(self.host, 25)
self.mailServer.login(self.user, self.passwd)
def send(self):
email_list = self.get_email_list()
result = self.mailServer.sendmail(self.user, email_list,
self.mensaje.as_string())
return result
def quit(self):
self.mailServer.quit()
Ejemplo de uso:
Código python:
Ver original#!/usr/bin/env python
#coding: UTF-8
import libmail
host = 'mail.midominio.com'
passwd = 'password'
name = 'Nombre del Usuario'
organization = "Organizacion"
mail = libmail.Mail(host, user, passwd, name, organization)
mail.set_subject('Asunto del correo')
mail.attach('MSWord.doc')
mail.attach('MSExcel.xls')
mail.attach('Imagen.jpg', 'image')
mail.get_message('mensaje.txt')
mail.connect()
mail.
add_to("[email protected]".
split(", ")) #Importante!, se envían listas como parámetrosmail.send()
mail.quit()
Advertencia, esto no es para hacer spam!!!