Skip to content

Commit

Permalink
New feature : sending decorated mails to mobile phone
Browse files Browse the repository at this point in the history
  • Loading branch information
rust committed Oct 20, 2011
1 parent 2ca358a commit f6151b5
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 8 deletions.
40 changes: 34 additions & 6 deletions lib/jpmobile/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def encoded_with_jpmobile
self.body.charset = @charset
self.body.mobile = @mobile
self.header['Content-Transfer-Encoding'] = @mobile.content_transfer_encoding(self.header)
self.header['Content-ID'] = nil if @mobile.decorated? and !self.content_type.match(/image\//)

buffer = header.encoded
buffer << "\r\n"
Expand Down Expand Up @@ -164,28 +165,32 @@ def body_lazy_with_jpmobile(value, index)

def rearrange!
if @mobile and @mobile.decoratable?
text_body_part = self.parts.find{|p| p.content_type.match(/^text\/plain/)}
html_body_part = self.parts.find{|p| p.content_type.match(/^text\/html/)}
@mobile.decorated = true
text_body_part = find_part_by_content_type("text/plain").first
html_body_part = find_part_by_content_type("text/html").first
html_body_part.transport_encoding = 'quoted-printable' if html_body_part
inline_images = []
attached_files = []
self.parts.each do |p|
attachments.each do |p|
if p.content_type.match(/^image\//) and p.content_disposition.match(/^inline/)
p.header['Content-Disposition'] = nil
inline_images << p
elsif p.content_disposition
attached_files << p
end
end

alternative_part = Mail::Part.new{content_type 'multipart/alternative'}
alternative_part.add_part(text_body_part)
alternative_part.add_part(html_body_part)
alternative_part.add_part(text_body_part) if text_body_part
alternative_part.add_part(html_body_part) if html_body_part

if @mobile.require_related_part?
related_part = Mail::Part.new{content_type 'multipart/related'}
related_part.add_part(alternative_part)
inline_images.each do |inline_image|
related_part.add_part(inline_image)
end
inline_images.clear
else
related_part = alternative_part
end
Expand All @@ -197,12 +202,29 @@ def rearrange!
self.body = nil

self.add_part(related_part)
inline_images.each do |inline_image|
self.add_part(inline_image)
end
attached_files.each do |attached_file|
self.add_part(attached_file)
end
end
end

def find_part_by_content_type(content_type)
finded_parts = []

self.parts.each do |part|
if part.multipart?
finded_parts << part.find_part_by_content_type(content_type)
elsif part.content_type.match(/^#{content_type}/)
finded_parts << part
end
end

finded_parts.flatten
end

private
def convert_encoding_jpmobile
# decide mobile carrier
Expand Down Expand Up @@ -289,7 +311,13 @@ def encoded_with_jpmobile(transfer_encoding = '8bit')
enc = Mail::Encodings::get_encoding(get_best_encoding(transfer_encoding))
Jpmobile::Util.force_encode(enc.encode(@raw_source), nil, @charset)
else
@mobile.to_mail_body(Jpmobile::Util.force_encode(@raw_source, @charset, Jpmobile::Util::UTF8))
if transfer_encoding == 'quoted-printable'
# [str].pack("M").gsub(/\n/, "\r\n")
Jpmobile::Util.force_encode([@mobile.to_mail_body(Jpmobile::Util.force_encode(@raw_source, @charset, Jpmobile::Util::UTF8))].pack("M").gsub(/\n/, "\r\n"), Jpmobile::Util::BINARY, @charset)
# @mobile.to_mail_body(Jpmobile::Util.force_encode(@raw_source, @charset, Jpmobile::Util::UTF8))
else
@mobile.to_mail_body(Jpmobile::Util.force_encode(@raw_source, @charset, Jpmobile::Util::UTF8))
end
end
else
encoded_without_jpmobile(transfer_encoding)
Expand Down
5 changes: 5 additions & 0 deletions lib/jpmobile/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ def mail(headers={}, &block)
end
self.lookup_context.mobile = @mobile.variants

@mobile.decorated = headers.delete(:decorated)

m = super(headers, &block)

m.mobile = @mobile
m.charset = @mobile.mail_charset

# for decorated-mail manipulation
m.rearrange! if @mobile.decorated?

m
end

Expand Down
15 changes: 14 additions & 1 deletion lib/jpmobile/mobile/abstract_mobile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,15 @@ def mail_charset(charset = nil)
end
def content_transfer_encoding(headers)
transfer_encoding = headers['Content-Transfer-Encoding']
if headers['Content-Type'].to_s.match(/text/)
case headers['Content-Type'].to_s
when /text\/plain/
transfer_encoding.to_s == MAIL_CONTENT_TRANSFER_ENCODING ? transfer_encoding : MAIL_CONTENT_TRANSFER_ENCODING
when /text\/html/
if self.decorated?
'quoted-printable'
else
transfer_encoding.to_s == MAIL_CONTENT_TRANSFER_ENCODING ? transfer_encoding : MAIL_CONTENT_TRANSFER_ENCODING
end
else
transfer_encoding
end
Expand Down Expand Up @@ -160,6 +167,12 @@ def decoratable?
def require_related_part?
false
end
def decorated=(boolean)
@decorated = boolean
end
def decorated?
@decorated
end

# リクエストがこのクラスに属するか調べる
# メソッド名に関して非常に不安
Expand Down
11 changes: 11 additions & 0 deletions test/rails/overrides/app/mailers/decorated_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
class DecoratedMailer < Jpmobile::Mailer::Base
default :from => "info@jp.mobile"
default :to => "info@jp.mobile"

def deco_mail(to_mail)
attachments.inline['photo.jpg'] = open(File.join(Rails.root, 'spec/fixtures/mobile_mailer/photo.jpg')).read

mail(:to => to_mail, :subject => '題名', :decorated => true)
end
end
10 changes: 10 additions & 0 deletions test/rails/overrides/app/views/decorated_mailer/deco_mail.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<body>
<p>
HTMLメールの本文
</p>
<p>
<%= image_tag attachments['photo.jpg'].url -%>
</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
テキストメールの本文
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions test/rails/overrides/spec/mailers/decorated_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
require File.expand_path(File.join(File.dirname(__FILE__), '/../spec_helper'))

describe DecoratedMailer do
include Jpmobile::Util

before(:each) do
ActionMailer::Base.deliveries = []
end

shared_examples_for "content-type" do
it "sends decorated mail successfully" do
DecoratedMailer.deco_mail(@to).deliver

email = ActionMailer::Base.deliveries.first
email.header['Content-Type'].main_type.should == 'multipart'
email.header['Content-Type'].sub_type.should == 'mixed'
end
end

describe "docomo" do
before(:each) do
@to = "docomo@docomo.ne.jp"
end

it_behaves_like "content-type"
end

describe "au" do
before(:each) do
@to = "au@ezweb.ne.jp"
end

it_behaves_like "content-type"
end

describe "softbank" do
before(:each) do
@to = "softbank@softbank.ne.jp"
end

it_behaves_like "content-type"
end
end
2 changes: 1 addition & 1 deletion test/rails/overrides/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
config.run_all_when_everything_filtered = true
config.color_enabled = true
config.filter_run_excluding :broken => true
config.full_backtrace = true
# config.full_backtrace = true
end

require 'pp'

0 comments on commit f6151b5

Please sign in to comment.