Class: Mail::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/jpmobile/mail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mobileObject

Returns the value of attribute mobile


15
16
17
# File 'lib/jpmobile/mail.rb', line 15

def mobile
  @mobile
end

Instance Method Details

#add_charset_with_jpmobileObject Also known as: add_charset


121
122
123
# File 'lib/jpmobile/mail.rb', line 121

def add_charset_with_jpmobile
  add_charset_without_jpmobile unless multipart? && @mobile
end

#encoded_with_jpmobileObject Also known as: encoded


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jpmobile/mail.rb', line 23

def encoded_with_jpmobile
  if @mobile
    header['subject'].mobile = @mobile if header['subject']
    header['from'].mobile    = @mobile if header['from']
    header['to'].mobile      = @mobile if header['to']
    self.charset             = @mobile.mail_charset unless multipart?

    ready_to_send!

    self.body.mobile  = @mobile
    self.header['Content-Transfer-Encoding'].value = @mobile.content_transfer_encoding(self.header)
    if @mobile.decorated?
      unless self.content_type.match(/image\//)
        self.header['Content-ID'] = nil
      end

      unless self.header['Content-Type'].sub_type == 'mixed'
        self.header['Date']         = nil
        self.header['Mime-Version'] = nil
      end
    end

    buffer = header.encoded
    buffer << "\r\n"
    buffer = @mobile.utf8_to_mail_encode(buffer)
    buffer << body.encoded(content_transfer_encoding)

    ascii_compatible!(buffer)
  else
    encoded_without_jpmobile
  end
end

#find_part_by_content_type(content_type) ⇒ Object


215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/jpmobile/mail.rb', line 215

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

#init_with_hash_with_jpmobile(hash) ⇒ Object Also known as: init_with_hash


69
70
71
72
73
74
75
76
# File 'lib/jpmobile/mail.rb', line 69

def init_with_hash_with_jpmobile(hash)
  if hash[:body_raw]
    @mobile = hash[:mobile]
    init_with_string(hash[:body_raw])
  else
    init_with_hash_without_jpmobile(hash)
  end
end

#init_with_string(string) ⇒ Object


81
82
83
84
85
86
87
88
# File 'lib/jpmobile/mail.rb', line 81

def init_with_string(string)
  # convert to ASCII-8BIT for ascii incompatible encodings
  s = Jpmobile::Util.ascii_8bit(string)
  self.raw_source = s
  set_envelope_header
  parse_message
  @separate_parts = multipart?
end

#parse_message_with_jpmobileObject Also known as: parse_message


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jpmobile/mail.rb', line 56

def parse_message_with_jpmobile
  header_part, body_part = raw_source.lstrip.split(/#{CRLF}#{CRLF}|#{CRLF}#{WSP}*#{CRLF}(?!#{WSP})/m, 2)
  # header_part, body_part = raw_source.lstrip.split(HEADER_SEPARATOR, 2)

  self.header = header_part

  @body_part_jpmobile = body_part
  convert_encoding_jpmobile
  body_part = @body_part_jpmobile

  self.body   = body_part
end

#process_body_raw_with_jpmobileObject Also known as: process_body_raw


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jpmobile/mail.rb', line 90

def process_body_raw_with_jpmobile
  process_body_raw_without_jpmobile

  if @mobile
    @body.mobile = @mobile
    @body.content_type_with_jpmobile = self.content_type

    if has_content_transfer_encoding? and
        ["base64", "quoted-printable"].include?(self.content_transfer_encoding) and
        ["text"].include?(@mobile_main_type)
      @body.decode_transfer_encoding
    end

    if @body.multipart?
      @body.parts.each do |p|
        p.mobile  = @mobile
      end
    end
  end
end

#raw_source=(value) ⇒ Object

In jpmobile, value is already transfered correctly encodings.


112
113
114
# File 'lib/jpmobile/mail.rb', line 112

def raw_source=(value)
  @raw_source = value.to_crlf
end

#rearrange!Object

– normal multipart/mixed

|- multipart/alternative
|    |- text/plain
|    |- text/html
|    |- image/xxxx (インライン画像)
|- image/xxxx (添付画像)

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/jpmobile/mail.rb', line 165

def rearrange!
  if @mobile and @mobile.decoratable?
    @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 = []
    attachments.each do |p|
      if p.content_type.match(/^image\//)  and p.content_disposition.match(/^inline/)
        if p.header['Content-Type'].parameters['filename']
          p.header['Content-Type'].parameters['name'] = p.header['Content-Type'].parameters['filename'].to_s
        end
        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) 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

    unless self.header['Content-Type'].sub_type == 'mixed'
      self.header['Content-Type'] = self.content_type.gsub(/#{self.header['Content-Type'].sub_type}/, 'mixed')
    end
    self.parts.clear
    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

#separate_parts_with_jpmobileObject Also known as: separate_parts


116
117
118
119
# File 'lib/jpmobile/mail.rb', line 116

def separate_parts_with_jpmobile
  @body.mobile = @mobile
  separate_parts_without_jpmobile
end