Fixed #165, missing style on html emails with blockquote in outlook.
This commit is contained in:
parent
8965826a3f
commit
4438cef46a
2 changed files with 99 additions and 47 deletions
|
@ -7,10 +7,10 @@ module Channel::EmailBuild
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
mail = Channel::EmailBuild.build(
|
mail = Channel::EmailBuild.build(
|
||||||
:from => 'sender@example.com',
|
from: 'sender@example.com',
|
||||||
:to => 'recipient@example.com',
|
to: 'recipient@example.com',
|
||||||
:body => 'somebody with some text',
|
body: 'somebody with some text',
|
||||||
:content_type => 'text/plain',
|
content_type: 'text/plain',
|
||||||
)
|
)
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
@ -53,7 +53,7 @@ module Channel::EmailBuild
|
||||||
content_type 'text/html; charset=UTF-8'
|
content_type 'text/html; charset=UTF-8'
|
||||||
|
|
||||||
# complete check
|
# complete check
|
||||||
html_document = Channel::EmailBuild.html_complete_check( attr[:body] )
|
html_document = Channel::EmailBuild.html_complete_check(attr[:body])
|
||||||
|
|
||||||
body html_document
|
body html_document
|
||||||
end
|
end
|
||||||
|
@ -110,11 +110,17 @@ module Channel::EmailBuild
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
full_html_document_string = Channel::EmailBuild.html_complete_check( html_string )
|
Check if string is a complete html document. If not, add head and css styles.
|
||||||
|
|
||||||
|
full_html_document_string = Channel::EmailBuild.html_complete_check(html_string)
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def self.html_complete_check(html)
|
def self.html_complete_check(html)
|
||||||
|
|
||||||
|
# apply mail client fixes
|
||||||
|
html = Channel::EmailBuild.html_mail_client_fixes(html)
|
||||||
|
|
||||||
return html if html =~ /<html>/i
|
return html if html =~ /<html>/i
|
||||||
|
|
||||||
css = "font-family:'Helvetica Neue', Helvetica, Arial, Geneva, sans-serif; font-size: 12px;"
|
css = "font-family:'Helvetica Neue', Helvetica, Arial, Geneva, sans-serif; font-size: 12px;"
|
||||||
|
@ -166,4 +172,20 @@ HERE
|
||||||
|
|
||||||
html
|
html
|
||||||
end
|
end
|
||||||
|
|
||||||
|
=begin
|
||||||
|
|
||||||
|
Add/change markup to display html in any mail client nice.
|
||||||
|
|
||||||
|
html_string_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_string)
|
||||||
|
|
||||||
|
=end
|
||||||
|
|
||||||
|
def self.html_mail_client_fixes(html)
|
||||||
|
|
||||||
|
# https://github.com/martini/zammad/issues/165
|
||||||
|
html.gsub('<blockquote type="cite">', '<blockquote type="cite" style="border-left: 2px solid blue; margin: 0px; padding: 8px 12px 8px 12px;">')
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,22 +5,22 @@ class EmailBuildTest < ActiveSupport::TestCase
|
||||||
test 'document complete check' do
|
test 'document complete check' do
|
||||||
|
|
||||||
html = '<b>test</b>'
|
html = '<b>test</b>'
|
||||||
result = Channel::EmailBuild.html_complete_check( html )
|
result = Channel::EmailBuild.html_complete_check(html)
|
||||||
|
|
||||||
assert( result =~ /^<\!DOCTYPE/, 'test 1')
|
assert(result =~ /^<\!DOCTYPE/, 'test 1')
|
||||||
assert( result !~ /^.+?<\!DOCTYPE/, 'test 1')
|
assert(result !~ /^.+?<\!DOCTYPE/, 'test 1')
|
||||||
assert( result =~ /<html>/, 'test 1')
|
assert(result =~ /<html>/, 'test 1')
|
||||||
assert( result =~ /font-family/, 'test 1')
|
assert(result =~ /font-family/, 'test 1')
|
||||||
assert( result =~ %r{<b>test</b>}, 'test 1')
|
assert(result =~ %r{<b>test</b>}, 'test 1')
|
||||||
|
|
||||||
html = 'invalid <!DOCTYPE html><html><b>test</b></html>'
|
html = 'invalid <!DOCTYPE html><html><b>test</b></html>'
|
||||||
result = Channel::EmailBuild.html_complete_check( html )
|
result = Channel::EmailBuild.html_complete_check(html)
|
||||||
|
|
||||||
assert( result !~ /^<\!DOCTYPE/, 'test 2')
|
assert(result !~ /^<\!DOCTYPE/, 'test 2')
|
||||||
assert( result =~ /^.+?<\!DOCTYPE/, 'test 2')
|
assert(result =~ /^.+?<\!DOCTYPE/, 'test 2')
|
||||||
assert( result =~ /<html>/, 'test 2')
|
assert(result =~ /<html>/, 'test 2')
|
||||||
assert( result !~ /font-family/, 'test 2')
|
assert(result !~ /font-family/, 'test 2')
|
||||||
assert( result =~ %r{<b>test</b>}, 'test 2')
|
assert(result =~ %r{<b>test</b>}, 'test 2')
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -52,33 +52,33 @@ class EmailBuildTest < ActiveSupport::TestCase
|
||||||
>
|
>
|
||||||
> Thank you for installing Zammad. äöüß
|
> Thank you for installing Zammad. äöüß
|
||||||
>'
|
>'
|
||||||
assert_equal( should, mail.text_part.body.to_s )
|
assert_equal(should, mail.text_part.body.to_s)
|
||||||
assert_equal( html, mail.html_part.body.to_s )
|
assert_equal(html, mail.html_part.body.to_s)
|
||||||
|
|
||||||
parser = Channel::EmailParser.new
|
parser = Channel::EmailParser.new
|
||||||
data = parser.parse( mail.to_s )
|
data = parser.parse(mail.to_s)
|
||||||
|
|
||||||
# check body
|
# check body
|
||||||
assert_equal( should, data[:body] )
|
assert_equal(should, data[:body])
|
||||||
|
|
||||||
# check count of attachments, only 2, because 3 part is text message and is already in body
|
# check count of attachments, only 2, because 3 part is text message and is already in body
|
||||||
assert_equal( 2, data[:attachments].length )
|
assert_equal(2, data[:attachments].length)
|
||||||
|
|
||||||
# check attachments
|
# check attachments
|
||||||
if data[:attachments]
|
if data[:attachments]
|
||||||
data[:attachments].each { |attachment|
|
data[:attachments].each { |attachment|
|
||||||
if attachment[:filename] == 'message.html'
|
if attachment[:filename] == 'message.html'
|
||||||
assert_equal( nil, attachment[:preferences]['Content-ID'] )
|
assert_equal(nil, attachment[:preferences]['Content-ID'])
|
||||||
assert_equal( true, attachment[:preferences]['content-alternative'] )
|
assert_equal(true, attachment[:preferences]['content-alternative'])
|
||||||
assert_equal( 'text/html', attachment[:preferences]['Mime-Type'] )
|
assert_equal('text/html', attachment[:preferences]['Mime-Type'])
|
||||||
assert_equal( 'UTF-8', attachment[:preferences]['Charset'] )
|
assert_equal('UTF-8', attachment[:preferences]['Charset'])
|
||||||
elsif attachment[:filename] == 'somename.png'
|
elsif attachment[:filename] == 'somename.png'
|
||||||
assert_equal( nil, attachment[:preferences]['Content-ID'] )
|
assert_equal(nil, attachment[:preferences]['Content-ID'])
|
||||||
assert_equal( nil, attachment[:preferences]['content-alternative'] )
|
assert_equal(nil, attachment[:preferences]['content-alternative'])
|
||||||
assert_equal( 'image/png', attachment[:preferences]['Mime-Type'] )
|
assert_equal('image/png', attachment[:preferences]['Mime-Type'])
|
||||||
assert_equal( 'UTF-8', attachment[:preferences]['Charset'] )
|
assert_equal('UTF-8', attachment[:preferences]['Charset'])
|
||||||
else
|
else
|
||||||
assert( false, "invalid attachment, should not be there, #{attachment.inspect}" )
|
assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -106,28 +106,28 @@ class EmailBuildTest < ActiveSupport::TestCase
|
||||||
>
|
>
|
||||||
> Thank you for installing Zammad. äöüß
|
> Thank you for installing Zammad. äöüß
|
||||||
>'
|
>'
|
||||||
assert_equal( should, mail.text_part.body.to_s )
|
assert_equal(should, mail.text_part.body.to_s)
|
||||||
assert_equal( nil, mail.html_part )
|
assert_equal(nil, mail.html_part)
|
||||||
|
|
||||||
parser = Channel::EmailParser.new
|
parser = Channel::EmailParser.new
|
||||||
data = parser.parse( mail.to_s )
|
data = parser.parse(mail.to_s)
|
||||||
|
|
||||||
# check body
|
# check body
|
||||||
assert_equal( should, data[:body] )
|
assert_equal(should, data[:body])
|
||||||
|
|
||||||
# check count of attachments, 2
|
# check count of attachments, 2
|
||||||
assert_equal( 1, data[:attachments].length )
|
assert_equal(1, data[:attachments].length)
|
||||||
|
|
||||||
# check attachments
|
# check attachments
|
||||||
if data[:attachments]
|
if data[:attachments]
|
||||||
data[:attachments].each { |attachment|
|
data[:attachments].each { |attachment|
|
||||||
if attachment[:filename] == 'somename.png'
|
if attachment[:filename] == 'somename.png'
|
||||||
assert_equal( nil, attachment[:preferences]['Content-ID'] )
|
assert_equal(nil, attachment[:preferences]['Content-ID'])
|
||||||
assert_equal( nil, attachment[:preferences]['content-alternative'] )
|
assert_equal(nil, attachment[:preferences]['content-alternative'])
|
||||||
assert_equal( 'image/png', attachment[:preferences]['Mime-Type'] )
|
assert_equal('image/png', attachment[:preferences]['Mime-Type'])
|
||||||
assert_equal( 'UTF-8', attachment[:preferences]['Charset'] )
|
assert_equal('UTF-8', attachment[:preferences]['Charset'])
|
||||||
else
|
else
|
||||||
assert( false, "invalid attachment, should not be there, #{attachment.inspect}" )
|
assert(false, "invalid attachment, should not be there, #{attachment.inspect}")
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -148,17 +148,47 @@ class EmailBuildTest < ActiveSupport::TestCase
|
||||||
>
|
>
|
||||||
> Thank you for installing Zammad. äöüß
|
> Thank you for installing Zammad. äöüß
|
||||||
>'
|
>'
|
||||||
assert_equal( should, mail.body.to_s )
|
assert_equal(should, mail.body.to_s)
|
||||||
assert_equal( nil, mail.html_part )
|
assert_equal(nil, mail.html_part)
|
||||||
|
|
||||||
parser = Channel::EmailParser.new
|
parser = Channel::EmailParser.new
|
||||||
data = parser.parse( mail.to_s )
|
data = parser.parse(mail.to_s)
|
||||||
|
|
||||||
# check body
|
# check body
|
||||||
assert_equal( should, data[:body] )
|
assert_equal(should, data[:body])
|
||||||
|
|
||||||
# check count of attachments, 0
|
# check count of attachments, 0
|
||||||
assert_equal( 0, data[:attachments].length )
|
assert_equal(0, data[:attachments].length)
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'email - html email client fixes' do
|
||||||
|
|
||||||
|
# https://github.com/martini/zammad/issues/165
|
||||||
|
html_raw = '<blockquote type="cite">some
|
||||||
|
text
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
123
|
||||||
|
|
||||||
|
<blockquote type="cite">some
|
||||||
|
text
|
||||||
|
</blockquote>'
|
||||||
|
html_with_fixes = Channel::EmailBuild.html_mail_client_fixes(html_raw)
|
||||||
|
|
||||||
|
assert_not_equal(html_with_fixes, html_raw)
|
||||||
|
|
||||||
|
html_should = '<blockquote type="cite" style="border-left: 2px solid blue; margin: 0px; padding: 8px 12px 8px 12px;">some
|
||||||
|
text
|
||||||
|
</blockquote>
|
||||||
|
|
||||||
|
123
|
||||||
|
|
||||||
|
<blockquote type="cite" style="border-left: 2px solid blue; margin: 0px; padding: 8px 12px 8px 12px;">some
|
||||||
|
text
|
||||||
|
</blockquote>'
|
||||||
|
|
||||||
|
assert_equal(html_should, html_with_fixes)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue