From 6bbd505f5e08635a23d3abbf4371e679d4f21c51 Mon Sep 17 00:00:00 2001 From: Lucas Nussbaum Date: Tue, 17 Jun 2008 10:46:57 +0200 Subject: generate correct messages with images --- lib/feed2imap/itemtomail.rb | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'lib/feed2imap') diff --git a/lib/feed2imap/itemtomail.rb b/lib/feed2imap/itemtomail.rb index 0bd7420..06efbeb 100644 --- a/lib/feed2imap/itemtomail.rb +++ b/lib/feed2imap/itemtomail.rb @@ -47,7 +47,7 @@ class String end end -def item_to_mail(item, index, updated, from = 'Feed2Imap', include_images = false) +def item_to_mail(item, index, updated, from = 'Feed2Imap', inline_images = false) message = RMail::Message::new if item.creator and item.creator != '' if item.creator.include?('@') @@ -87,15 +87,25 @@ def item_to_mail(item, index, updated, from = 'Feed2Imap', include_images = fals # inline images as attachments if inline_images + message.header.set('Content-Type', 'multipart/related', 'type'=> 'multipart/alternative') + texthtml = RMail::Message::new + texthtml.header.set('Content-Type', 'multipart/alternative') + texthtml.add_part(textpart) + texthtml.add_part(htmlpart) + message.add_part(texthtml) + htmlpart.body.gsub!(/(]+)src="(\S+?\/([^\/]+?\.(png|gif|jpe?g)))"([^>]*>)/i) do |match| # $2 contains url, $3 the image name, $4 the image extension begin - image = Base64.encode64(HTTPFetcher::fetch($2, Time.at(0))) + image = Base64.encode64(HTTPFetcher::fetch($2, Time.at(0)).chomp) + "\n" cid = "#{Digest::MD5.hexdigest($2)}@feed2imap.acme.com" imgpart = RMail::Message.new - imgpart.header.set('Content-Type', "image/#{$4}", 'name' => $3) - imgpart.header.set('Content-Transfer-Encoding', 'base64') imgpart.header.set('Content-ID', "<#{cid}>") + type = $4 + type = 'jpeg' if type.downcase == 'jpg' # hack hack hack + imgpart.header.set('Content-Type', "image/#{type}", 'name' => $3) + imgpart.header.set('Content-Disposition', 'attachment', 'filename' => $3) + imgpart.header.set('Content-Transfer-Encoding', 'base64') imgpart.body = image message.add_part(imgpart) # now to specify what to replace with @@ -107,10 +117,11 @@ def item_to_mail(item, index, updated, from = 'Feed2Imap', include_images = fals $& # don't modify on exception end end + else + message.header['Content-Type'] = 'multipart/alternative' + message.add_part(textpart) + message.add_part(htmlpart) end - - message.add_part(textpart) - message.add_part(htmlpart) return message.to_s end -- cgit v1.2.3-54-g00ecf