Class: MIME::Message

Inherits:
Entity show all
Defined in:
lib/mime/message.rb

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

.generateObject



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

Raises:

  • (ArgumentError)


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]

  # Generate the attachment piece
  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)
  
  # Enclose in a top-level multipart/mixed
  if multipart? && multipart_type == 'mixed'
    # If we're already dealing with a mixed multipart, all we have to do is add the attachment part
    (@content ||= []) << attachment
  else
    # If there is no content yet, add a little message about the attachment(s).
    set_content 'See attachment(s)' if @content.nil?
    # Generate the new top-level multipart, transferring what is here already into a child object
    new_content = Entity.new
    # Whatever it is, since it's not multipart/mixed, transfer it into a child object and add the attachment.
    transfer_to(new_content)
    headers.reject! {|k,v| k =~ /content/}
    headers['content-type'] = 'multipart/mixed'
    headers.delete 'content-transfer-encoding' # because we don't need a transfer-encoding on a multipart package.
    @content = [new_content, attachment]
  end

  attachment
end

#attachmentsObject



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
  headers['from'].match(/([A-Z0-9._%+-]+@[A-Z0-9._%+-]+\.[A-Z]+)/i)[1] if headers['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)
  headers['content-type'] = 'multipart/alternative'
  @content = content_types.collect { |content_type| Entity.new('content-type' => content_type) }
end

#htmlObject



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)
  headers['subject'] = subj if subj
  headers['subject']
end

#textObject



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)
  headers['to'] = addressee if addressee
  headers['to'].match(/([A-Z0-9._%+-]+@[A-Z0-9._%+-]+\.[A-Z]+)/i)[1] if headers['to'].respond_to?(:match)
end