Guest User

Untitled

a guest
Jan 23rd, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1.  def callback_email(self, ch, method, properties, body):
  2.  
  3.     self.log_ ( " [x] Received %r" % (body,) )
  4.     data = json.loads(body)
  5.     grupo = data['grupo']
  6.     template = data['template']
  7.     words = data['words']
  8.     lang = data['lang']
  9.     to = data['to']
  10.     name = data['name']
  11.  
  12.     # Prepare Mail
  13.  
  14.     fp = open('templates/%s.body.%s.html' % (template, lang), 'r')
  15.     body = replace_all(fp.read(), words)
  16.     fp.close()
  17.     fp = open('templates/%s.subject.%s.txt' % (template, lang), 'r')
  18.     # No anda
  19.     #subject = fp.read()
  20.     # Anda
  21.     subject = "test massage"
  22.     fp.close()
  23.  
  24.     myre = re.compile(r"cid:[a-zA-Z0-9_-]*")
  25.     images = [ x.replace('cid:','') for x in  myre.findall(body) ]
  26.  
  27.     # Prepare Mail
  28.  
  29.     msgRoot = MIMEMultipart('related')
  30.     msgRoot['Subject'] = subject
  31.     msgRoot['From'] = "GocloudOne Plataform <ibrick8@gmail.com>"
  32.     msgRoot['To'] = "%s <%s>" % (name, to)
  33.     msgRoot.preamble = 'This is a multi-part message in MIME format.'
  34.  
  35.     msgAlternative = MIMEMultipart('alternative')
  36.     msgRoot.attach(msgAlternative)
  37.  
  38.     msgText = MIMEText('The HTML text cannot be shown')
  39.     msgAlternative.attach(msgText)
  40.  
  41.     msgText = MIMEText(body, 'html')
  42.     msgAlternative.attach(msgText)
  43.  
  44.     for image in images:
  45.  
  46.       fp = open("templates/%s.jpg" % image, 'rb')
  47.       msgImage = MIMEImage(fp.read())
  48.       fp.close()
  49.       msgImage.add_header('Content-ID', "<%s>" % image)
  50.       msgRoot.attach(msgImage)
  51.  
  52.     # Send Mail
  53.  
  54.     server = smtplib.SMTP('smtp.gmail.com:587')
  55.     server.ehlo()
  56.     server.starttls()
  57.     server.login("ibrick8@gmail.com", "xxxxxxx");
  58.     server.sendmail("ibrick8@gmail.com", to,  msgRoot.as_string());
  59.     server.close()
Add Comment
Please, Sign In to add comment