How to make CodeIgniter respect line breaks in a message when sending an email

How can I get codeigniter to send email when line breaks from a message field are observed?

form post - http://d.pr/Sae5

<?php echo form_open($this->uri->uri_string()); ?>
<table class="forms-table">
    <tr>
        <td>
            <label for="name">Name</label>
        </td>
        <td>
            <input type="text" id="name" name="name" value="<?php echo set_value('name'); ?>" />
        </td>
        <td>
            <?php echo form_error('name'); ?>
        </td>
    </tr>
    <tr>
        <td>
            <label for="email">Email</label>
        </td>
        <td>
            <input type="text" id="email" name="email" value="<?php echo set_value('email'); ?>" />
        </td>
        <td>
            <?php echo form_error('email'); ?>
        </td>
    </tr>
    <tr>
        <td>
            <label for="message">Message</label>
        </td>
        <td>
            <textarea name="message" id="message" cols="40" rows="6"><?php echo set_value('message'); ?></textarea>

        </td>
        <td>
            <?php echo form_error('message'); ?>
        </td>
    </tr>
    <tr>
        <td colspan="3">
            <input type="submit" value="submit" />
        </td>
    </tr>
</table>
<?php echo form_close(); ?>

When I receive an email, I get "hello, I'm awesome" all on one line. I have a new line and crlf config set to "\ r \ n", charset is "utf-8" and I get the value of my message field using

$message = $this->input->post('message');

...

$this->email->message($message);

any thoughts?

+3
source share
1 answer

why not send an email has HTML?

you must prepare the message first (I assume you are using POST)

$message = str_replace ("\r\n", "<br>", $this->input->post('message') );

or you can use native php way to get $_POST

$message = str_replace ("\r\n", "<br>", $_POST['message'] );

, <br>

config, :

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

$config['mailtype'] = 'html';
$this->email->initialize($config);


$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');

$this->email->subject('Email Test');
$this->email->message( $message );

$this->email->send();

! , ,

http://codeigniter.com/user_guide/libraries/email.html , , !


, , nl2br ->mailtype = 'html'; , :

$message = nl2br($this->input->post('message')); // https://codeigniter.com/user_guide/libraries/input.html

$this->load->library('email'); // use autoload.php to remove this line
$this->email->mailtype = 'html';

, , , CI , ->initialize. :

  • application\config email.php
  • , , :

'$config['mailtype'] = 'html';'

! ! , , mailtype. email config " Email Preferences . , application\config\autoload.php email, , , $this->load->library('email'); .

+7

All Articles