##// END OF EJS Templates
Mail handler: strip tags when receiving a html-only email (#2312)....
Jean-Philippe Lang -
r2134:3bb2fccaf11d
parent child
Show More
@@ -0,0 +1,22
1 x-sender: <jsmith@somenet.foo>
2 x-receiver: <redmine@somenet.foo>
3 Received: from [127.0.0.1] ([127.0.0.1]) by somenet.foo with Quick 'n Easy Mail Server SMTP (1.0.0.0);
4 Sun, 14 Dec 2008 16:18:06 GMT
5 Message-ID: <494531B9.1070709@somenet.foo>
6 Date: Sun, 14 Dec 2008 17:18:01 +0100
7 From: "John Smith" <jsmith@somenet.foo>
8 User-Agent: Thunderbird 2.0.0.18 (Windows/20081105)
9 MIME-Version: 1.0
10 To: redmine@somenet.foo
11 Subject: HTML email
12 Content-Type: text/html; charset=ISO-8859-1
13 Content-Transfer-Encoding: 7bit
14
15 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
16 <html>
17 <head>
18 </head>
19 <body bgcolor="#ffffff" text="#000000">
20 This is a <b>html-only</b> email.<br>
21 </body>
22 </html>
@@ -16,6 +16,7
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class MailHandler < ActionMailer::Base
18 class MailHandler < ActionMailer::Base
19 include ActionView::Helpers::SanitizeHelper
19
20
20 class UnauthorizedAction < StandardError; end
21 class UnauthorizedAction < StandardError; end
21 class MissingInformation < StandardError; end
22 class MissingInformation < StandardError; end
@@ -88,7 +89,7 class MailHandler < ActionMailer::Base
88 issue.status = status
89 issue.status = status
89 end
90 end
90 issue.subject = email.subject.chomp.toutf8
91 issue.subject = email.subject.chomp.toutf8
91 issue.description = email.plain_text_body.chomp
92 issue.description = plain_text_body
92 issue.save!
93 issue.save!
93 add_attachments(issue)
94 add_attachments(issue)
94 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
95 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
@@ -120,7 +121,7 class MailHandler < ActionMailer::Base
120 raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project)
121 raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project)
121
122
122 # add the note
123 # add the note
123 journal = issue.init_journal(user, email.plain_text_body.chomp)
124 journal = issue.init_journal(user, plain_text_body)
124 add_attachments(issue)
125 add_attachments(issue)
125 # check workflow
126 # check workflow
126 if status && issue.new_statuses_allowed_to(user).include?(status)
127 if status && issue.new_statuses_allowed_to(user).include?(status)
@@ -156,21 +157,30 class MailHandler < ActionMailer::Base
156 end
157 end
157
158
158 def get_keyword(attr)
159 def get_keyword(attr)
159 if @@handler_options[:allow_override].include?(attr.to_s) && email.plain_text_body =~ /^#{attr}:[ \t]*(.+)$/i
160 if @@handler_options[:allow_override].include?(attr.to_s) && plain_text_body =~ /^#{attr}:[ \t]*(.+)$/i
160 $1.strip
161 $1.strip
161 elsif !@@handler_options[:issue][attr].blank?
162 elsif !@@handler_options[:issue][attr].blank?
162 @@handler_options[:issue][attr]
163 @@handler_options[:issue][attr]
163 end
164 end
164 end
165 end
165 end
166
166
167 # Returns the text/plain part of the email
167 class TMail::Mail
168 # If not found (eg. HTML-only email), returns the body with tags removed
168 # Returns body of the first plain text part found if any
169 def plain_text_body
169 def plain_text_body
170 return @plain_text_body unless @plain_text_body.nil?
170 return @plain_text_body unless @plain_text_body.nil?
171 p = self.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten
171 parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten
172 plain = p.detect {|c| c.content_type == 'text/plain'}
172 if parts.empty?
173 @plain_text_body = plain.nil? ? self.body : plain.body
173 parts << @email
174 end
175 plain_text_part = parts.detect {|p| p.content_type == 'text/plain'}
176 if plain_text_part.nil?
177 # no text/plain part found, assuming html-only email
178 # strip html tags and remove doctype directive
179 @plain_text_body = strip_tags(@email.body.to_s)
180 @plain_text_body.gsub! %r{^<!DOCTYPE .*$}, ''
181 else
182 @plain_text_body = plain_text_part.body.to_s
183 end
184 @plain_text_body.strip!
174 end
185 end
175 end
186 end
176
@@ -129,6 +129,15 class MailHandlerTest < Test::Unit::TestCase
129 assert_match /This is reply/, journal.notes
129 assert_match /This is reply/, journal.notes
130 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
130 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
131 end
131 end
132
133 def test_should_strip_tags_of_html_only_emails
134 issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
135 assert issue.is_a?(Issue)
136 assert !issue.new_record?
137 issue.reload
138 assert_equal 'HTML email', issue.subject
139 assert_equal 'This is a html-only email.', issue.description
140 end
132
141
133 private
142 private
134
143
General Comments 0
You need to be logged in to leave comments. Login now