CodeIgniter Forums
CI3 inline images in email using buffer string - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: CI3 inline images in email using buffer string (/showthread.php?tid=78588)



CI3 inline images in email using buffer string - ludis - 02-10-2021

Good day,
I have a hard time adding a buffer string as inline image in an HTML email. 
Code:
$this->email->attach( $bin, 'inline', $filename, 'image/png' )

The command
Code:
$this->email->attachment_cid( $filename )
returns FALSE.

I looked into the Email library, function attachment_cid() and it turns out that buffered images have two filenames. The first one is gibberish, the second one is the actual filename. But the function checks only the first name and because that one's different from the chosen filename the function thinks there's no attachment of that name and returns FALSE.

Is this a bug in the function attachment_cid()?


RE: CI3 inline images in email using buffer string - ludis - 02-16-2021

Just for reference, I extended the Email class with a custom method to be used with buffer strings:
Code:
    public function buffered_attachment_cid($filename)
    {
        for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
        {

            for ( $j = 0, $n = count( $this->_attachments[$i]['name'] ); $j < $n; $j++ )
            {
                if ($this->_attachments[$i]['name'][$j] === $filename)
                {
                    $this->_attachments[$i]['multipart'] = 'related';
                    $this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][$j]).'@');
                    return $this->_attachments[$i]['cid'];
                }
            }
        }

        return FALSE;
    }
This one handles inline buffer string images correctly.