Class: BulkMail::Message

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

Overview

The Message class can be used to compose email messages with headers. Usage is pretty straightforward:

m = BulkMail::Message.new({:from => my_email, :subject => 'hello'},
  'Hi there!')

Direct Known Subclasses

MultiPart

Defined Under Namespace

Classes: MultiPart

Constant Summary collapse

DEFAULT_HEADERS =
{
  :mime_version => '1.0'
}
PLAIN =
'text/plain; charset=UTF-8'.freeze
HTML =
'text/html; charset=UTF-8'.freeze
LINE_BREAK =
"\r\n"
HEADER_FMT =
"%s: %s\r\n".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, body) ⇒ Message

Creates a new message, including the default headers in the message headers.



33
34
35
36
# File 'lib/bulkmail/message.rb', line 33

def initialize(headers, body)
  @headers = DEFAULT_HEADERS.merge(headers)
  @body = body
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



23
24
25
# File 'lib/bulkmail/message.rb', line 23

def body
  @body
end

#headersObject

Returns the value of attribute headers.



23
24
25
# File 'lib/bulkmail/message.rb', line 23

def headers
  @headers
end

Class Method Details

.format_headers(headers) ⇒ Object

Formats the message headers into a string.



42
43
44
45
46
47
48
49
50
# File 'lib/bulkmail/message.rb', line 42

def self.format_headers(headers)
  headers.inject('') do |m, kv|
    if kv[1]
      m << (HEADER_FMT % [kv[0].to_header, kv[1]])
    else
      m
    end
  end
end

Instance Method Details

#to_sObject

Composes the header and body into a message string.



53
54
55
56
57
58
# File 'lib/bulkmail/message.rb', line 53

def to_s
  msg = Message.format_headers(@headers)
  msg << LINE_BREAK
  msg << body
  msg
end