Ejemplo n.º 1
0
 def test_html_email_with_images(self):
     msg = MIMEMultipart(
         _subparts=[
             MIMEMultipart(
                 'alternative',
                 _subparts=[
                     MIMEText('This is a test message'),
                     MIMEText('This is a <em>test</em> message', 'html')
                 ])
         ])
     with open(pkg_resources.resource_filename(
             'forgediscussion', 'tests/data/python-logo.png'), 'rb') as fp:
         img = MIMEImage(fp.read())
         img.add_header('Content-Disposition', 'attachment',
                        filename='python-logo.png')
         msg.attach(img)
     self._post_email(
         self.email_address,
         [self.forum.email_address],
         'Test Simple Thread',
         msg)
     r = self.app.get('/p/test/discussion/testforum/')
     assert 'Test Simple Thread' in str(r)
     assert len(r.html.findAll('tr')) == 2
     href = r.html.findAll('tr')[1].find('a')['href']
     r = self.app.get(href)
     assert 'alternate' in str(r)
     assert 'python-logo.png' in str(r)
Ejemplo n.º 2
0
 def AttacheImagesIncluses(self, email=None):
     index = 0
     for img in self.images:
         fp = open(img, 'rb')
         msgImage = MIMEImage(fp.read())
         fp.close()
         msgImage.add_header('Content-ID', '<image%d>' % index)
         msgImage.add_header('Content-Disposition', 'inline', filename=img)
         email.attach(msgImage)
         index += 1
Ejemplo n.º 3
0
 def AttacheFichiersJoints(self, email=None):
     for fichier in self.fichiers:
         """Guess the content type based on the file's extension. Encoding
         will be ignored, altough we should check for simple things like
         gzip'd or compressed files."""
         ctype, encoding = mimetypes.guess_type(fichier)
         if ctype is None or encoding is not None:
             # No guess could be made, or the file is encoded (compresses), so
             # use a generic bag-of-bits type.
             ctype = 'application/octet-stream'
         maintype, subtype = ctype.split('/', 1)
         if maintype == 'text':
             fp = open(fichier)
             # Note : we should handle calculating the charset
             part = MIMEText(fp.read(), _subtype=subtype)
             fp.close()
         elif maintype == 'image':
             fp = open(fichier, 'rb')
             part = MIMEImage(fp.read(), _subtype=subtype)
             fp.close()
         else:
             fp = open(fichier, 'rb')
             part = MIMEBase(maintype, subtype)
             part.set_payload(fp.read())
             fp.close()
             # Encode the payload using Base64
             encoders.encode_base64(part)
         # Set the filename parameter
         nomFichier = os.path.basename(fichier)
         if type(nomFichier) == six.text_type:
             nomFichier = FonctionsPerso.Supprime_accent(nomFichier)
         # changement cosmetique pour ajouter les guillements autour du filename
         part.add_header('Content-Disposition',
                         "attachment; filename=\"%s\"" % nomFichier)
         email.attach(part)
Ejemplo n.º 4
0
 def AttacheImagesIncluses(self, email=None):
     index = 0
     for img in self.images:
         fp = open(img, 'rb')
         msgImage = MIMEImage(fp.read())
         fp.close()
         msgImage.add_header('Content-ID', '<image%d>' % index)
         msgImage.add_header('Content-Disposition', 'inline', filename=img)
         email.attach(msgImage)
         index += 1
Ejemplo n.º 5
0
def create_message(recipient, image, size, sender, reply_to):
    msg = MIMEMultipart('related')
    msg['from'] = Header(sender, 'utf-8')
    msg['to'] = Header(recipient, 'utf-8')
    msg['subject'] = Header(EMAIL_SUBJECT, 'utf-8')
    if reply_to:
        msg['reply-to'] = Header(reply_to, 'utf-8')

    template = EMAIL_TEMPLATE
    template = template.replace('${width}', str(size[0]))
    template = template.replace('${height}', str(size[1]))

    text = MIMEText(template, 'html', 'utf-8')
    msg.attach(text)

    with open(image, 'rb') as fp:
        img = MIMEImage(fp.read())
    img.add_header('Content-ID', 'image')
    img.add_header('Content-Disposition', 'inline', filename=basename(image))
    msg.attach(img)

    return msg