Imran Abdur Rahim
Imran Abdur Rahim

Reputation: 417

Send base64 encoded email using CodeIgniter

I am trying to send base64 encoded email using CodeIgniter. Below the code:

$message = base64_encode($body);
$config = Array('protocol' => 'smtp', 'smtp_host' => $smtp, 'smtp_port' => $port, 'smtp_user' => $user, 'smtp_pass' => $pass, 'mailtype' => 'html', 'charset' => 'utf-8','_bit_depths' => array('7bit', '8bit', 'base64'),'_encoding' => 'base64',);

$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from($disemail, $disname);
$this->email->reply_to($disemail, $disname);
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message); 

But email is not sending properly. Instead of base64 decode i have just found base64 encoded text in mail.

Here is the debug of the sending:

Mime-Version: 1.0

Content-Type: multipart/alternative; boundary="B_ALT_5596ca8577c5c" This is a multi-part message in MIME format. Your email application may not support this format.

--B_ALT_5596ca8577c5c Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: base64

PGh0bWw+PGJvZHk+PGgxPnRoaXMgaXMgaHRtbDwvaDE+PC9ib2R5PjwvaHRtbD4=

--B_ALT_5596ca8577c5c Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable

PGh0bWw+PGJvZHk+PGgxPnRoaXMgaXMgaHRtbDwvaDE+PC9ib2R5PjwvaHRtbD4=3D

--B_ALT_5596ca8577c5c--

I couldn't find the exact problem. I am using CodeIgniter version 2.1.3 .

Upvotes: 0

Views: 3828

Answers (2)

Imran Abdur Rahim
Imran Abdur Rahim

Reputation: 417

CodeIgniter version 2.1.3 has not any option for base64 encoded email. After edit in core file, found the solution. I have shared the solution.

Answer:

Note: This is a hack into core file. If you just need only base64 encoded message try it. Otherwise create additional library file.

We need to edit something in library file email.php. Which is located in "libraries/Email.php".

For text only message.

1/

First add base64 as encode option.

Edit: $_bit_depths variable.

var $_bit_depths    = array('7bit', '8bit','base64');

2/

Then in sending controller. Add '_encoding' => 'base64' in email config. Check my code in question.

For HTML message

Follow step 1 and 2. And then step below:

3/

in Email.php file, find the function function _build_message() .

In this function, find switch ($this->_get_content_type()) and go to case 'html ' And delete or comment two places. Below edited code:

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;

                    $body .= $this->_get_mime_message() . $this->newline . $this->newline;
                    $body .= "--" . $this->_alt_boundary . $this->newline;

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

                    // I have commented 3 lines
                    /*
                    $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
                    $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
                    $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
                    */
                }

                //Instead of this commented line, use line below.
                //$this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
                $this->_finalbody = $body . $this->_body . $this->newline . $this->newline;

And that's it. Now base64 encoded email will send properly.

Upvotes: 0

raidenace
raidenace

Reputation: 12826

You have to set the header of your content as Content-Transfer-Encoding:base64. This is not set by default by CodeIgniter. In order to do this, you either have to modify their email library or simply extend their email class and add this header, in order to comply with standards and to ensure you do not hack the core.

Upvotes: 0

Related Questions