Cakephp sending UTF-8 Emails and lineLength

梦想的初衷 提交于 2020-01-06 07:26:14

问题


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:


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


来源:https://stackoverflow.com/questions/17706331/cakephp-sending-utf-8-emails-and-linelength

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!