Module: Mail::Encodings

Includes:
Patterns
Defined in:
lib/mail/encodings/base64.rb,
lib/mail/encodings/encodings.rb,
lib/mail/encodings/quoted_printable.rb

Defined Under Namespace

Classes: Base64, QuotedPrintable

Constant Summary

Constants included from Patterns

Patterns::ATOM_UNSAFE, Patterns::CONTROL_CHAR, Patterns::CRLF, Patterns::FIELD_BODY, Patterns::FIELD_LINE, Patterns::FIELD_NAME, Patterns::FWS, Patterns::HEADER_LINE, Patterns::PHRASE_UNSAFE, Patterns::TEXT, Patterns::TOKEN_UNSAFE, Patterns::WSP

Class Method Summary collapse

Methods included from Patterns

included

Class Method Details

.b_value_encode(str, encoding = nil) ⇒ Object

Encode a string with Base64 Encoding and returns it ready to be inserted as a value for a field, that is, in the =?<charset>?B?<string>?= format

Example:

Encodings.b_value_encode('This is あ string', 'UTF-8') 
#=> "=?UTF-8?B?VGhpcyBpcyDjgYIgc3RyaW5n?="


146
147
148
149
150
151
# File 'lib/mail/encodings/encodings.rb', line 146

def Encodings.b_value_encode(str, encoding = nil)
  string, encoding = RubyVer.b_value_encode(str, encoding)
  string.split("\n").map do |str|
    "=?#{encoding}?B?#{str.chomp}?="
  end.join(" ")
end

.decode_encode(str, output_type) ⇒ Object

Decodes or encodes a string as needed for either Base64 or QP encoding types in the =?<encoding>??<string>?=“ format.

The output type needs to be :decode to decode the input string or :encode to encode the input string. The character set used for encoding will either be the value of $KCODE for Ruby < 1.9 or the encoding on the string passed in.

On encoding, will only send out Base64 encoded strings.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mail/encodings/encodings.rb', line 79

def Encodings.decode_encode(str, output_type)
  case
  when output_type == :decode
    Encodings.value_decode(str)
  else
    if str.ascii_only?
      str
    else
      Encodings.b_value_encode(str, find_encoding(str))
    end
  end
end

.defined?(str) ⇒ Boolean

Is the encoding we want defined?

Example:

Encodings.defined?(:base64) #=> true

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/mail/encodings/encodings.rb', line 16

def Encodings.defined?( str )
  string = str.to_s.split(/[_-]/).map { |v| v.capitalize }.join('')
  RubyVer.has_constant?(Mail::Encodings, string)
end

.get_encoding(str) ⇒ Object

Gets a defined encoding type, QuotedPrintable or Base64 for now.

Each encoding needs to be defined as a Mail::Encodings::ClassName for this to work, allows us to add other encodings in the future.

Example:

Encodings.get_encoding(:base64) #=> Mail::Encodings::Base64


29
30
31
32
# File 'lib/mail/encodings/encodings.rb', line 29

def Encodings.get_encoding( str )
  string = str.to_s.split(/[_-]/).map { |v| v.capitalize }.join('')
  RubyVer.get_constant(Mail::Encodings, string)
end

.param_decode(str, encoding) ⇒ Object

Decodes a parameter value using URI Escaping.

Example:

Mail::Encodings.param_decode("This%20is%20fun", 'us-ascii') #=> "This is fun"

str = Mail::Encodings.param_decode("This%20is%20fun", 'iso-8559-1')
str.encoding #=> 'ISO-8859-1'      ## Only on Ruby 1.9
str #=> "This is fun"


67
68
69
# File 'lib/mail/encodings/encodings.rb', line 67

def Encodings.param_decode(str, encoding)
  RubyVer.param_decode(str, encoding)
end

.param_encode(str) ⇒ Object

Encodes a parameter value using URI Escaping, note the language field ‘en’ can be set using Mail::Configuration, like so:

Mail.defaults.do
  param_encode_language 'jp'
end

The character set used for encoding will either be the value of $KCODE for Ruby < 1.9 or the encoding on the string passed in.

Example:

Mail::Encodings.param_encode("This is fun") #=> "us-ascii'en'This%20is%20fun"


47
48
49
50
51
52
53
54
55
56
# File 'lib/mail/encodings/encodings.rb', line 47

def Encodings.param_encode(str)
  case
  when str.ascii_only? && str =~ TOKEN_UNSAFE
    %Q{"#{str}"}
  when str.ascii_only?
    str
  else
    RubyVer.param_encode(str)
  end
end

.q_value_encode(str, encoding = nil) ⇒ Object

Encode a string with Quoted-Printable Encoding and returns it ready to be inserted as a value for a field, that is, in the =?<charset>?Q?<string>?= format

Example:

Encodings.q_value_encode('This is あ string', 'UTF-8') 
#=> "=?UTF-8?Q?This_is_=E3=81=82_string?="


160
161
162
163
# File 'lib/mail/encodings/encodings.rb', line 160

def Encodings.q_value_encode(str, encoding = nil)
  string, encoding = RubyVer.q_value_encode(str, encoding)
  "=?#{encoding}?Q?#{string.chomp.gsub(/ /, '_')}?="
end

.unquote_and_convert_to(str, to_encoding) ⇒ Object

Takes an encoded string of the format =?<encoding>??<string>?=



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mail/encodings/encodings.rb', line 114

def Encodings.unquote_and_convert_to(str, to_encoding)
  original_encoding, string = split_encoding_from_string( str )

  output = value_decode( str ).to_s

  if original_encoding.to_s.downcase.gsub("-", "") == to_encoding.to_s.downcase.gsub("-", "")
    output
  elsif original_encoding && to_encoding
    begin
      require 'iconv'
      Iconv.iconv(to_encoding, original_encoding, output).first
    rescue Iconv::IllegalSequence, Iconv::InvalidEncoding, Errno::EINVAL
      # the 'from' parameter specifies a charset other than what the text
      # actually is...not much we can do in this case but just return the
      # unconverted text.
      #
      # Ditto if either parameter represents an unknown charset, like
      # X-UNKNOWN.
      output
    end
  else
    output
  end
end

.value_decode(str) ⇒ Object

Decodes a given string as Base64 or Quoted Printable, depending on what type it is.

String has to be of the format =?<encoding>??<string>?=



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mail/encodings/encodings.rb', line 96

def Encodings.value_decode(str)
  str.gsub!(/\?=(\s*)=\?/, '?==?') # Remove whitespaces between 'encoded-word's
  str.gsub(/(.*?)(=\?.*?\?.\?.*?\?=)|$/m) do
    before = $1.to_s
    text = $2.to_s
    
    case
    when text =~ /=\?.+\?[Bb]\?/m
      before + b_value_decode(text)
    when text =~ /=\?.+\?[Qq]\?/m
      before + q_value_decode(text)
    else
      before + text
    end
  end
end