Method: Mail::Body#encoded

Defined in:
lib/mail/body.rb

#encoded(transfer_encoding = nil) ⇒ Object

Returns a body encoded using transfer_encoding. Multipart always uses an identiy encoding (i.e. no encoding). Calling this directly is not a good idea, but supported for compatibility TODO: Validate that preamble and epilogue are valid for requested encoding



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/mail/body.rb', line 150

def encoded(transfer_encoding = nil)
  if multipart?
    self.sort_parts!
    encoded_parts = parts.map { |p| p.encoded }
    ([preamble] + encoded_parts).join(crlf_boundary) + end_boundary + epilogue.to_s
  else
    dec = Mail::Encodings.get_encoding(encoding)
    enc =
      if Utilities.blank?(transfer_encoding)
        dec
      else
        negotiate_best_encoding(transfer_encoding)
      end

    if dec.nil?
      # Cannot decode, so skip normalization
      raw_source
    else
      # Decode then encode to normalize and allow transforming 
      # from base64 to Q-P and vice versa
      decoded = dec.decode(raw_source)
      if defined?(Encoding) && charset && charset != "US-ASCII"
        decoded = decoded.encode(charset)
        decoded.force_encoding('BINARY') unless Encoding.find(charset).ascii_compatible?
      end
      enc.encode(decoded)
    end
  end
end