From b582794ea7e0ee93e33e4ea7e42e8b7253a2b2bb Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 27 Dec 2014 15:07:49 +0100 Subject: [PATCH] Added support of html emails. --- app/models/channel/email_build.rb | 72 +++++++++++++++++-- .../communicate_email/background_job.rb | 17 ++--- 2 files changed, 74 insertions(+), 15 deletions(-) diff --git a/app/models/channel/email_build.rb b/app/models/channel/email_build.rb index f20f5eeab..98424986a 100644 --- a/app/models/channel/email_build.rb +++ b/app/models/channel/email_build.rb @@ -4,6 +4,17 @@ require 'mail' 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) mail = Mail.new @@ -20,18 +31,31 @@ class Channel::EmailBuild attr['Auto-Submitted'] = 'auto-generated' end - attr['X-Powered-BY'] = 'OTRS - Open Ticket Request System (http://otrs.org/)' - attr['X-Mailer'] = 'OTRS Mail Service (3.x)' + #attr['X-Powered-BY'] = 'Zammad - Support/Helpdesk (http://www.zammad.org/)' + attr['X-Mailer'] = 'Zammad Mail Service (1.x)' # set headers - attr.each do |key, v| - if key.to_s != 'attachments' && key.to_s != 'body' - mail[key.to_s] = v.to_s - end + attr.each do |key, value| + next if key.to_s == 'attachments' + next if key.to_s == 'body' + next if key.to_s == 'content_type' + mail[key.to_s] = value.to_s 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 + content_type 'text/plain; charset=UTF-8' body attr[:body] end @@ -47,4 +71,38 @@ 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/observer/ticket/article/communicate_email/background_job.rb b/app/models/observer/ticket/article/communicate_email/background_job.rb index a3aebde6c..aecfba297 100644 --- a/app/models/observer/ticket/article/communicate_email/background_job.rb +++ b/app/models/observer/ticket/article/communicate_email/background_job.rb @@ -13,14 +13,15 @@ class Observer::Ticket::Article::CommunicateEmail::BackgroundJob a = Channel::EmailSend.new message = a.send( { - :message_id => record.message_id, - :in_reply_to => record.in_reply_to, - :from => record.from, - :to => record.to, - :cc => record.cc, - :subject => subject, - :body => record.body, - :attachments => record.attachments + :message_id => record.message_id, + :in_reply_to => record.in_reply_to, + :from => record.from, + :to => record.to, + :cc => record.cc, + :subject => subject, + :content_type => record.content_type, + :body => record.body, + :attachments => record.attachments } )