Class: Mail::Address

### Home Classes Methods

### In Files

- lib/mail/elements/address.rb

### Parent

Object

### Methods

- ::new
- #address
- #address=
- #comments
- #decoded
- #display_name
- #display_name=
- #domain
- #encoded
- #format
- #inspect
- #local
- #name
- #raw
- #to_s

### Included Modules

- Mail::Utilities

### Files

- TODO.rdoc
- VERSION

### Class/Module Index

Quicksearch

- Mail
- Mail::Address
- Mail::AddressContainer
- Mail::AddressLists
- Mail::AddressLists::PrimaryAddress0
- Mail::AddressLists::PrimaryAddress1
- Mail::AddressListsParser
- Mail::AttachmentsList
- Mail::BccField
- Mail::Body
- Mail::CcField
- Mail::CommentsField
- Mail::ContentDescriptionField
- Mail::ContentDisposition
- Mail::ContentDisposition::ContentDisposition0
- Mail::ContentDisposition::ContentDisposition1
- Mail::ContentDisposition::ContentDisposition2
- Mail::ContentDisposition::DispositionType0
- Mail::ContentDisposition::DispositionType1
- Mail::ContentDisposition::Parameter0
- Mail::ContentDisposition::Parameter1
- Mail::ContentDisposition::Value0
- Mail::ContentDispositionField
- Mail::ContentDispositionParser
- Mail::ContentIdField
- Mail::ContentLocation
- Mail::ContentLocation::Location0
- Mail::ContentLocation::Primary0
- Mail::ContentLocationField
- Mail::ContentLocationParser
- Mail::ContentTransferEncoding
- Mail::ContentTransferEncoding::Encoding0
- Mail::ContentTransferEncoding::Encoding1
- Mail::ContentTransferEncoding::Primary0
- Mail::ContentTransferEncodingElement
- Mail::ContentTransferEncodingField
- Mail::ContentTransferEncodingParser
- Mail::ContentType
- Mail::ContentType::CompositeType0
- Mail::ContentType::CompositeType1
- Mail::ContentType::ContentType0
- Mail::ContentType::ContentType1
- Mail::ContentType::ContentType2
- Mail::ContentType::DiscreteType0
- Mail::ContentType::DiscreteType1
- Mail::ContentType::DiscreteType2
- Mail::ContentType::DiscreteType3
- Mail::ContentType::DiscreteType4
- Mail::ContentType::Parameter0
- Mail::ContentType::Parameter1
- Mail::ContentType::Value0
- Mail::ContentTypeField
- Mail::ContentTypeParser
- Mail::DateField
- Mail::DateTime
- Mail::DateTime::Primary0
- Mail::DateTime::Primary1
- Mail::DateTimeParser
- Mail::Encodings
- Mail::Encodings::Base64
- Mail::Encodings::Binary
- Mail::Encodings::EightBit
- Mail::Encodings::QuotedPrintable
- Mail::Encodings::SevenBit
- Mail::Encodings::TransferEncoding
- Mail::Envelope
- Mail::EnvelopeFrom
- Mail::EnvelopeFrom::CtimeDate0
- Mail::EnvelopeFrom::Primary0
- Mail::EnvelopeFromElement
- Mail::EnvelopeFromParser
- Mail::Exim
- Mail::Field
- Mail::Field::FieldError
- Mail::FieldList
- Mail::FileDelivery
- Mail::FromField
- Mail::Header
- Mail::IMAP
- Mail::InReplyToField
- Mail::IndifferentHash
- Mail::KeywordsField
- Mail::Matchers
- Mail::Matchers::HasSentEmailMatcher
- Mail::MessageIdField
- Mail::MessageIds
- Mail::MessageIds::Primary0
- Mail::MessageIdsElement
- Mail::MessageIdsParser
- Mail::MimeVersion
- Mail::MimeVersion::Version0
- Mail::MimeVersion::Version1
- Mail::MimeVersionElement
- Mail::MimeVersionField
- Mail::MimeVersionParser
- Mail::Multibyte
- Mail::Multibyte::EncodingError
- Mail::OptionalField
- Mail::POP3
- Mail::ParameterHash
- Mail::Part
- Mail::PartsList
- Mail::Patterns
- Mail::PhraseList
- Mail::PhraseLists
- Mail::PhraseLists::PrimaryPhrase0
- Mail::PhraseListsParser
- Mail::RFC2045
- Mail::RFC2045::CustomXToken0
- Mail::RFC2045Parser
- Mail::RFC2822
- Mail::RFC2822::AddrSpec0
- Mail::RFC2822::Address0
- Mail::RFC2822::Address1
- Mail::RFC2822::AddressList0
- Mail::RFC2822::AddressList1
- Mail::RFC2822::AngleAddr0
- Mail::RFC2822::Atom0
- Mail::RFC2822::CFWS0
- Mail::RFC2822::CFWS1
- Mail::RFC2822::Comment0
- Mail::RFC2822::Comment1
- Mail::RFC2822::Date0
- Mail::RFC2822::DateTime0
- Mail::RFC2822::DateTime1
- Mail::RFC2822::Day0
- Mail::RFC2822::DayOfWeek0
- Mail::RFC2822::DomainLiteral0
- Mail::RFC2822::DomainLiteral1
- Mail::RFC2822::DomainText0
- Mail::RFC2822::DomainText1
- Mail::RFC2822::DotAtom0
- Mail::RFC2822::DotAtomText0
- Mail::RFC2822::FWS0
- Mail::RFC2822::FWS1
- Mail::RFC2822::Group0
- Mail::RFC2822::Hour0
- Mail::RFC2822::ItemName0
- Mail::RFC2822::ItemName1
- Mail::RFC2822::LocalDotAtom0
- Mail::RFC2822::LocalDotAtomText0
- Mail::RFC2822::MailboxList0
- Mail::RFC2822::MailboxList1
- Mail::RFC2822::MailboxListGroup0
- Mail::RFC2822::MessageIds0
- Mail::RFC2822::MessageIds1
- Mail::RFC2822::Minute0
- Mail::RFC2822::Month0
- Mail::RFC2822::MsgId0
- Mail::RFC2822::MsgIdDomainText0
- Mail::RFC2822::MsgIdDomainText1
- Mail::RFC2822::MsgIdDotAtomText0
- Mail::RFC2822::MsgIdValue0
- Mail::RFC2822::NameAddr0
- Mail::RFC2822::NameValList0
- Mail::RFC2822::NameValList1
- Mail::RFC2822::NameValList2
- Mail::RFC2822::NameValPair0
- Mail::RFC2822::NoFoldLiteral0
- Mail::RFC2822::NoFoldQuote0
- Mail::RFC2822::Path0
- Mail::RFC2822::PhraseList0
- Mail::RFC2822::PhraseList1
- Mail::RFC2822::QuotedDomain0
- Mail::RFC2822::QuotedPair0
- Mail::RFC2822::QuotedString0
- Mail::RFC2822::QuotedString1
- Mail::RFC2822::Received0
- Mail::RFC2822::Return0
- Mail::RFC2822::Second0
- Mail::RFC2822::Time0
- Mail::RFC2822::TimeOfDay0
- Mail::RFC2822::TimeOfDay1
- Mail::RFC2822::Year0
- Mail::RFC2822::Zone0
- Mail::RFC2822Obsolete
- Mail::RFC2822Obsolete::ObsAddrList0
- Mail::RFC2822Obsolete::ObsAddrList1
- Mail::RFC2822Obsolete::ObsAngleAddr0
- Mail::RFC2822Obsolete::ObsBcc0
- Mail::RFC2822Obsolete::ObsCc0
- Mail::RFC2822Obsolete::ObsComments0
- Mail::RFC2822Obsolete::ObsDay0
- Mail::RFC2822Obsolete::ObsDay1
- Mail::RFC2822Obsolete::ObsDayOfWeek0
- Mail::RFC2822Obsolete::ObsDomain0
- Mail::RFC2822Obsolete::ObsDomain1
- Mail::RFC2822Obsolete::ObsDomainList0
- Mail::RFC2822Obsolete::ObsDomainList1
- Mail::RFC2822Obsolete::ObsFWS0
- Mail::RFC2822Obsolete::ObsFWS1
- Mail::RFC2822Obsolete::ObsFrom0
- Mail::RFC2822Obsolete::ObsHour0
- Mail::RFC2822Obsolete::ObsHour1
- Mail::RFC2822Obsolete::ObsInReplyTo0
- Mail::RFC2822Obsolete::ObsKeywords0
- Mail::RFC2822Obsolete::ObsLocalPart0
- Mail::RFC2822Obsolete::ObsLocalPart1
- Mail::RFC2822Obsolete::ObsMboxList0
- Mail::RFC2822Obsolete::ObsMboxList1
- Mail::RFC2822Obsolete::ObsMessageId0
- Mail::RFC2822Obsolete::ObsMinute0
- Mail::RFC2822Obsolete::ObsMinute1
- Mail::RFC2822Obsolete::ObsMonth0
- Mail::RFC2822Obsolete::ObsOptional0
- Mail::RFC2822Obsolete::ObsOrigDate0
- Mail::RFC2822Obsolete::ObsPhraseList0
- Mail::RFC2822Obsolete::ObsPhraseList1
- Mail::RFC2822Obsolete::ObsQp0
- Mail::RFC2822Obsolete::ObsReceived0
- Mail::RFC2822Obsolete::ObsReferences0
- Mail::RFC2822Obsolete::ObsReplyTo0
- Mail::RFC2822Obsolete::ObsResentBcc0
- Mail::RFC2822Obsolete::ObsResentCc0
- Mail::RFC2822Obsolete::ObsResentDate0
- Mail::RFC2822Obsolete::ObsResentFrom0
- Mail::RFC2822Obsolete::ObsResentMid0
- Mail::RFC2822Obsolete::ObsResentRply0
- Mail::RFC2822Obsolete::ObsResentSend0
- Mail::RFC2822Obsolete::ObsResentTo0
- Mail::RFC2822Obsolete::ObsReturn0
- Mail::RFC2822Obsolete::ObsRoute0
- Mail::RFC2822Obsolete::ObsSecond0
- Mail::RFC2822Obsolete::ObsSecond1
- Mail::RFC2822Obsolete::ObsSender0
- Mail::RFC2822Obsolete::ObsSubject0
- Mail::RFC2822Obsolete::ObsText0
- Mail::RFC2822Obsolete::ObsText1
- Mail::RFC2822Obsolete::ObsTo0
- Mail::RFC2822Obsolete::ObsYear0
- Mail::RFC2822Obsolete::ObsYear1
- Mail::RFC2822ObsoleteParser
- Mail::RFC2822Parser
- Mail::Received
- Mail::Received::Primary0
- Mail::ReceivedElement
- Mail::ReceivedField
- Mail::ReceivedParser
- Mail::ReferencesField
- Mail::ReplyToField
- Mail::ResentBccField
- Mail::ResentCcField
- Mail::ResentDateField
- Mail::ResentFromField
- Mail::ResentMessageIdField
- Mail::ResentSenderField
- Mail::ResentToField
- Mail::Retriever
- Mail::ReturnPathField
- Mail::Ruby18
- Mail::Ruby19
- Mail::RubyVer
- Mail::SMTP
- Mail::SMTPConnection
- Mail::SenderField
- Mail::Sendmail
- Mail::ShellEscape
- Mail::StructuredField
- Mail::SubjectField
- Mail::TestMailer
- Mail::TestRetriever
- Mail::ToField
- Mail::UnstructuredField
- Mail::Utilities
- Mail::VERSION
- Net
- Net::SMTP
- Object
- String

No matching classes.

# Mail::Address

### Public Class Methods

new(value = nil) click to toggle source

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 22
    def initialize(value = nil)
      @output_type = nil
      @tree = nil
      @raw_text = value
      case
      when value.nil?
        @parsed = false
        return
      else
        parse(value)
      end
    end

### Public Instance Methods

address() click to toggle source

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 65
    def address
      parse unless @parsed
      domain ? "#{local}@#{domain}" : local
    end

address=(value) click to toggle source

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 75
    def address=(value)
      parse(value)
    end

comments() click to toggle source

Returns an array of comments that are in the email, or an empty array if there are no comments

    a = Address.new('Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>')
    a.comments #=> ['My email address']

    # File lib/mail/elements/address.rb, line 124
    def comments
      parse unless @parsed
      if get_comments.empty?
        nil
      else
        get_comments.map { |c| c.squeeze(" ") }
      end
    end

decoded() click to toggle source

    # File lib/mail/elements/address.rb, line 165
    def decoded
      @output_type = :decode
      format
    end

display_name() click to toggle source

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 83
    def display_name
      parse unless @parsed
      @display_name ||= get_display_name
      Encodings.decode_encode(@display_name.to_s, @output_type) if @display_name
    end

display_name=( str ) click to toggle source

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 95
    def display_name=( str )
      @display_name = str
    end

domain() click to toggle source

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 114
    def domain
      parse unless @parsed
      strip_all_comments(get_domain) if get_domain
    end

encoded() click to toggle source

    # File lib/mail/elements/address.rb, line 160
    def encoded
      @output_type = :encode
      format
    end

format() click to toggle source

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 48
    def format
      parse unless @parsed
      case
      when tree.nil?
        ''
      when display_name
        [quote_phrase(display_name), "<#{address}>", format_comments].compact.join(" ")
      else
        [address, format_comments].compact.join(" ")
      end
    end

inspect() click to toggle source

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 155
    def inspect
      parse unless @parsed
      "#<#{self.class}:#{self.object_id} Address: |#{to_s}| >"
    end

local() click to toggle source

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 104
    def local
      parse unless @parsed
      "#{obs_domain_list}#{get_local.strip}" if get_local
    end

name() click to toggle source

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 138
    def name
      parse unless @parsed
      get_name
    end

raw() click to toggle source

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 37
    def raw
      @raw_text
    end

to_s() click to toggle source

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 147
    def to_s
      parse unless @parsed
      format
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.