From 80307158b640c4a6666d932b5c91ea1a77997334 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 27 Dec 2014 16:25:58 +0100 Subject: [PATCH] Improved sending emails. --- app/models/channel/email_build.rb | 48 +++++-------------------------- app/models/channel/sendmail.rb | 3 +- app/models/channel/smtp.rb | 5 ++-- lib/core_ext/string.rb | 40 +++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 45 deletions(-) diff --git a/app/models/channel/email_build.rb b/app/models/channel/email_build.rb index 98424986a..26e2f7f1b 100644 --- a/app/models/channel/email_build.rb +++ b/app/models/channel/email_build.rb @@ -2,15 +2,15 @@ require 'mail' -class Channel::EmailBuild +module Channel::EmailBuild =begin - backend = Channel::EmailBuild.new - mail = backend.send( - :from => 'sender@example.com', - :to => 'recipient@example.com', - :body => 'somebody with some text', + mail = Channel::EmailBuild.build( + :from => 'sender@example.com', + :to => 'recipient@example.com', + :body => 'somebody with some text', + :content_type => 'text/plain', ) =end @@ -50,7 +50,7 @@ class Channel::EmailBuild end # generate plain part - attr[:body] = html2text( attr[:body] ) + attr[:body] = attr[:body].html2text end # add plain text part @@ -71,38 +71,4 @@ class Channel::EmailBuild end mail end - - private - - # from https://gist.github.com/petrblaho/657856 - def html2text(html) - text = html. - gsub(/( |\n|\s)+/im, ' ').squeeze(' ').strip. - gsub(/<([^\s]+)[^>]*(src|href)=\s*(.?)([^>\s]*)\3[^>]*>\4<\/\1>/i, '\4') - - links = [] - linkregex = /<[^>]*(src|href)=\s*(.?)([^>\s]*)\2[^>]*>\s*/i - while linkregex.match(text) - links << $~[3] - text.sub!(linkregex, "[#{links.size}]") - end - - text = CGI.unescapeHTML( - text. - gsub(/<(script|style)[^>]*>.*<\/\1>/im, ''). - gsub(//m, ''). - gsub(/]*)>/i, "___\n"). - gsub(/]*)>/i, "\n* "). - gsub(/]*)>/i, '> '). - gsub(/<(br)(| [^>]*)>/i, "\n"). - gsub(/<(\/h[\d]+|p)(| [^>]*)>/i, "\n\n"). - gsub(/<[^>]*>/, '') - ).lstrip.gsub(/\n[ ]+/, "\n") + "\n" - - for i in (0...links.size).to_a - text = text + "\n [#{i+1}] <#{CGI.unescapeHTML(links[i])}>" unless links[i].nil? - end - links = nil - text - end end \ No newline at end of file diff --git a/app/models/channel/sendmail.rb b/app/models/channel/sendmail.rb index b32d46ce1..12326324b 100644 --- a/app/models/channel/sendmail.rb +++ b/app/models/channel/sendmail.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -class Channel::Sendmail < Channel::EmailBuild +class Channel::Sendmail + include Channel::EmailBuild def send(attr, channel, notification = false) # return if we run import mode diff --git a/app/models/channel/smtp.rb b/app/models/channel/smtp.rb index 86d2a9edb..75a53a8eb 100644 --- a/app/models/channel/smtp.rb +++ b/app/models/channel/smtp.rb @@ -1,12 +1,13 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ -class Channel::SMTP < Channel::EmailBuild +class Channel::SMTP + include Channel::EmailBuild def send(attr, channel, notification = false) # return if we run import mode return if Setting.get('import_mode') - mail = build(attr, notification) + mail = self.build(attr, notification) mail.delivery_method :smtp, { :openssl_verify_mode => 'none', :address => channel[:options][:host], diff --git a/lib/core_ext/string.rb b/lib/core_ext/string.rb index e93564fe2..0d7f77e23 100644 --- a/lib/core_ext/string.rb +++ b/lib/core_ext/string.rb @@ -38,4 +38,42 @@ class String } .join('') end -end + +=begin + + text = html_string.html2text + +=end + + # from https://gist.github.com/petrblaho/657856 + def html2text + text = self. + gsub(/( |\n|\s)+/im, ' ').squeeze(' ').strip. + gsub(/<([^\s]+)[^>]*(src|href)=\s*(.?)([^>\s]*)\3[^>]*>\4<\/\1>/i, '\4') + + links = [] + linkregex = /<[^>]*(src|href)=\s*(.?)([^>\s]*)\2[^>]*>\s*/i + while linkregex.match(text) + links << $~[3] + text.sub!(linkregex, "[#{links.size}]") + end + + text = CGI.unescapeHTML( + text. + gsub(/<(script|style)[^>]*>.*<\/\1>/im, ''). + gsub(//m, ''). + gsub(/]*)>/i, "___\n"). + gsub(/]*)>/i, "\n* "). + gsub(/]*)>/i, '> '). + gsub(/<(br)(| [^>]*)>/i, "\n"). + gsub(/<(\/h[\d]+|p)(| [^>]*)>/i, "\n\n"). + gsub(/<[^>]*>/, '') + ).lstrip.gsub(/\n[ ]+/, "\n") + "\n" + + for i in (0...links.size).to_a + text = text + "\n [#{i+1}] <#{CGI.unescapeHTML(links[i])}>" unless links[i].nil? + end + links = nil + text + end +end \ No newline at end of file