4 Replies Latest reply on Mar 11, 2013 10:10 PM by pawans

    How to include inline images on "send as email" for Blog Posts

    mark.cablayan

      We needed to implement a way to include inline images on the "send as email" functionality and I thought it might be helpful for anyone trying to do the same.

       

      I extended the com.pega.mesh.community.action.SendContentNotification class and added the following code block to the sendContentNotification(String recipientName, String recipientEmail) method.

       

      if(jiveObject.getObjectType() == JiveConstants.BLOGPOST) {
                  BlogPost bg = (BlogPost)jiveObject;
                  JiveIterator<Image> itr = bg.getImages();
                  while(itr.hasNext()) {
                      Image i = itr.next();
                      String imageName = i.getName().replace(" ","");                
                      try {
                          message.addAttachment(new InputStreamDataSource(imageName,new MimetypesFileTypeMap().getContentType(attachmentNameFrom(i)),i.getData()));
                          
                          String regex = "<a href=\".*" + i.getName().replace(" ", "\\+").replace(".","\\.") + ".*?<\\/a>";
                          String imgTag = "<img src=\"" + imageName +"\">";
                          objectHtmlBody = objectHtmlBody.replaceAll(regex, imgTag);
         
                      } catch (IOException e) {
                          log.error(e.toString());
                      }
                  }
      }
      

       

      This adds the images to the email as attachments and adds "img" references in place of the original hrefs.

      The admin console restricts our image sizes, so we decided not to include any size restrictions in the actual code, however, in can be added rather easily.

       

      -Mark