Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added Email::attach_cid() returning CID which enables to embed an attachment to html #1585

Closed
wants to merge 6 commits into from

2 participants

Petr Heralecký Andrey Andreev
Petr Heralecký

Sorry there are few more changes in this one pull request, but every this changes are connected and can't be separate. Look at commits as in individual changes. Every these changes are directed for attaching inline images, but if some commit is insufficient or something, I can rewrite it.

If you want to try demo of this functins, I'm sending email to you with demo in few minutes.

Andrey Andreev narfbg commented on the diff
system/libraries/Email.php
((20 lines not shown))
+ }
+
+ if ( ! $fp = fopen($filename, FOPEN_READ))
+ {
+ $this->_set_error_message('lang:email_attachment_unreadable', $filename);
+ unset($this->_attachments[$ind]);
+ return FALSE;
+ }
+
+ $this->_attachments[$ind]['type'] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
+ $file_content = stream_get_contents($fp);
+ fclose($fp);
+ }
+ else
+ {
+ $file_content = $filename;
Andrey Andreev Owner
narfbg added a note

What is this for?

Petr Heralecký
melounek added a note

Looks like opportunity to attach plain text instead of filename, but you are right, this shouldn't be in this pull-request.
Should I do this pull-request again for actual version and without this mistake?

Andrey Andreev Owner
narfbg added a note

Yes, please.
Quite a lot of changes have been done to the Email library since this was submitted anyway. The $_attachments unification for example is already done.

Now I see it... the opportunity to attach buffer instead of filename is already there https://github.com/melounek/CodeIgniter/blob/develop/system/libraries/Email.php#L1367
documented there https://github.com/melounek/CodeIgniter/blob/develop/user_guide_src/source/changelog.rst (find "email library")

anyway... today I will send actual pull request...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Andrey Andreev
Owner

Replaced by #2807.

Andrey Andreev narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 26, 2012
  1. unificate email attachment properties

    Melounek authored
Commits on Jun 27, 2012
  1. Added method Email::attach_cid() returning CID which enables to embed…

    Melounek authored
    … an attachment to html
Commits on Jun 28, 2012
  1. fix commits bbf51f6 and d172512

    Melounek authored
This page is out of date. Refresh to see the latest.
1  system/language/english/email_lang.php
View
@@ -29,6 +29,7 @@
$lang['email_invalid_address'] = "Invalid email address: %s";
$lang['email_attachment_missing'] = "Unable to locate the following email attachment: %s";
$lang['email_attachment_unreadable'] = "Unable to open this attachment: %s";
+$lang['email_attachment_not_attached'] = "Attachment '%s' probably wasn not correctly attached.";
$lang['email_no_recipients'] = "You must include recipients: To, Cc, or Bcc";
$lang['email_send_failure_phpmail'] = "Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.";
$lang['email_send_failure_sendmail'] = "Unable to send email using PHP Sendmail. Your server might not be configured to send mail using this method.";
113 system/libraries/Email.php
View
@@ -51,7 +51,7 @@ class CI_Email {
public $wrapchars = 76; // Number of characters to wrap at.
public $mailtype = 'text'; // text/html Defines email formatting
public $charset = 'utf-8'; // Default char set: iso-8859-1 or us-ascii
- public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate)
+ public $multipart = 'related'; // "related" (separate) or "mixed" (in the body)
public $alt_message = ''; // Alternative message for HTML emails
public $validate = FALSE; // TRUE/FALSE. Enables email validation
public $priority = 3; // Default priority (1 - 5)
@@ -81,9 +81,7 @@ class CI_Email {
protected $_cc_array = array();
protected $_bcc_array = array();
protected $_headers = array();
- protected $_attach_name = array();
- protected $_attach_type = array();
- protected $_attach_disp = array();
+ protected $_attachments = 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');
@@ -171,9 +169,7 @@ public function clear($clear_attachments = FALSE)
if ($clear_attachments !== FALSE)
{
- $this->_attach_name = array();
- $this->_attach_type = array();
- $this->_attach_disp = array();
+ $this->_attachments = array();
}
return $this;
@@ -409,11 +405,62 @@ public function message($body)
*/
public function attach($filename, $disposition = '', $newname = NULL, $mime = '')
{
- $this->_attach_name[] = array($filename, $newname);
- $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters
- $this->_attach_type[] = $mime;
+ $ind = count($this->_attachments); // incremented index of attachments
+ $this->_attachments[$ind]['name'] = array($filename,$newname);
+ $this->_attachments[$ind]['disp'] = empty($disposition) ? 'attachment' : $disposition;
+ $this->_attachments[$ind]['type'] = $mime;
+ $this->_attachments[$ind]['cid'] = NULL;
+
+ if ($this->_attachments[$ind]['type'] === '')
+ {
+ if ( ! file_exists($filename))
+ {
+ $this->_set_error_message('lang:email_attachment_missing', $filename);
+ unset($this->_attachments[$ind]);
+ return FALSE;
+ }
+
+ if ( ! $fp = fopen($filename, FOPEN_READ))
+ {
+ $this->_set_error_message('lang:email_attachment_unreadable', $filename);
+ unset($this->_attachments[$ind]);
+ return FALSE;
+ }
+
+ $this->_attachments[$ind]['type'] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
+ $file_content = stream_get_contents($fp);
+ fclose($fp);
+ }
+ else
+ {
+ $file_content = $filename;
Andrey Andreev Owner
narfbg added a note

What is this for?

Petr Heralecký
melounek added a note

Looks like opportunity to attach plain text instead of filename, but you are right, this shouldn't be in this pull-request.
Should I do this pull-request again for actual version and without this mistake?

Andrey Andreev Owner
narfbg added a note

Yes, please.
Quite a lot of changes have been done to the Email library since this was submitted anyway. The $_attachments unification for example is already done.

Now I see it... the opportunity to attach buffer instead of filename is already there https://github.com/melounek/CodeIgniter/blob/develop/system/libraries/Email.php#L1367
documented there https://github.com/melounek/CodeIgniter/blob/develop/user_guide_src/source/changelog.rst (find "email library")

anyway... today I will send actual pull request...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
+ $this->_attachments[$ind]['content'] = chunk_split(base64_encode($file_content));
+
return $this;
}
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Sets and return id of attachment (useful for attached inline pictures)
+ *
+ * @param string
+ * @return id
+ */
+ public function attach_cid($filename)
+ {
+ foreach($this->_attachments as $ind => $attach)
+ {
+ if($attach['name'][0] == $filename)
+ {
+ $this->_attachments[$ind]['cid'] = uniqid(basename($this->_attachments[$ind]['name'][0]) . "@");
+ return $this->_attachments[$ind]['cid'];
+ }
+ }
+ return FALSE;
+ }
// --------------------------------------------------------------------
@@ -629,9 +676,9 @@ protected function _get_content_type()
{
if ($this->mailtype === 'html')
{
- return (count($this->_attach_name) === 0) ? 'html' : 'html-attach';
+ return (count($this->_attachments) === 0) ? 'html' : 'html-attach';
}
- elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0)
+ elseif ($this->mailtype === 'text' && count($this->_attachments) > 0)
{
return 'plain-attach';
}
@@ -1036,45 +1083,19 @@ protected function _build_message()
}
$attachment = array();
- for ($i = 0, $c = count($this->_attach_name), $z = 0; $i < $c; $i++)
+ for ($i = 0, $c = count($this->_attachments), $z = 0; $i < $c; $i++)
{
- $filename = $this->_attach_name[$i][0];
- $basename = is_null($this->_attach_name[$i][1]) ? basename($filename) : $this->_attach_name[$i][1];
- $ctype = $this->_attach_type[$i];
- $file_content = '';
-
- if ($this->_attach_type[$i] === '')
- {
- 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->_attach_content[$i];
- }
+ $filename = $this->_attachments[$i]['name'][0];
+ $basename = is_null($this->_attachments[$i]['name'][1]) ? basename($filename) : $this->_attachments[$i]['name'][1];
$attachment[$z++] = '--'.$this->_atc_boundary.$this->newline
- .'Content-type: '.$ctype.'; '
+ .'Content-type: '.$this->_attachments[$i]['type'].'; '
.'name="'.$basename.'"'.$this->newline
- .'Content-Disposition: '.$this->_attach_disp[$i].';'.$this->newline
- .'Content-Transfer-Encoding: base64'.$this->newline;
+ .'Content-Disposition: '.$this->_attachments[$i]['disp'].';'.$this->newline
+ .'Content-Transfer-Encoding: base64'.$this->newline
+ .($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.'--';
5 user_guide_src/source/changelog.rst
View
@@ -138,6 +138,11 @@ Release Date: Not Released
- CI_Loader::_ci_autoloader() is now a protected method.
- 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).
+ - Email attachments are creating in function Email::attach() - it's faster for sending many emails with same attachments
+ - Email attachments opening by stream_get_contents() instead of fread. Thats enable to attach file paths a whole urls.
+ - Added method Email::attach_cid() returning CID which enables to embed an attachment to html.
+ - Unification of Email properties like Email->attach_name etc... to one associative array Email->_attachments
+ - Change multipart type from mixed to related in Email library. It makes better results in some Email clients especially for inlnine attachments.
- :doc:`Cart library <libraries/cart>` changes include:
- It now auto-increments quantity's instead of just resetting it, this is the default behaviour of large e-commerce sites.
- Product Name strictness can be disabled via the Cart Library by switching "$product_name_safe".
24 user_guide_src/source/libraries/email.rst
View
@@ -230,8 +230,8 @@ $this->email->attach()
----------------------
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 function multiple times. For example::
+parameter. For multiple attachments use the function multiple times.
+For example::
$this->email->attach('/path/to/photo1.jpg');
$this->email->attach('/path/to/photo2.jpg');
@@ -252,6 +252,26 @@ parameter as mime-type::
$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()
-------------------------------
Something went wrong with that request. Please try again.