Class: BulkMail::Message::MultiPart

Inherits:
BulkMail::Message show all
Defined in:
lib/bulkmail/message.rb

Overview

The MultiPart class can be used for composing multi-part messages. For example:

m = BulkMail::Message::MultiPart.new(
  {:from => 'my_email', :subject => 'Hello'}, :alternate)
m.add_part({:content_type => BulkMail::Message::PLAIN},
  'You are now seeing the plain text version')
m.add_part({:content_type => BulkMail::Message::HTML},
  '<p>You are now seeing the HTML version</p>')
puts m.to_s

Constant Summary collapse

BOUNDARY =
'!!@@##$$'.freeze
MULTI_PART_CONTENT_TYPE_FMT =
"multipart/%s; charset=UTF-8; boundary=\"#{BOUNDARY}\"".freeze
BOUNDARY_LINE =
"\r\n--#{BOUNDARY}\r\n".freeze

Constants inherited from BulkMail::Message

DEFAULT_HEADERS, HEADER_FMT, HTML, LINE_BREAK, PLAIN

Instance Attribute Summary collapse

Attributes inherited from BulkMail::Message

#body, #headers

Instance Method Summary collapse

Methods inherited from BulkMail::Message

format_headers

Constructor Details

#initialize(headers, multi_type) ⇒ MultiPart

Initializes a new instance.



75
76
77
78
79
# File 'lib/bulkmail/message.rb', line 75

def initialize(headers, multi_type)
  super(headers, nil)
  @multi_type = multi_type
  @parts = []
end

Instance Attribute Details

#multi_typeObject

Returns the value of attribute multi_type.



72
73
74
# File 'lib/bulkmail/message.rb', line 72

def multi_type
  @multi_type
end

#partsObject

Returns the value of attribute parts.



72
73
74
# File 'lib/bulkmail/message.rb', line 72

def parts
  @parts
end

Instance Method Details

#add_part(headers, body) ⇒ Object

Adds a part to the message.



82
83
84
# File 'lib/bulkmail/message.rb', line 82

def add_part(headers, body)
  @parts << Message.new(headers, body)
end

#to_sObject

Composes the parts into an email message.



91
92
93
94
95
96
97
98
99
100
# File 'lib/bulkmail/message.rb', line 91

def to_s
  @headers[:content_type] = MULTI_PART_CONTENT_TYPE_FMT % @multi_type
  msg = Message.format_headers(@headers)
  @parts.each do |part|
    msg << BOUNDARY_LINE
    msg << part.to_s
  end
  msg << BOUNDARY_LINE
  msg
end