Class: RhizMail::Message

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

Direct Known Subclasses

SimpleTemplateMessage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Message

Pass a hash to Message.new to create it:

Message.new(
  :body => 'body', :subject => "subject here",
  :from_address => '[email protected]', :to_address => '[email protected]'
)

Any of the attributes listed above (under “Attributes”) are valid for this hash.

The older, deprecated style of instantiation is to take specific parameters:

Message.new( subject, to_address, from_address, body )

Unless they are explicitly set, during creation or afterwards using accessors, the following defaults apply:

to_name

nil

from_name

nil

content_type

nil (possible values for content_type are :html and :multipart)

charset

‘iso-8859-1’



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rhizmail.rb', line 167

def initialize( *args )
	@charset = 'iso-8859-1'
	if args.first.is_a? Hash
		args.first.each do |key, val| self.send( key.to_s + '=', val ); end
	else
		subject, to_address, from_address, body = args
		@subject = subject
		@to_address = to_address
		@from_address = from_address
		@body = body
	end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



143
144
145
# File 'lib/rhizmail.rb', line 143

def body
  @body
end

#charsetObject

Returns the value of attribute charset.



143
144
145
# File 'lib/rhizmail.rb', line 143

def charset
  @charset
end

#content_typeObject

Returns the value of attribute content_type.



143
144
145
# File 'lib/rhizmail.rb', line 143

def content_type
  @content_type
end

#from_addressObject

Returns the value of attribute from_address.



143
144
145
# File 'lib/rhizmail.rb', line 143

def from_address
  @from_address
end

#from_nameObject

Returns the value of attribute from_name.



143
144
145
# File 'lib/rhizmail.rb', line 143

def from_name
  @from_name
end

#subjectObject

Returns the value of attribute subject.



143
144
145
# File 'lib/rhizmail.rb', line 143

def subject
  @subject
end

#to_addressObject

Returns the value of attribute to_address.



143
144
145
# File 'lib/rhizmail.rb', line 143

def to_address
  @to_address
end

#to_nameObject

Returns the value of attribute to_name.



143
144
145
# File 'lib/rhizmail.rb', line 143

def to_name
  @to_name
end

Instance Method Details

#deliverObject

Sends the email.



181
# File 'lib/rhizmail.rb', line 181

def deliver; Mailer.get_mailer.send_message( self ); end

#from_headerObject

:nodoc:



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

def from_header # :nodoc:
	fromHeader = "From: "
	if @from_name
		fromHeader += "\"#{@from_name}\" <#{@from_address}>"
	else
		fromHeader += "#{@from_address}"
	end
	fromHeader
end

#headersObject

Returns an array of strings describing the headers for this email message.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rhizmail.rb', line 195

def headers
	headers = []
	headers << "Subject: #{@subject}"
	headers << to_header
	headers << from_header
	if content_type == :html
		headers << "Content-Type: text/html; charset=\"#{@charset}\""
		headers << "MIME-Version: 1.0"
	elsif content_type == :multipart
		headers << "Content-Type: Multipart/Alternative; charset=\"#{@charset}\""
		headers << "MIME-Version: 1.0"
	end
	headers
end

#to_headerObject

:nodoc:



210
211
212
213
214
215
216
217
218
# File 'lib/rhizmail.rb', line 210

def to_header # :nodoc:
	toHeader = "To: "
	if @to_name
		toHeader += "\"#{@to_name}\" <#{@to_address}>"
	else
		toHeader += "#{@to_address}"
	end
	toHeader
end

#verify_sendableObject

Mailer calls this before sending a message; subclasses can override this if they want to ensure that certain parts of the message are valid before sending.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/rhizmail.rb', line 223

def verify_sendable
	[ :subject, :from_address, :to_address, :body ].each { |field|
		if self.send( field ).nil?
			raise( InvalidStateError,
						 "Can't send email with blank #{ field.id2name }" )
		end
	}
	content_types = [ :html, :multipart ]
	unless content_type.nil? or content_types.include?( content_type )
		raise(
			InvalidStateError,
			"I don't recognize the content-type #{ content_type }"
		)
	end
end