Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML email's text representation includes too many linebreaks #153

Closed
foobear opened this issue Oct 15, 2020 · 4 comments
Closed

HTML email's text representation includes too many linebreaks #153

foobear opened this issue Oct 15, 2020 · 4 comments
Labels

Comments

@foobear
Copy link
Member

foobear commented Oct 15, 2020

MailFinder.email_text_body deduplicates multiple \n newlines before it converts \r\n linebreaks to \n. That way, newlines in bodies with only \r\n linebreaks are actually never deduplicated.

For example,

"<p>\r\nHello!\r\n</p>\r\n<p>\r\nBye!\r\n</p>"

results in

"Hello!\n\n\nBye!"

Relevant code:

def email_text_body(mail, type = '')
body = if mail.html_part && type != 'plain-text'
dom = Nokogiri::HTML(mail.html_part.body.to_s)
dom.at_css('body').text.gsub(/\n\n/, "\n")
elsif mail.text_part && type != 'HTML'
mail.text_part.body.to_s
else
mail.body.to_s
end
body.gsub("\r\n", "\n") # The mail gem (>= 2.7.1) switched from \n to \r\n line breaks (LF to CRLF) in plain text mails.
end

While we could just move gsub(/\n\n/, "\n") from the html_part section to the end of email_text_body and always deduplicate, this would impact plain-text bodies negatively.
Maybe use gsub(/\r?\n\r?\n/, "\n") in the html_part section instead. Or gsub(/\r?\n(?:\r?\n)/, "\n"), maybe?

Please also provide a 2.99.x backport for this to allow easier migration to 3.0.

@foobear foobear changed the title HTML text representation includes too many linebreaks HTML email' text representation includes too many linebreaks Oct 15, 2020
@foobear foobear changed the title HTML email' text representation includes too many linebreaks HTML email's text representation includes too many linebreaks Oct 15, 2020
@makmic makmic added the bug label Oct 16, 2020
@kratob
Copy link
Member

kratob commented Oct 28, 2020

I suggest using [\r\n]+ for the regexp.

@foobear
Copy link
Member Author

foobear commented Oct 28, 2020

Note that this would discard blank lines in plain-text bodies, and tests expecting those would fail.

Putting gsub(/[\r\n]+/, "\n") only in the HTML part's branch should be fine.

@FLeinzi
Copy link
Contributor

FLeinzi commented Oct 28, 2020

Yes, @kratob means the regexp in line 63

@FLeinzi
Copy link
Contributor

FLeinzi commented Nov 4, 2020

I have fixed this in v3.0.1.

There is also a backport in v2.99.2

@FLeinzi FLeinzi closed this as completed Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants