Improved sending emails.

This commit is contained in:
Martin Edenhofer 2014-12-27 16:25:58 +01:00
parent b582794ea7
commit 80307158b6
4 changed files with 51 additions and 45 deletions

View file

@ -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(/<hr(| [^>]*)>/i, "___\n").
gsub(/<li(| [^>]*)>/i, "\n* ").
gsub(/<blockquote(| [^>]*)>/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

View file

@ -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

View file

@ -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],

View file

@ -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(/(&nbsp;|\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(/<hr(| [^>]*)>/i, "___\n").
gsub(/<li(| [^>]*)>/i, "\n* ").
gsub(/<blockquote(| [^>]*)>/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