Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add option to automatically attach inline images in HTML email messages
Added 'inline_images' and 'image_path' preferences. Changed 'attach' function to return Content ID with new '_get_content_id' function when disposition is 'inline'. Changed '_build_message' function to automatically attach inline images if preferences are set.
  • Loading branch information
barrymieny committed Sep 12, 2012
1 parent 88e3857 commit fa1786c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
54 changes: 50 additions & 4 deletions system/libraries/Email.php
Expand Up @@ -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 = '';
Expand All @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

// --------------------------------------------------------------------
Expand Down Expand Up @@ -575,6 +589,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
*
Expand Down Expand Up @@ -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' :
Expand Down Expand Up @@ -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));
}
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/libraries/email.rst
Expand Up @@ -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
Expand Down

0 comments on commit fa1786c

Please sign in to comment.