将textarea保存到数据库中,并通过cakephp发送保留文本区域格式

问题描述:

的电子邮件,我保存一个文本区域,该区域将用作电子邮件消息到数据库中。 我可以正确保存它,但是当我试图从数据库中提取数据并使用该数据发送电子邮件时,我收到的电子邮件是一长串文本,而不是保持格式如何在文本区域。是否可以保持格式发送电子邮件?将textarea保存到数据库中,并通过cakephp发送保留文本区域格式

例如:如果在文本区域我输入类似:

This is line 1. 
This is line 2. 
This is line 3. 

我得到的电子邮件将

This is line 1. This is line 2. This is Line 3. 

我要的是什么,我打字:

This is line 1. 
This is line 2. 
This is line 3. 

这是代码: App :: uses('CakeEmail','Network/Email');

$recipients = str_replace(' ', '', $recipients); 
    $recipients = explode(',', $recipients); 
    $email = new CakeEmail(); 
    $email->from($user_email); 
    $email->to($recipients); 
    $email->subject($final_subject); 
    $email->template('download_link_email'); 
    $email->emailFormat('both'); 
    $email->viewVars(array('url' => $this->generateUrl('datas', 'download', $group_id, $user_id, $email_id), 'final_subject' => $final_subject, 'final_recipient' => $final_recipients, 'final_message' => $final_message)); 
    $email->send(); 

和download_link_email模板

<html> 
    <head> 
     <title><?php echo $final_subject; ?></title> 
    </head> 
    <body> 
     <h1><?php echo $final_subject; ?></h1> 
     <p><?php echo $final_message; ?></p> 
     <p><?php echo $this->Html->link('Click here to download', $url);?>.</p> 
    </body> 
</html> 

至于文本区域是如何被保存到数据库中,它是标准的CakePHP的风格

你需要做这样的事情:

$order = array("\r\n", "\n", "\r"); 
$replace = '<br />'; 
$final_message = str_replace($order, $replace, $final_message); 

你可以检查here了解更多关于str_replace的信息

或者您可以使用nl2br(感谢eggyal指点出来)

nl2br($final_message) 
+0

不知道这个问题,但似乎是大致相同 – Josejulio 2013-03-14 21:49:39