document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. ##################################################################################
  4. #       1)Configure the eMail adress
  5. #       mail = EMail()
  6. #       mail.confMail(eMail-address, password, smtp-server, smtp-port)
  7. #       eg: mail.confMail(\'mail@mail.com\', \'password\', \'smtp.mail.com\', 25)
  8. #       2)Create a new eMail
  9. #       mail.newMail(subject)
  10. #       eg: mail.newMail(\'Hello!\')
  11. #       3)Add text and/or attachments
  12. #       mail.addText(text)
  13. #       addAttachment(path_to_file)
  14. #       4)Send eMail
  15. #       mail.send(receiver)
  16. #       eg: mail.send(\'mail@gmail.com\') it can be sent multiple times to different adresses
  17. #       mail.send() returns True if the eMail was sent successfully and False otherwise
  18. ##################################################################################
  19. import os, smtplib
  20. from email.MIMEMultipart import MIMEMultipart
  21. from email.MIMEText import MIMEText
  22. from email.mime.application import MIMEApplication
  23.  
  24.  
  25. class EMail(object):
  26.     def __init__(self, ctimes=20):
  27.         self.ctimes = ctimes
  28.         self.connected = False
  29.  
  30.     def confMail(self, fr, psw, smtp, port):
  31.         self.fr = fr
  32.         self.psw = psw
  33.         self.smtp = smtp
  34.         self.port = port
  35.  
  36.         #Conexion
  37.         tryMail = True
  38.         tries = 0
  39.         while tryMail:
  40.             try:
  41.                 self.mailServer = mailServer = smtplib.SMTP(self.smtp, self.port)
  42.                 mailServer.ehlo()
  43.                 mailServer.starttls()
  44.                 mailServer.ehlo()
  45.                 acceptance = mailServer.login(self.fr, self.psw)
  46.                 if acceptance[0] == 235:
  47.                     tryMail = False
  48.                     self.connected = True
  49.                 else:
  50.                     tries += 1
  51.                     if tries >= self.ctimes:
  52.                         tryMail = False
  53.                         self.connected = False
  54.             except:
  55.                 tries += 1
  56.                 if tries >= self.ctimes:
  57.                     tryMail = False
  58.                     self.connected = False
  59.    
  60.     def newMail(self, subject):
  61.         self.msj = MIMEMultipart()
  62.         self.msj[\'From\'] = self.fr
  63.         self.msj[\'Subject\'] = subject
  64.    
  65.     def addText(self, text):
  66.         self.msj.attach(MIMEText(text))
  67.    
  68.     def addAttachment(self, arch):
  69.         part = MIMEApplication(open(arch,"rb").read())
  70.         part.add_header(\'Content-Disposition\', \'attachment\', filename=arch.replace(\'\\\\\', \'/\').split(\'/\')[-1])
  71.         self.msj.attach(part)
  72.    
  73.     def send(self, to):
  74.         self.to = to
  75.         self.msj[\'To\'] = to
  76.        
  77.         #Enviar mensaje
  78.         if self.connected:
  79.             tryMail = True
  80.             tries = 0
  81.             while tryMail:
  82.                 try:
  83.                     # Enviar
  84.                     self.mailServer.sendmail(self.fr, self.to, self.msj.as_string())
  85.                     sent = True
  86.                     tryMail = False
  87.                 except:
  88.                     tries += 1
  89.                     if tries > self.ctimes:
  90.                         tryMail = False
  91.                         sent = False
  92.         else:
  93.             sent = False
  94.         return sent
  95.    
  96.     def close(self):
  97.         # Cerrar la conexion
  98.         self.mailServer.close()
');