Object
Mail::Address handles all email addresses in Mail. It takes an email address string and parses it, breaking it down into it’s component parts and allowing you to get the address, comments, display name, name, local part, domain part and fully formatted address.
Mail::Address requires a correctly formatted email address per RFC2822 or RFC822. It handles all obsolete versions including obsolete domain routing on the local part.
a = Address.new('Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>') a.format #=> 'Mikel Lindsaar <mikel@test.lindsaar.net> (My email address)' a.address #=> 'mikel@test.lindsaar.net' a.display_name #=> 'Mikel Lindsaar' a.local #=> 'mikel' a.domain #=> 'test.lindsaar.net' a.comments #=> ['My email address'] a.to_s #=> 'Mikel Lindsaar <mikel@test.lindsaar.net> (My email address)'
# File lib/mail/elements/address.rb, line 23 23: def initialize(value = nil) 24: @output_type = nil 25: @tree = nil 26: @raw_text = value 27: case 28: when value.nil? 29: @parsed = false 30: return 31: else 32: parse(value) 33: end 34: end
Returns the address that is in the address itself. That is, the local@domain string, without any angle brackets or the like.
a = Address.new('Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>') a.address #=> 'mikel@test.lindsaar.net'
# File lib/mail/elements/address.rb, line 66 66: def address 67: parse unless @parsed 68: domain ? "#{local}@#{domain}" : local 69: end
Provides a way to assign an address to an already made Mail::Address object.
a = Address.new a.address = 'Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>' a.address #=> 'mikel@test.lindsaar.net'
# File lib/mail/elements/address.rb, line 76 76: def address=(value) 77: parse(value) 78: end
# File lib/mail/elements/address.rb, line 166 166: def decoded 167: @output_type = :decode 168: format 169: end
Returns the display name of the email address passed in.
a = Address.new('Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>') a.display_name #=> 'Mikel Lindsaar'
# File lib/mail/elements/address.rb, line 84 84: def display_name 85: parse unless @parsed 86: @display_name ||= get_display_name 87: Encodings.decode_encode(@display_name.to_s, @output_type) if @display_name 88: end
Provides a way to assign a display name to an already made Mail::Address object.
a = Address.new a.address = 'mikel@test.lindsaar.net' a.display_name = 'Mikel Lindsaar' a.format #=> 'Mikel Lindsaar <mikel@test.lindsaar.net>'
# File lib/mail/elements/address.rb, line 96 96: def display_name=( str ) 97: @display_name = str 98: end
Returns the domain part (the right hand side of the @ sign in the email address) of the address
a = Address.new('Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>') a.domain #=> 'test.lindsaar.net'
# File lib/mail/elements/address.rb, line 115 115: def domain 116: parse unless @parsed 117: strip_all_comments(get_domain) if get_domain 118: end
# File lib/mail/elements/address.rb, line 161 161: def encoded 162: @output_type = :encode 163: format 164: end
Returns a correctly formatted address for the email going out. If given an incorrectly formatted address as input, Mail::Address will do it’s best to format it correctly. This includes quoting display names as needed and putting the address in angle brackets etc.
a = Address.new('Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>') a.format #=> 'Mikel Lindsaar <mikel@test.lindsaar.net> (My email address)'
# File lib/mail/elements/address.rb, line 49 49: def format 50: parse unless @parsed 51: case 52: when tree.nil? 53: '' 54: when display_name 55: [quote_phrase(display_name), "<#{address}>", format_comments].compact.join(" ") 56: else 57: [address, format_comments].compact.join(" ") 58: end 59: end
Shows the Address object basic details, including the Address
a = Address.new('Mikel (My email) <mikel@test.lindsaar.net>') a.inspect #=> "#<Mail::Address:14184910 Address: |Mikel <mikel@test.lindsaar.net> (My email)| >"
# File lib/mail/elements/address.rb, line 156 156: def inspect 157: parse unless @parsed 158: "#<#{self.class}:#{self.object_id} Address: |#{to_s}| >" 159: end
Returns the local part (the left hand side of the @ sign in the email address) of the address
a = Address.new('Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>') a.local #=> 'mikel'
# File lib/mail/elements/address.rb, line 105 105: def local 106: parse unless @parsed 107: "#{obs_domain_list}#{get_local.strip}" 108: end
Sometimes an address will not have a display name, but might have the name as a comment field after the address. This returns that name if it exists.
a = Address.new('mikel@test.lindsaar.net (Mikel Lindsaar)') a.name #=> 'Mikel Lindsaar'
# File lib/mail/elements/address.rb, line 139 139: def name 140: parse unless @parsed 141: get_name 142: end
Returns the raw imput of the passed in string, this is before it is passed by the parser.
# File lib/mail/elements/address.rb, line 38 38: def raw 39: @raw_text 40: end
Returns the format of the address, or returns nothing
a = Address.new('Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>') a.format #=> 'Mikel Lindsaar <mikel@test.lindsaar.net> (My email address)'
# File lib/mail/elements/address.rb, line 148 148: def to_s 149: parse unless @parsed 150: format 151: end
# File lib/mail/elements/address.rb, line 273 273: def format_comments 274: if comments 275: comment_text = comments.map {|c| escape_paren(c) }.join(' ').squeeze(" ") 276: @format_comments ||= "(#{comment_text})" 277: else 278: nil 279: end 280: end
# File lib/mail/elements/address.rb, line 218 218: def get_comments 219: if tree.respond_to?(:comments) 220: @comments = tree.comments.map { |c| unparen(c.text_value.to_str) } 221: else 222: @comments = [] 223: end 224: end
# File lib/mail/elements/address.rb, line 226 226: def get_display_name 227: if tree.respond_to?(:display_name) 228: name = unquote(tree.display_name.text_value.strip) 229: str = strip_all_comments(name.to_s) 230: elsif comments 231: if domain 232: str = strip_domain_comments(format_comments) 233: else 234: str = nil 235: end 236: else 237: nil 238: end 239: 240: if str.blank? 241: nil 242: else 243: str 244: end 245: end
# File lib/mail/elements/address.rb, line 186 186: def get_domain 187: if tree.respond_to?(:angle_addr) 188: @domain_text ||= tree.angle_addr.addr_spec.domain.text_value.strip 189: elsif tree.respond_to?(:domain) 190: @domain_text ||= tree.domain.text_value.strip 191: elsif tree.respond_to?(:addr_spec) 192: tree.addr_spec.domain.text_value.strip 193: else 194: nil 195: end 196: end
# File lib/mail/elements/address.rb, line 291 291: def get_local 292: case 293: when tree.respond_to?(:local_dot_atom_text) 294: tree.local_dot_atom_text.text_value 295: when tree.respond_to?(:angle_addr) 296: tree.angle_addr.addr_spec.local_part.text_value 297: when tree.respond_to?(:addr_spec) 298: tree.addr_spec.local_part.text_value 299: else 300: tree.local_part.text_value 301: end 302: end
# File lib/mail/elements/address.rb, line 247 247: def get_name 248: if display_name 249: str = display_name 250: else 251: if comments 252: comment_text = comments.join(' ').squeeze(" ") 253: str = "(#{comment_text})" 254: end 255: end 256: 257: if str.blank? 258: nil 259: else 260: unparen(str) 261: end 262: end
# File lib/mail/elements/address.rb, line 282 282: def obs_domain_list 283: if tree.respond_to?(:angle_addr) 284: obs = tree.angle_addr.elements.select { |e| e.respond_to?(:obs_domain_list) } 285: !obs.empty? ? obs.first.text_value : nil 286: else 287: nil 288: end 289: end
# File lib/mail/elements/address.rb, line 173 173: def parse(value = nil) 174: @parsed = true 175: case 176: when value.nil? 177: nil 178: when value.class == String 179: self.tree = Mail::AddressList.new(value).address_nodes.first 180: else 181: self.tree = value 182: end 183: end
# File lib/mail/elements/address.rb, line 198 198: def strip_all_comments(string) 199: unless comments.blank? 200: comments.each do |comment| 201: string = string.gsub("(#{comment})", '') 202: end 203: end 204: string.strip 205: end
# File lib/mail/elements/address.rb, line 207 207: def strip_domain_comments(value) 208: unless comments.blank? 209: comments.each do |comment| 210: if get_domain && get_domain.include?("(#{comment})") 211: value = value.gsub("(#{comment})", '') 212: end 213: end 214: end 215: value.to_s.strip 216: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.
Returns an array of comments that are in the email, or an empty array if there are no comments