Class: MmMail::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/mmmail.rb

Overview

A Message object representing an Email to be passed to a Transport.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Message

Creates a new message with associated fields.

Examples:

MmMail::Message.new(:to => '[email protected]', :body => 'hi')

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a message with.

Options Hash (opts):

  • :from (String) — default: 'nobody@localhost'

    The email’s From field

  • :subject (String) — default: '') The email's Subject field

    ”) The email’s Subject field

  • :body (String) — default: '') The email's body (not a header)

    ”) The email’s body (not a header)

  • :to (String) — default: nil

    The email’s To field. List multiple recipients as ‘[email protected], [email protected]’, not an array.



149
150
151
152
153
154
155
156
# File 'lib/mmmail.rb', line 149

def initialize(opts = {})
  defaults = {
    :from => 'nobody@localhost',
    :subject => '',
    :body => ''
  }
  @headers = defaults.merge(opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Override this method to allow any call to obj.meth or obj.meth= to set a header field on this object.

Examples:

To set the field ‘X-Message-Id’

m.x_message_id = '1234'
m.x_message_id == '1234' # => true


183
184
185
186
187
188
189
190
191
# File 'lib/mmmail.rb', line 183

def method_missing(sym, *args)
  if sym.to_s =~ /=$/
    self[sym.to_s[0..-2].to_sym] = args.first
  elsif @headers.has_key?(sym)
    self[sym]
  else
    super
  end
end

Instance Method Details

#[](k) ⇒ String

Allow access of fields by header name or symbolic representation

Examples:

m[:x_message_id] = '1234'
m['X-Message-Id'] == '1234' # => true

Parameters:

  • k (String, Symbol)

    the header or symbolic header value to lookup.

Returns:

  • (String)

    the value associated with the field



166
# File 'lib/mmmail.rb', line 166

def [](k)     @headers[translate_header_to_sym(k)]     end

#[]=(k, v) ⇒ Object

Allow access of fields by header name or symbolic representation

Examples:

m[:x_message_id] = '1234'
m['X-Message-Id'] == '1234' # => true


174
# File 'lib/mmmail.rb', line 174

def []=(k, v) @headers[translate_header_to_sym(k)] = v end

#recipients_listArray<String>

Returns all the recipients in the To field.

Examples:

m.to = '[email protected], [email protected]'
m.recipients_list # => ['[email protected]', '[email protected]']

Returns:

  • (Array<String>)

    the emails in the To field of the message.



216
217
218
# File 'lib/mmmail.rb', line 216

def recipients_list
  to.split(/\s*,\s*/)
end

#respond_to?(sym) ⇒ Boolean

Override this method to verify if a field has been set.

Returns:

  • (Boolean)

    whether the field was set (or if a regular method is callable.)



197
198
199
200
# File 'lib/mmmail.rb', line 197

def respond_to?(sym)
  return true if super
  @headers.has_key?(sym)
end

#to_sString

Returns the message in its full form as expected by an SMTP server.

Returns:

  • (String)

    the email with headers followed by a body



205
206
207
# File 'lib/mmmail.rb', line 205

def to_s
  [headers, body].join("\n")
end

#valid?Boolean

Checks if the message is valid. Validity is based on having the From, To and Subject fields set. From and To must not be empty.

Returns:

  • (Boolean)

    whether or not the message is a valid e-mail



225
226
227
228
229
230
231
# File 'lib/mmmail.rb', line 225

def valid?
  [:from, :to].each do |field|
    return false if !self[field] || self[field].empty?
  end
  
  self[:subject] ? true : false
end