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, #headers, #initialize, #inspect, #multipart?, #multipart_boundary, #parsed, #part_filename, #save_to_file, #to_s
Constructor Details
This class inherits a constructor from MIME::Entity
Class Method Details
9
10
11
|
# File 'lib/mime/message.rb', line 9
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
|
# File 'lib/mime/message.rb', line 49
def attach_file(filename)
short_filename = filename.match(/([^\\\/]+)$/)[1]
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
new_content = Entity.new
transfer_to(new_content)
.reject! {|k,v| k =~ /content/}
['content-type'] = 'multipart/mixed'
@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
|
23
24
25
|
# File 'lib/mime/message.rb', line 23
def from
['from'].match(/([A-Z0-9._%+-]+@[A-Z0-9._%+-]+\.[A-Z]+)/i)[1]
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
18
19
20
21
|
# File 'lib/mime/message.rb', line 18
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
13
14
15
16
|
# File 'lib/mime/message.rb', line 13
def to(addressee=nil)
['to'] = addressee if addressee
['to'].match(/([A-Z0-9._%+-]+@[A-Z0-9._%+-]+\.[A-Z]+)/i)[1]
end
|