Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add option to automatically attach inline images in HTML email messages #990

Closed
wants to merge 3 commits into
from
View
@@ -63,6 +63,8 @@ class CI_Email {
public $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
public $bcc_batch_mode = FALSE; // TRUE/FALSE - Turns on/off Bcc batch feature
public $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
+ public $inline_images = FALSE; // TRUE/FALSE Turns on/off attaching inline images
+ public $image_path = ''; // Path to inline images folder
protected $_safe_mode = FALSE;
protected $_subject = '';
@@ -84,6 +86,7 @@ class CI_Email {
protected $_attach_name = array();
protected $_attach_type = array();
protected $_attach_disp = array();
+ protected $_attach_cid = array();
protected $_protocols = array('mail', 'sendmail', 'smtp');
protected $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
protected $_bit_depths = array('7bit', '8bit');
@@ -174,6 +177,7 @@ public function clear($clear_attachments = FALSE)
$this->_attach_name = array();
$this->_attach_type = array();
$this->_attach_disp = array();
+ $this->_attach_cid = array();
}
return $this;
@@ -410,9 +414,19 @@ public function message($body)
public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
{
$this->_attach_name[] = array($filename, $newname);
+ $this->_attach_type[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
$this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
- $this->_attach_type[] = $mime;
- return $this;
+ if ($disposition === 'inline')
+ {
+ $cid = $this->_get_content_id();
+ $this->_attach_cid[] = $cid;
+ return "cid:".$cid;
+ }
+ else
+ {
+ $this->_attach_cid[] = NULL;
+ return $this;
+ }
}
// --------------------------------------------------------------------
@@ -576,6 +590,20 @@ protected function _get_message_id()
// --------------------------------------------------------------------
/**
+ * Get a Content ID
+ *
+ * @return string
+ */
+ protected function _get_content_id()
+ {
+ $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
+
+ return uniqid('').strstr($from, '@');
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Get Mail Protocol
*
* @param bool
@@ -928,6 +956,23 @@ protected function _build_message()
$hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
$body = '';
+ if ($this->inline_images === TRUE && $this->mailtype === 'html')
+ {
+ preg_match_all('/<.*?(src|background)\=([\x22\x27])?([^\s\x22\x27]+)([\x22\x27])?.*?>/i', $this->_body, $matched_images, PREG_PATTERN_ORDER);
+ $embed_images = array_unique($matched_images[2]);
+ if (isset($embed_images))
+ {
+ foreach ($embed_images as $filename)
+ {
+ if (strpos($filename, '://') === FALSE)
+ {
+ $cid = $this->attach($this->image_path.$filename, "inline");
+ $this->_body = str_replace($filename, $cid, $this->_body);
+ }
+ }
+ }
+ }
+
switch ($this->_get_content_type())
{
case 'plain' :
@@ -1069,10 +1114,11 @@ protected function _build_message()
}
$attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
- .'Content-type: '.$ctype.'; '
+ .'Content-Type: '.$ctype.'; '
.'name="'.$basename.'"'.$this->newline
.'Content-Disposition: '.$this->_attach_disp[$i].';'.$this->newline
- .'Content-Transfer-Encoding: base64'.$this->newline;
+ .'Content-Transfer-Encoding: base64'.$this->newline
+ .(($this->_attach_cid[$i]) ? 'Content-ID: <'.$this->_attach_cid[$i].'>'.$this->newline : '');
$attachment[$z++] = chunk_split(base64_encode($file_content));
}
@@ -105,6 +105,8 @@ Preference Default Value Options Descript
**bcc_batch_mode** FALSE TRUE or FALSE (boolean) Enable BCC Batch Mode.
**bcc_batch_size** 200 None Number of emails in each BCC batch.
**dsn** FALSE TRUE or FALSE (boolean) Enable notify message from server
+**inline_images** FALSE TRUE or FALSE (boolean) Enable automatic attaching of inline images in HTML email.
+**image_path** No Default None The base path for inline images.
=================== ====================== ============================ =======================================================================
Email Function Reference