使用 CodeIgniter 发送 base64 编码的电子邮件

Send base64 encoded email using CodeIgniter

我正在尝试使用 CodeIgniter 发送 base64 编码的电子邮件。 代码下方:

$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); 

但是电子邮件发送不正确。我刚刚在邮件中找到了 base64 编码的文本,而不是 base64 解码。

发送调试如下:

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--

我找不到确切的问题。 我正在使用 CodeIgniter 版本 2.1.3 .

您必须将内容的 header 设置为 Content-Transfer-Encoding:base64。这不是由 CodeIgniter 默认设置的。为此,您必须修改他们的电子邮件库或简单地扩展他们的电子邮件 class 并添加此 header,以符合标准并确保您不会破解核心。

CodeIgniter 版本 2.1.3 没有任何用于 base64 编码电子邮件的选项。在核心文件中编辑后,找到了解决方案。我已经分享了解决方案。

答案:

注意:这是对核心文件的破解。如果您只需要 base64 编码的消息,请尝试它。否则创建额外的库文件。

我们需要在库文件中编辑一些东西 email.php。位于 "libraries/Email.php".

对于纯文本消息。

1/

首先添加 base64 作为编码选项。

编辑:$_bit_depths 变量。

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

2/

然后在发送控制器。在电子邮件配置中添加 '_encoding' => 'base64'。检查我的有问题的代码。

对于HTML条消息

按照步骤 1 和 2 进行操作。然后执行以下步骤:

3/

在 Email.php 文件中,找到函数 function _build_message()

在这个函数中,找到switch ($this->_get_content_type())然后去case 'html '删除或者评论两个地方。 下面编辑的代码:

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;

就是这样。现在 base64 编码的电子邮件将正确发送。