Merge pull request #2807 from melounek/develop

Added Email::attach_cid() returning CID which enables to embed an attachment to html
diff --git a/system/libraries/Email.php b/system/libraries/Email.php
index efdbfd7..a41884f 100644
--- a/system/libraries/Email.php
+++ b/system/libraries/Email.php
@@ -718,14 +718,67 @@
 	 */
 	public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
 	{
+		if ($mime === '')
+		{
+			if ( ! file_exists($filename))
+			{
+				$this->_set_error_message('lang:email_attachment_missing', $filename);
+				return FALSE;
+			}
+			
+			if ( ! $fp = fopen($filename, FOPEN_READ))
+			{
+				$this->_set_error_message('lang:email_attachment_unreadable', $filename);
+				return FALSE;
+			}
+			
+			$file_content = stream_get_contents($fp);
+			$mime = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
+			fclose($fp);
+		}
+		else
+		{
+			$file_content =& $filename; // buffered file
+		}
+		
 		$this->_attachments[] = array(
 			'name'		=> array($filename, $newname),
 			'disposition'	=> empty($disposition) ? 'attachment' : $disposition,  // Can also be 'inline'  Not sure if it matters
-			'type' 		=> $mime
+			'type'		=> $mime,
+			'content'	=> chunk_split(base64_encode($file_content))
 		);
 
 		return $this;
 	}
+	
+	// --------------------------------------------------------------------
+        
+	/**
+	 * Set and return id of attachment 
+	 * 
+	 * useful for attached inline pictures
+	 *
+	 * @param	string	$filename
+	 * @return	string
+	 */
+	public function attach_cid($filename)
+	{
+		if ($this->multipart !== 'related')
+		{
+			$this->multipart = 'related'; // Thunderbird need this for inline images
+		}
+		
+		for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
+		{
+			if ($this->_attachments[$i]['name'][0] === $filename)
+			{
+				$this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@');
+				return $this->_attachments[$i]['cid'];
+			}
+		}
+		
+		return FALSE;
+	}
 
 	// --------------------------------------------------------------------
 
@@ -1361,41 +1414,15 @@
 			$filename = $this->_attachments[$i]['name'][0];
 			$basename = ($this->_attachments[$i]['name'][1] === NULL)
 				? basename($filename) : $this->_attachments[$i]['name'][1];
-			$ctype = $this->_attachments[$i]['type'];
-			$file_content = '';
-
-			if ($ctype === '')
-			{
-				if ( ! file_exists($filename))
-				{
-					$this->_set_error_message('lang:email_attachment_missing', $filename);
-					return FALSE;
-				}
-
-				$file = filesize($filename) +1;
-
-				if ( ! $fp = fopen($filename, FOPEN_READ))
-				{
-					$this->_set_error_message('lang:email_attachment_unreadable', $filename);
-					return FALSE;
-				}
-
-				$ctype = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
-				$file_content = fread($fp, $file);
-				fclose($fp);
-			}
-			else
-			{
-				$file_content =& $this->_attachments[$i]['name'][0];
-			}
 
 			$attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
-				.'Content-type: '.$ctype.'; '
+				.'Content-type: '.$this->_attachments[$i]['type'].'; '
 				.'name="'.$basename.'"'.$this->newline
 				.'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
-				.'Content-Transfer-Encoding: base64'.$this->newline;
+				.'Content-Transfer-Encoding: base64'.$this->newline
+				.(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline);
 
-			$attachment[$z++] = chunk_split(base64_encode($file_content));
+			$attachment[$z++] = $this->_attachments[$i]['content'];
 		}
 
 		$body .= implode($this->newline, $attachment).$this->newline.'--'.$this->_atc_boundary.'--';
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index 0488d9d..34eff5d 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -334,6 +334,7 @@
 
       -  Added custom filename to ``Email::attach()`` as ``$this->email->attach($filename, $disposition, $newname)``.
       -  Added possibility to send attachment as buffer string in ``Email::attach()`` as ``$this->email->attach($buffer, $disposition, $newname, $mime)``.
+      -  Added method ``Email::attach_cid()`` returning CID which enables to embed an attachment to html.
       -  Added dsn (delivery status notification) option.
       -  Renamed method _set_header() to set_header() and made it public to enable adding custom headers in the :doc:`Email Library <libraries/email>`.
       -  Successfully sent emails will automatically clear the parameters.
diff --git a/user_guide_src/source/libraries/email.rst b/user_guide_src/source/libraries/email.rst
index 39629ec..274d88d 100644
--- a/user_guide_src/source/libraries/email.rst
+++ b/user_guide_src/source/libraries/email.rst
@@ -247,8 +247,8 @@
 ----------------------
 
 Enables you to send an attachment. Put the file path/name in the first
-parameter. Note: Use a file path, not a URL. For multiple attachments
-use the method multiple times. For example::
+parameter. For multiple attachments use the method multiple times.
+For example::
 
 	$this->email->attach('/path/to/photo1.jpg');
 	$this->email->attach('/path/to/photo2.jpg');
@@ -269,6 +269,26 @@
 
 	$this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
 
+$this->email->attach_cid()
+ --------------------------
+ 
+ Returns CID which enables to embed an attachment to html. First parameter 
+ must be attached file.
+ 
+ ::
+ 
+	$filename = '/img/photo1.jpg';
+	$this->email->attach($filename);
+	foreach ($list as $address)
+	{
+		$this->email->to($address);
+		$cid = $this->email->attach_cid($filename);
+		$this->email->message('<img src='cid:". $cid ."' alt="photo1" />');
+		$this->email->send();
+	}
+ 
+ CID for each Email have to be create again to be unique.
+
 $this->email->print_debugger()
 ------------------------------