CodeIgniter: Scalar Variable Attachments

Many systems that provide email sending seem to only allow file attachments. There never seems to be a feature for creating the attachment in a variable and sending it. CodeIgniter, no matter how awesome, is no exception. So, I’ve been forced to cobble together an extension to the email handler which does it. It suffers from being a bit memory piggy, because it caches the attachments in memory so it can treat both file and scalar attachments the same. Take a gander, if you need such a beast.

_set_error_message('email_attachment_missing', $filename);
            return;
        }

        $file = filesize($filename) +1;
        if (!$fp = fopen($filename, FOPEN_READ))
        {
            $this->_set_error_message('email_attachment_unreadable', $filename);
            return;
        }
        $this->_attach_data[]     = fread($fp,$file);
        fclose($fp);

        $this->_attach_name[]     = $filename;
        $this->_attach_basename[] = basename($filename);
        $this->_attach_type[]     = $this->_mime_types(next(explode('.', basename($filename))));
        $this->_attach_disp[]     = $disposition; // Can also be 'inline'  Not sure if it matters
        $this->_attach_newname[]  = $newfilename != null ? $newfilename : $filename;     //replaces the filename 
    }

    // --------------------------------------------------------------------
    /**
     * Assign string attachments
     *
     * @access    public
     * @param    string
     * @return    void
     */
    function raw_attach($filename = NULL, $mimetype = 'application/octet-stream',
                    $disposition = 'attachment', $data = NULL)
    {
        $this->_attach_name[]     = $filename;
        $this->_attach_basename[] = $filename;
        $this->_attach_type[]     = $mimetype;
        $this->_attach_disp[]     = $disposition;
        $this->_attach_newname[]  = $filename;     //replaces the filename
        $this->_attach_data[]     = $data;
    }

    // --------------------------------------------------------------------

    /**
     * Build Final Body and attachments
     *
     * @access    private
     * @return    void
     */
    function _build_message()
    {
        if ($this->wordwrap === TRUE  AND  $this->mailtype != 'html')
        {
            $this->_body = $this->word_wrap($this->_body);
        }

        $this->_set_boundaries();
        $this->_write_headers();

        $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';

        switch ($this->_get_content_type())
        {
            case 'plain' :

                $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
                $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();

                if ($this->_get_protocol() == 'mail')
                {
                    $this->_header_str .= $hdr;
                    $this->_finalbody = $this->_body;

                    return;
                }

                $hdr .= $this->newline . $this->newline . $this->_body;

                $this->_finalbody = $hdr;
                return;

            break;
            case 'html' :

                if ($this->send_multipart === FALSE)
                {
                    $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
                    $hdr .= "Content-Transfer-Encoding: quoted-printable";
                }
                else
                {
                    $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
                    $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
                    $hdr .= "--" . $this->_alt_boundary . $this->newline;

                    $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
                    $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
                    $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;

                    $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
                    $hdr .= "Content-Transfer-Encoding: quoted-printable";
                }

                $this->_body = $this->_prep_quoted_printable($this->_body);

                if ($this->_get_protocol() == 'mail')
                {
                    $this->_header_str .= $hdr;
                    $this->_finalbody = $this->_body . $this->newline . $this->newline;

                    if ($this->send_multipart !== FALSE)
                    {
                        $this->_finalbody .= "--" . $this->_alt_boundary . "--";
                    }

                    return;
                }

                $hdr .= $this->newline . $this->newline;
                $hdr .= $this->_body . $this->newline . $this->newline;

                if ($this->send_multipart !== FALSE)
                {
                    $hdr .= "--" . $this->_alt_boundary . "--";
                }

                $this->_finalbody = $hdr;
                return;

            break;
            case 'plain-attach' :

                $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
               $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
                $hdr .= "--" . $this->_atc_boundary . $this->newline;

                $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
                $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();

                if ($this->_get_protocol() == 'mail')
                {
                    $this->_header_str .= $hdr;

                    $body  = $this->_body . $this->newline . $this->newline;
                }

                $hdr .= $this->newline . $this->newline;
                $hdr .= $this->_body . $this->newline . $this->newline;

            break;
            case 'html-attach' :

                $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
                $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
                $hdr .= "--" . $this->_atc_boundary . $this->newline;

                $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
                $hdr .= "--" . $this->_alt_boundary . $this->newline;

                $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
                $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
                $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;

                $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
                $hdr .= "Content-Transfer-Encoding: quoted-printable";

                $this->_body = $this->_prep_quoted_printable($this->_body);

                if ($this->_get_protocol() == 'mail')
                {
                    $this->_header_str .= $hdr;

                    $body  = $this->_body . $this->newline . $this->newline;
                    $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
                }

                $hdr .= $this->newline . $this->newline;
                $hdr .= $this->_body . $this->newline . $this->newline;
                $hdr .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;

            break;
        }

        $attachment = array();

        $z = 0;

        for ($i=0; $i < count($this->_attach_name); $i++)
        {
            $filename = $this->_attach_name[$i];
            $basename = $this->_attach_basename[$i];
            $ctype = $this->_attach_type[$i];
            //added for changing filename
            $newfilename = $this->_attach_newname[$i];

            $h  = "--".$this->_atc_boundary.$this->newline;
            $h .= "Content-type: ".$ctype."; ";
            $h .= "name=\"".$newfilename."\"".$this->newline;     //makes it so you can change the filename
            $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
            $h .= "Content-Transfer-Encoding: base64".$this->newline;

            $attachment[$z++] = $h;

            $attachment[$z++] = chunk_split(base64_encode($this->_attach_data[$i]));
        }

        if ($this->_get_protocol() == 'mail')
        {
            $this->_finalbody = $body . implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";

            return;
        }

        $this->_finalbody = $hdr.implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";

        return;
    }

}
?>

Related Posts:

This entry was posted in Programming and tagged , , , , , . Bookmark the permalink.

Comments are closed.