开发者

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

How can I make codeigniter to send an email while respecting the line breaks from the message field?

the form message - 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>
        <t开发者_JS百科d>
            <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 get the email, I get "hi, i am awesome" all in one line. I have the newline 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?


why not send the email has html ?

first you must prepare the message (I'm assuming that you're using POST)

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

or you can use the native php way to get $_POST

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

What you've done, is to replace new lines with <br>

Then you just need to load the lib and set it correctly trough config, for example:

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

That's it! I hope this helps,

You can get more info on http://codeigniter.com/user_guide/libraries/email.html Hopefully you'll take the time to read it!


Just to add, you can simplify this process using nl2br and simply ->mailtype = 'html';. Like so:

$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';

Also, if you want to create a config to use all the time, you can actually create a config file and CI will use it automatically, thus you never need to use ->initialize. To do so, just follow these simple steps:

  • in the directory of application\config, create a file named email.php
  • Then you can simply write in your configuration in that file like so:

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

Viola! You're done. It's just that simple! Now just call your email class and use as usual with no need to configure things like mailtype. You can see full list of email config options under the title Email Preferences here. Don't forget, you can use the application\config\autoload.php to load the email library automatically, thus removing this line $this->load->library('email'); from your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜