Added support of html emails.
This commit is contained in:
parent
cfc06fe1a8
commit
b582794ea7
2 changed files with 74 additions and 15 deletions
|
@ -4,6 +4,17 @@ require 'mail'
|
||||||
|
|
||||||
class Channel::EmailBuild
|
class Channel::EmailBuild
|
||||||
|
|
||||||
|
=begin
|
||||||
|
|
||||||
|
backend = Channel::EmailBuild.new
|
||||||
|
mail = backend.send(
|
||||||
|
:from => 'sender@example.com',
|
||||||
|
:to => 'recipient@example.com',
|
||||||
|
:body => 'somebody with some text',
|
||||||
|
)
|
||||||
|
|
||||||
|
=end
|
||||||
|
|
||||||
def build(attr, notification = false)
|
def build(attr, notification = false)
|
||||||
mail = Mail.new
|
mail = Mail.new
|
||||||
|
|
||||||
|
@ -20,18 +31,31 @@ class Channel::EmailBuild
|
||||||
attr['Auto-Submitted'] = 'auto-generated'
|
attr['Auto-Submitted'] = 'auto-generated'
|
||||||
end
|
end
|
||||||
|
|
||||||
attr['X-Powered-BY'] = 'OTRS - Open Ticket Request System (http://otrs.org/)'
|
#attr['X-Powered-BY'] = 'Zammad - Support/Helpdesk (http://www.zammad.org/)'
|
||||||
attr['X-Mailer'] = 'OTRS Mail Service (3.x)'
|
attr['X-Mailer'] = 'Zammad Mail Service (1.x)'
|
||||||
|
|
||||||
# set headers
|
# set headers
|
||||||
attr.each do |key, v|
|
attr.each do |key, value|
|
||||||
if key.to_s != 'attachments' && key.to_s != 'body'
|
next if key.to_s == 'attachments'
|
||||||
mail[key.to_s] = v.to_s
|
next if key.to_s == 'body'
|
||||||
end
|
next if key.to_s == 'content_type'
|
||||||
|
mail[key.to_s] = value.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
# add body
|
# add html part
|
||||||
|
if attr[:content_type] && attr[:content_type] == 'text/html'
|
||||||
|
mail.html_part = Mail::Part.new do
|
||||||
|
content_type 'text/html; charset=UTF-8'
|
||||||
|
body attr[:body]
|
||||||
|
end
|
||||||
|
|
||||||
|
# generate plain part
|
||||||
|
attr[:body] = html2text( attr[:body] )
|
||||||
|
end
|
||||||
|
|
||||||
|
# add plain text part
|
||||||
mail.text_part = Mail::Part.new do
|
mail.text_part = Mail::Part.new do
|
||||||
|
content_type 'text/plain; charset=UTF-8'
|
||||||
body attr[:body]
|
body attr[:body]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -47,4 +71,38 @@ class Channel::EmailBuild
|
||||||
end
|
end
|
||||||
mail
|
mail
|
||||||
end
|
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
|
end
|
|
@ -19,6 +19,7 @@ class Observer::Ticket::Article::CommunicateEmail::BackgroundJob
|
||||||
:to => record.to,
|
:to => record.to,
|
||||||
:cc => record.cc,
|
:cc => record.cc,
|
||||||
:subject => subject,
|
:subject => subject,
|
||||||
|
:content_type => record.content_type,
|
||||||
:body => record.body,
|
:body => record.body,
|
||||||
:attachments => record.attachments
|
:attachments => record.attachments
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue