Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control line breaks in plain text email views (.text.erb)

Unlike most plain text email related questions, my problem is that there are too many line breaks in the plain text emails Rails is sending out.

For simplicity while starting up, I ditched HTML emails altogether and just use plain text emails (using .text.erb views). My problems occur where I have conditional lines in the view, as the new line of code in my view file carries over to the email.

For example:

    Line 1
    <%= "Line 2" if false %>
    Line 3

will render as:

    Line 1

    Line 3

and not the intended output:

    Line 1
    Line 3

My current hack is to use the following:

    Line 1
    <%= "Line 2\n" if false %>Line 3

This can get really messy when there are multiple conditionals in a row.

Surely there must be a better way!

like image 313
Felix Avatar asked Feb 15 '13 14:02

Felix


People also ask

How do you insert a line break in plain text email?

You must be using double quotes, and you use \r\n to make a new line.

Why Outlook remove extra line breaks?

Microsoft Outlook removes line breaks in signatures for messages sent in plain text format. For the sender, the message looks normal but when displayed to the recipient, line breaks are removed so the formatting of the signature is compromised.

How do you break an email in HTML?

To do a line break in HTML, use the <br> tag.


1 Answers

This is to answer Felix' question on Andy Waite's answer (I don't think multi-line code is possible in comments and this question is about multi-line code).

I think <%= "foo\n" if something -%> would work, but this seems cleaner to me:

Line 1
<% if something -%>
foo
<% end -%>
Line 3
like image 59
Richard Jones Avatar answered Oct 26 '22 19:10

Richard Jones