How to format text in CodeIgniter

2019-09-02 04:31发布

问题:

Im trying to send an array in an email. I use the function

$this->email->message(print_r($array2, true));

When i use this, the array come through in the email, but its displayed in a single line. Is it possible to use something like "pre and /pre" tags to format it? I tried using the typography helper in CI,

$this->load->helper('typography');
$array2 = auto_typography($array);

but it doesn't seem to work. Any ideas on how to go about this?

Thanks,

Chris.

回答1:

This one works. I tried it.

$message = '<pre>'.print_r($array2, true).'</pre>';
$this->email->message($message);

But you need to do this first. On your email config:

$config = Array(
            'protocol' => 'smtp',
            'mailtype'  => 'html',
            'charset'   => 'iso-8859-1',
            'newline'  => '<br/>',
            'wordwrap'  => TRUE
);

$this->load->library('email', $config); 

The $config['mailtype'] is the one responsible for the preformatted text.



回答2:

I would suggest sending the array in the message method without attaching the print_r function to the call. I would instead work with a view and send the array to the view for display.



回答3:

You can send email with PRE tag if you want. You would need to use parser tho. Also, first you need to make sure your email config file is set to send HTML emails instead of plain text.

In autoload.php include parser library.

Then in your controller construct the array and parse it:

$data = array('array' => $array);
$body = $this->parser->parse('path_to/email_template', $data, true);

Put your email template in your views folder and use it like this.

<pre>
  {array}
</pre>