I have a long HTML string as my email body and I need to insert new line characters ("\n") to split the HTML into several lines, because mail servers have problems with long lines.
How do I do it without breaking the HTML tags?
I'm not talking about adding new line-breaks (<br>) in the HTML, I want to insert new line characters in the string so when mail servers fetch HTML line by line it shouldn't reach the line length limit.
For example:
If I have a HTML string:
<head><title>Enter a title, displayed at the top of the window.</title></head><body><h1>Enter the main heading, usually the same as the title.</h1><p>Be <b>bold</b> in stating your key points. Put them in a list: </p></body>
I want to insert new line-breaks:
<head>\n<title>\nEnter a title, displayed at the top of the window.\n</title>\n</head>\n<body>\n<h1>\nEnter the main heading, usually the same as the title.\n</h1>\n<p>\nBe <b>bold</b> in stating your key points. Put them in a list: \n</p>\n</body>
Many HTML emails use the quoted-printable encoding:
<head><title>Enter a title, displayed at the top of the window.</title></he=
ad><body><h1>Enter the main heading, usually the same as the title.</h1><p>=
Be <b>bold</b> in stating your key points. Put them in a list: </p></body>
In the above encoding, a trailing = marks the end of a line but doesn't break it. The original string can be reassembled by joining the lines accordingly.
Fortunately, there's no need to re-invent the wheel. The excellent Mail gem can do that for you: (and much more, you want to check out multi-part emails)
require 'mail'
mail = Mail.new do
content_type 'text/html'
content_transfer_encoding 'quoted_printable'
body "<head><title>Enter a title, displayed at the top of the window.</title></head><body><h1>Enter the main heading, usually the same as the title.</h1><p>Be <b>bold</b> in stating your key points. Put them in a list: </p></body>"
end
puts mail.to_s
Output:
Date: Wed, 03 Jun 2020 16:44:11 +0200
Message-ID: <5ed7b7...>
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<head><title>Enter a title, displayed at the top of the window.</title></=
head><body><h1>Enter the main heading, usually the same as the title.</h1=
><p>Be <b>bold</b> in stating your key points. Put them in a list: </p></=
body>=
Since you tagged your question ruby-on-rails, you might also want to check out Action Mailer.
Related
Hopefully I can make this understandable as I am not a programmer.
I have an email that I believe the header information is correct but the content has been altered.
In the content portion of the header in every gmail that I have sent/forwarded there is an original text version of the email content and an HTML version which is limited to 76 characters and each sentence ends with an =.
This occurs whether the sentence was a complete sentence or if the email servers cut the words into 2 sections like this.
"little boy blue ju=
mped over the moon"=
When the line hit 75 characters it inserted the = sign and moved the rest of the word to the next line
This creates a perfectly square box of text.
Now, I have an email where I believe the almost clever individual attempted to spoof the content part of the header data but instead completed each sentence with a =20 then moved to the next line. No words were cropped and the context is not in a perfect square as it is on every other email header I have inspected.
Also, when an email is forwarded each line is prefaced with a > or a >> which is not the case in this particular email.
Additionally in the HTML portion of the emails the paragraph will end with additional coding such as...
In the HTML code used in Gmails headers, what does the =20 designate?
Also, based on the minimal amount of information I have supplied to you am I correct in believing the content may be spoofed?
How to post topic in correct wiki syntax when body has "\r\n" which means "carriage return" and "newline"?
When I use data=json.dumps(%topic_body%) it makes all my text with literally "\r\n" in it!
Of course text formatted like this - cannot be used as wiki formatted on confluence!
This is an example of usual markup:
h1. Some Description
[Some link|Link...] is ...
h2. Some
h2. Some Versions
* 9
* 10
* 11
h1. Some Software
||Table 1 ||Block 1||Some||Some 2||
This is how it reproduces via json:
{"storage": {"value": "b'h1. Some Description\\r\\n[Some link|Link...] is ...\\r\\n\\r\\nh2. Some\\r\\n\\r\\nh2. Some Versions\\r\\n* 9\\r\\n* 10\\r\\n* 11\\r\\n\\r\\nh1. Some Software \\r\\n\\r\\n||Table 1 ||Block 1||Some||Some 2||'", "representation": "wiki"}}}
This is important, and I can't send body in markdown because my confluence did not understand this way of macros:
<ac:structured-macro ac:name="attachments">
</ac:structured-macro>
So I need to send my topic body that way, which can include new line method (https://confluence.atlassian.com/doc/confluence-wiki-markup-251003035.html)
Also I can't use documented:
Explicitly, by entering two consecutive backslashes: \\
Because in this condition wiki markup make all text the same forematted, just like:
h1. Some Description \\ \\ [Some link|Link...] is ...
This whole string will be "h1." size. All all other text from this string will be formatted as h1, ignoring any other tags.
Fixed!
You can ignore the case, when visual(in browser) wiki markdown does not work for you. It will work via REST anyway!
I am saving copy from a textarea on a web page into a sql server db. This process preserves the carriage return and line feed when displaying the text elsewhere.
I want to add some content to the same field programmatic-ally - does anyone know what the characters are that create this cr and lf? I cannot determine them using ASCII converters.
CR + LF is as follows in T-SQL:
SELECT 'foo' + CHAR(13) + CHAR(10) + 'bar';
In a lot of cases it is important to include both and it is important to list them in that specific order.
When HTML is displayed it will mostly ignore white-space, so a CR+LF in the HTML (carriage return and line-feed) will not cause a new line to be shown in the HTML rendered in a browser.
Instead, HTML uses paragraph tags <p></p>, and line-break tags <br /> for paragraphs and line-breaks respectively.
I have a simple html page with a div element in it.
The innerHTML property of the div is set through query String.
In query string I pass html strings,i.e.
<p style='font-size:20px;color:green;'> Sun rises in the east </p> etc...
I get the appropriate output.
However, if I pass color code in style attribute say, #00990a, I am not displayed any content.
Can someone help me through this?
if theres a color code that contains a #, everything after that will be treated fragment identifier. to avoid this you have to url-encode your parameter-value (replacing # with %23 an d doing the same with other characters that have a special meaning (#&%=?#...)).
Finally your url should look like this:
PageUrl?Content=%3Cp+style%3D%27color%3A%23009900%27%3EContent%3C%2Fp%3E
Since you haven't shown us any code, I shall guess…
In a URI, # indicates the start of the fragment identifier (as ? indicates the start of the query string). Your colour is terminated the query string and starting the fragment identifier. You need to URL encode any character that has special meaning in URLs. (# is %23).
Do make sure that you sanitise the passed HTML and CSS on the server though. It is very easy to expose yourself to XSS attacks otherwise.
Certain fields in our mysql db appear to contain newline characters so that if I SELECT on them something like the following will be returned for a single SQL call:
Life to be sure is nothing much to lose
But young men think it is and we were young
If I want to preserve the line breaks when displaying this field on a webpage, is the standard solution to write a script to replace '\n\r' with a br HTML tag or is there a better way?
Thanks!
Assuming PHP here...
nl2br() adds in <br /> for every \n. Don't forget to escape the content first, to prevent XSS attacks. See below:
<?php echo nl2br(htmlspecialchars($content)); ?>
HTML is a markup language. Regardless of how many linebreaks you put in the source code, you won't see anything from it back in the presentation (of course assuming you aren't using <pre> or white-space:pre). HTML uses the <br> element to represent a linebreak. So you basically indeed need to convert the real and invisible linebreaks denoted by the characters xA (newline, linefeed, LF, \n) and/or xD (carriage return, CR, \r) by a HTML <br> element.
In most programming languages you can just do this by a string replace of "\n" by "<br>".
You can wrap it in <pre> .. </pre>.