Method: Mail::Encodings.encode_non_usascii

Defined in:
lib/mail/encodings.rb

.encode_non_usascii(address, charset) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mail/encodings.rb', line 170

def Encodings.encode_non_usascii(address, charset)
  return address if address.ascii_only? or charset.nil?

  # Encode all strings embedded inside of quotes
  address = address.gsub(/("[^"]*[^\/]")/) { |s| Encodings.b_value_encode(unquote(s), charset) }

  # Then loop through all remaining items and encode as needed
  tokens = address.split(/\s/)

  map_with_index(tokens) do |word, i|
    if word.ascii_only?
      word
    else
      previous_non_ascii = i>0 && tokens[i-1] && !tokens[i-1].ascii_only?
      if previous_non_ascii #why are we adding an extra space here?
        word = " #{word}"
      end
      Encodings.b_value_encode(word, charset)
    end
  end.join(' ')
end