Cakephp sending UTF-8 Emails and lineLength

2019-08-25 03:42发布

I'm trying to send an emails with UTF8 characters. Mostly the Email looks how I suspect, but randomly there will be garbage characters. I believe the garbage characters happen when a new line is inserted in the middle of one of the characters. I suspect CakePHP's email component is the culprit since I was reading that it has a feature to insert new lines according to its lineLength property. Is there any way to fix this? I'm using CakePHP 1.3.

$this->Email->to = $sendEmail;
$this->Email->from = empty($this->data['Contact']['email']) ? $sendEmail : $this->data['Contact']['email'];
$this->Email->subject = $subject;
$this->Email->sendAs = 'text';
$this->Email->template = 'contact'
$this->set('fields', $this->data['Contact']);
$this->Email->charset = "utf-8";
$this->Email->headerCharset = "utf-8";
return $this->Email->send();

From the email header:

Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

1条回答
倾城 Initia
2楼-- · 2019-08-25 04:38

I discovered this problem can be solved by encoding the email message in base64. CakePHP's email component does not natively do this, so I wrote a class Email64 to extend the email component. I rewrote all functions that contained

'Content-Transfer-Encoding: 7bit';

to

'Content-Transfer-Encoding: base64';

And then in the _mail() function, I replaced calls to php's mail function -- something like this --

return @mail($to, $this->_encode($this->subject),  $message, $header, $this->additionalParams);

to this --

return @mail($to, $this->_encode($this->subject),  rtrim(chunk_split(base64_encode($message))), $header, $this->additionalParams);
查看更多
登录 后发表回答