CodeIgniter Forums
HTML email does not recognize newline - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: HTML email does not recognize newline (/showthread.php?tid=15915)



HTML email does not recognize newline - El Forum - 02-18-2009

[eluser]jules123[/eluser]
Hello,

I am using the CI Email class to send emails. The only config I have in my config/email.php file is: $config['mailtype'] = 'html';

I receive the HTML emails fine except that the email body does not have any newline characters.

For example, if my message body is "ABC\nDEF\n", it shows up as ABC DEF and not -

ABC
DEF

Any thoughts what is going on? Any help is appreciated!


HTML email does not recognize newline - El Forum - 02-18-2009

[eluser]xzela[/eluser]
if you are using HTML as the mailtype, you could probably use the html tag for new lines:
Code:
<br /> //new line



HTML email does not recognize newline - El Forum - 02-18-2009

[eluser]jules123[/eluser]
Thanks for the tip! But do I have to use the HTML tag br? I would like the flexibility to use \n if I decide to switch to text mailtype. Besides, I thought the default config is $config['newline'] = "\n"; so the \n should work....

What am I missing?


HTML email does not recognize newline - El Forum - 02-18-2009

[eluser]xzela[/eluser]
Just for kicks, try using single quotes for the '\n' in your string. Something like this:
Code:
$var = "ABC" . '\n' . "DEF" . '\n';

and if that doesn't work, reverse it:
Code:
$var = 'ABC' . "\n" . 'DEF' . "\n";

just a shot in the dark. Not sure if it will work though.


HTML email does not recognize newline - El Forum - 02-19-2009

[eluser]TheFuzzy0ne[/eluser]
Just like an HTML Web page, new lines are irrelevant. If you're sending in HTML format, &lt;br /&gt; is how you "render" a new line. You can put as many "\n"s in there as your heart desires. It still won't have any bearing on how the page is rendered, unless you send in plain text.

If you're wanting to switch between formats (no idea why), you could always perform a search and replace on the Email body before sending.

Hope this helps.


HTML email does not recognize newline - El Forum - 02-19-2009

[eluser]jules123[/eluser]
Thanks all. TheFuzzy0ne - agree with what you are saying that in an HTML message, the br tag is needed for rendering newlines. After replacing \n by the br tag, HTML emails are appearing fine.

It's just that I interpreted the below to mean that - use \n to define where you want to have newlines and then depending on whether mailtype is text or html, CI will do whatever is necessary...

$config['newline'] = "\n";
$config['mailtype'] = 'html';

Anyway, the reason to have the flexibility to switch between the mailtypes is that we will have multiple installations of the same application with potentially different mailtypes...

So now --- I would like to do the replacement of \n by br by checking what the mailtype is. But I am not sure how to retrieve the configuration from config\email.php. If I do $this->config->item('mailtype'), I get an empty string and not my actual config (text or html). Is there a way to get the mailtype?

And finally, thanks again for all the help! This forum is great!! Smile


HTML email does not recognize newline - El Forum - 02-19-2009

[eluser]TheFuzzy0ne[/eluser]
I agree, the $config['newline'] part may be misleading. To clear it up, that newline character is used for wordwrapping the text. Smile

There may be a better way to achieve what you want. I would suggest having two views, one for plain text, and one for HTML. You can then grab the output of the appropriate view depending on which mail type you want to use.
Code:
if ($mail_type == "html")
{
    $body = $this->load->view('email_html', $data, TRUE);
}
else
{
    $body = $this->load->view('email_plaintext', $data, TRUE);
}



HTML email does not recognize newline - El Forum - 02-19-2009

[eluser]jules123[/eluser]
Actually, I have a central email function (defined in a helper) that I use from everywhere to send emails. So it is very easy for me to check the mailtype and do the replacement inside this function.

My problem however is - how do I retrieve the mailtype configuration in this helper function? If I do the following -
$mail_type = $this->config->item('mailtype');
This gives me an empty string instead of giving the actual setting from the config/email.php file.

Now, I can repeat the mailtype setting in my custom config file, as say $config['mailtype_repeat'] = 'html'; and get it that way, but I would hate to do the repetition...


HTML email does not recognize newline - El Forum - 02-19-2009

[eluser]TheFuzzy0ne[/eluser]
From within your helper function:
Code:
$CI =& get_instance(); // Get a reference to the CI super object.
$mail_type = $CI->email->mailtype; // Sorted!

I haven't tested it, but it should work.


HTML email does not recognize newline - El Forum - 02-19-2009

[eluser]jules123[/eluser]
Ah! Perfect! Working just fine. Thanks so much! Smile