Class: MIME::Message
Overview
A Message is really a MIME::Entity, but should be used for the outermost Entity, because it includes helper methods to access common data from an email message.
Instance Attribute Summary
Attributes inherited from Entity
#content, #encoding, #multipart_type
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Entity
#attachment?, #decoded_content, #find_part, #find_parts, #from_parsed, #from_tmail, #headers, #initialize, #inspect, #multipart?, #multipart_boundary, #parsed, #part_filename, #save_to_file, #set_content, #to_s
Constructor Details
This class inherits a constructor from MIME::Entity
Class Method Details
8
9
10
|
# File 'lib/mime/message.rb', line 8
def self.generate
Message.new('content-type' => 'text/plain', 'mime-version' => '1.0')
end
|
Instance Method Details
#attach_file(filename) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/mime/message.rb', line 49
def attach_file(filename)
raise ArgumentError, "Currently the <filename> given must be a String (path to a file)." unless filename.is_a?(String)
short_filename = filename.match(/([^\\\/]+)$/)[1]
require 'shared-mime-info'
attachment = Entity.new(
'content-type' => MIME.check(filename).type + "; \r\n name=\"#{short_filename}\"",
'content-disposition' => "attachment; \r\n filename=\"#{short_filename}\"",
'content-transfer-encoding' => 'base64'
)
attachment.content = File.read(filename)
if multipart? && multipart_type == 'mixed'
(@content ||= []) << attachment
else
set_content 'See attachment(s)' if @content.nil?
new_content = Entity.new
transfer_to(new_content)
.reject! {|k,v| k =~ /content/}
['content-type'] = 'multipart/mixed'
.delete 'content-transfer-encoding' @content = [new_content, attachment]
end
attachment
end
|
#attachments ⇒ Object
27
28
29
|
# File 'lib/mime/message.rb', line 27
def attachments
find_parts(:content_disposition => 'attachment')
end
|
#from(dummy = nil) ⇒ Object
22
23
24
25
|
# File 'lib/mime/message.rb', line 22
def from(dummy=nil)
raise "Can't set FROM address in the message - will be set automatically when the message is sent." if dummy
['from'].match(/([A-Z0-9._%+-]+@[A-Z0-9._%+-]+\.[A-Z]+)/i)[1] if ['from'].respond_to?(:match)
end
|
#generate_multipart(*content_types) ⇒ Object
44
45
46
47
|
# File 'lib/mime/message.rb', line 44
def generate_multipart(*content_types)
['content-type'] = 'multipart/alternative'
@content = content_types.collect { |content_type| Entity.new('content-type' => content_type) }
end
|
35
36
37
38
|
# File 'lib/mime/message.rb', line 35
def html
part = find_part(:content_type => 'text/html')
part.content if part
end
|
#save_attachments_to(path = nil) ⇒ Object
40
41
42
|
# File 'lib/mime/message.rb', line 40
def save_attachments_to(path=nil)
attachments.each {|a| a.save_to_file(path) }
end
|
#subject(subj = nil) ⇒ Object
17
18
19
20
|
# File 'lib/mime/message.rb', line 17
def subject(subj=nil)
['subject'] = subj if subj
['subject']
end
|
31
32
33
34
|
# File 'lib/mime/message.rb', line 31
def text
part = find_part(:content_type => 'text/plain')
part.content if part
end
|
#to(addressee = nil) ⇒ Object
12
13
14
15
|
# File 'lib/mime/message.rb', line 12
def to(addressee=nil)
['to'] = addressee if addressee
['to'].match(/([A-Z0-9._%+-]+@[A-Z0-9._%+-]+\.[A-Z]+)/i)[1] if ['to'].respond_to?(:match)
end
|