var replaceBase64Images = require('./replaceBase64Images');
var crypto = require('crypto');

var plugin = function(mail, done) {
  if (!mail || !mail.data || !mail.data.html) {
    return done();
  }

  mail.resolveContent(mail.data, 'html', function(err, html) {
    if (err) return done(err);

    var attachments = {};

    html = replaceBase64Images(html, function(mimeType, base64) {
      if (attachments[base64]) return attachments[base64].cid;

      var randomCid = crypto.randomBytes(8).toString('hex');

      attachments[base64] = {
        contentType: mimeType,
        cid: randomCid,
        content: base64,
        encoding: 'base64',
        contentDisposition: 'inline'
      };
      return randomCid;
    });

    mail.data.html = html;

    if (!mail.data.attachments) mail.data.attachments = [];

    Object.keys(attachments).forEach(function(cid) {
      mail.data.attachments.push(attachments[cid]);
    });

    done();
  });
};

module.exports = plugin;

@@ -98,16 +98,72 @@
 plugin =
+ function(options) %7B%0A  options = options %7C%7C %7B%7D;%0A  return
  functio
@@ -178,16 +178,18 @@
 done) %7B%0A
+  
   if (!m
@@ -228,24 +228,26 @@
 html) %7B%0A    
+  
 return done(
@@ -255,11 +255,15 @@
 ;%0A  
+ 
+ 
 %7D%0A%0A
+  
   ma
@@ -321,24 +321,26 @@
 html) %7B%0A    
+  
 if (err) ret
@@ -355,24 +355,26 @@
 (err);%0A%0A    
+  
 var attachme
@@ -388,16 +388,18 @@
 %7D;%0A%0A    
+ 
+ 
 html = r
@@ -454,24 +454,26 @@
 64) %7B%0A      
+  
 if (attachme
@@ -518,16 +518,18 @@
 %5D.cid;%0A%0A
+  
       va
@@ -541,16 +541,44 @@
 domCid =
+ (options.cidPrefix %7C%7C '') +
  crypto.
@@ -606,24 +606,26 @@
 ng('hex');%0A%0A
+  
       attach
@@ -646,24 +646,26 @@
 = %7B%0A        
+  
 contentType:
@@ -683,16 +683,18 @@
         
+  
 cid: ran
@@ -705,24 +705,26 @@
 id,%0A        
+ 
+ 
 content: bas
@@ -728,16 +728,18 @@
 base64,%0A
+  
         
@@ -762,24 +762,26 @@
 4',%0A        
+ 
+ 
 contentDispo
@@ -803,16 +803,18 @@
 '%0A      
+  
 %7D;%0A     
@@ -810,24 +810,26 @@
    %7D;%0A      
+ 
+ 
 return rando
@@ -834,24 +834,26 @@
 domCid;%0A    
+  
 %7D);%0A%0A    mai
@@ -845,24 +845,26 @@
    %7D);%0A%0A    
+ 
+ 
 mail.data.ht
@@ -875,16 +875,18 @@
  html;%0A%0A
+  
     if (
@@ -938,16 +938,18 @@
  = %5B%5D;%0A%0A
+  
     Obje
@@ -995,24 +995,26 @@
 id) %7B%0A      
+  
 mail.data.at
@@ -1047,16 +1047,18 @@
 %5Bcid%5D);%0A
+  
     %7D);%0A
@@ -1062,24 +1062,26 @@
 );%0A%0A    
+  
 done();%0A
   %7D);%0A%7D;
@@ -1072,22 +1072,28 @@
 done();%0A
+  
   %7D);%0A
+  %7D%0A
 %7D;%0A%0Amodu

add ability to set CID prefix which might be useful for email parsing, usage: smtpTransport.use('compile', inlineBase64({ cidPrefix: 'cidPrefix_' }));