Permalink
Browse files

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...
1 parent ed6485b commit 9260cce7f612ec76c64bb2d6b00bf05f663d4d13 @barrymieny barrymieny committed Jan 27, 2012
Showing with 62 additions and 10 deletions.
  1. +55 −6 system/libraries/Email.php
  2. +7 −4 user_guide_src/source/libraries/email.rst
View
61 system/libraries/Email.php
@@ -55,15 +55,17 @@ class CI_Email {
public $charset = "utf-8"; // Default char set: iso-8859-1 or us-ascii
public $multipart = "mixed"; // "mixed" (in the body) or "related" (separate)
public $alt_message = ''; // Alternative message for HTML emails
- public $validate = FALSE; // TRUE/FALSE. Enables email validation
+ public $validate = FALSE; // TRUE/FALSE Enables email validation
public $priority = "3"; // Default priority (1 - 5)
public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
public $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
// even on the receiving end think they need to muck with CRLFs, so using "\n", while
// distasteful, is the only thing that seems to work for all environments.
- public $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
+ 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 = ''; // TRUE/FALSE Turns on/off attaching inline images
private $_safe_mode = FALSE;
private $_subject = "";
private $_body = "";
@@ -84,6 +86,7 @@ class CI_Email {
private $_attach_name = array();
private $_attach_type = array();
private $_attach_disp = array();
+ private $_attach_cid = array();
private $_protocols = array('mail', 'sendmail', 'smtp');
private $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
private $_bit_depths = array('7bit', '8bit');
@@ -175,6 +178,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;
@@ -423,7 +427,17 @@ public function attach($filename, $disposition = '', $newname = NULL)
$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
- return $this;
+ if ($disposition === 'inline')
+ {
+ $cid = $this->_get_content_id();
+ $this->_attach_cid[] = $cid;
+ return "cid:".$cid;
+ }
+ else
+ {
+ $this->_attach_cid[] = NULL;
+ return $this;
+ }
}
// --------------------------------------------------------------------
@@ -604,7 +618,22 @@ protected function _get_message_id()
{
$from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
- return "<".uniqid('').strstr($from, '@').">";
+ return "<".uniqid('').strstr($from, '@').">";
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Get a Content ID
+ *
+ * @access protected
+ * @return string
+ */
+ protected function _get_content_id()
+ {
+ $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
+
+ return uniqid('').strstr($from, '@');
}
// --------------------------------------------------------------------
@@ -980,6 +1009,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)\=\"(.*)\".*?>/', $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' :
@@ -1100,11 +1146,14 @@ protected function _build_message()
return FALSE;
}
+ $content_id = ($this->_attach_cid[$i]) ? "Content-ID: <".$this->_attach_cid[$i].">".$this->newline : '';
+
$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
+ . $content_id;
$file = filesize($filename) +1;
View
11 user_guide_src/source/libraries/email.rst
@@ -85,7 +85,7 @@ Preference Default Value Options Descript
=================== ====================== ============================ =======================================================================
**useragent** CodeIgniter None The "user agent".
**protocol** mail mail, sendmail, or smtp The mail sending protocol.
-**mailpath** /usr/sbin/sendmail None The server path to Sendmail.
+**mailpath** /usr/sbin/sendmail None The server path to Sendmail.
**smtp_host** No Default None SMTP Server Address.
**smtp_user** No Default None SMTP Username.
**smtp_pass** No Default None SMTP Password.
@@ -104,6 +104,8 @@ Preference Default Value Options Descript
**newline** \\n "\\r\\n" or "\\n" or "\\r" Newline character. (Use "\\r\\n" to comply with RFC 822).
**bcc_batch_mode** FALSE TRUE or FALSE (boolean) Enable BCC Batch Mode.
**bcc_batch_size** 200 None Number of emails in each BCC batch.
+**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
@@ -228,11 +230,12 @@ use the function multiple times. For example::
$this->email->attach('/path/to/photo2.jpg');
$this->email->attach('/path/to/photo3.jpg');
-If you'd like to change the disposition or add a custom file name, you can use the second and third paramaters. To use the default disposition (attachment), leave the second parameter blank. Here's an example::
-
+If you'd like to change the disposition or add a custom file name, you
+can use the second and third paramaters. To use the default disposition
+(attachment), leave the second parameter blank. Here's an example::
+
$this->email->attach('/path/to/photo1.jpg', 'inline');
$this->email->attach('/path/to/photo1.jpg', '', 'birthday.jpg');
-
$this->email->print_debugger()
-------------------------------

0 comments on commit 9260cce

Please sign in to comment.