Class: ActionMailer::Part

Inherits:
Object show all
Includes:
AdvAttrAccessor, PartContainer
Defined in:
lib/action_mailer/part.rb

Overview

Represents a subpart of an email message. It shares many similar attributes of ActionMailer::Base. Although you can create parts manually and add them to the parts list of the mailer, it is easier to use the helper methods in ActionMailer::PartContainer.

Instance Attribute Summary

Attributes included from PartContainer

#parts

Instance Method Summary collapse

Methods included from PartContainer

#attachment, #part

Methods included from AdvAttrAccessor

included

Constructor Details

#initialize(params) ⇒ Part

Create a new part from the given params hash. The valid params keys correspond to the accessors.



43
44
45
46
47
48
49
50
51
52
# File 'lib/action_mailer/part.rb', line 43

def initialize(params)
  @content_type = params[:content_type]
  @content_disposition = params[:disposition] || "inline"
  @charset = params[:charset]
  @body = params[:body]
  @filename = params[:filename]
  @transfer_encoding = params[:transfer_encoding] || "quoted-printable"
  @headers = params[:headers] || {}
  @parts = []
end

Instance Method Details

#to_mail(defaults) ⇒ Object

Convert the part to a mail object which can be included in the parts list of another mail object.



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/action_mailer/part.rb', line 56

def to_mail(defaults)
  part = TMail::Mail.new

  real_content_type, ctype_attrs = parse_content_type(defaults)

  if @parts.empty?
    part.content_transfer_encoding = transfer_encoding || "quoted-printable"
    case (transfer_encoding || "").downcase
      when "base64" then
        part.body = TMail::Base64.folding_encode(body)
      when "quoted-printable"
        part.body = [Utils.normalize_new_lines(body)].pack("M*")
      else
        part.body = body
    end

    # Always set the content_type after setting the body and or parts!
    # Also don't set filename and name when there is none (like in
    # non-attachment parts)
    if content_disposition == "attachment"
      ctype_attrs.delete "charset"
      part.set_content_type(real_content_type, nil,
        squish("name" => filename).merge(ctype_attrs))
      part.set_content_disposition(content_disposition,
        squish("filename" => filename).merge(ctype_attrs))
    else
      part.set_content_type(real_content_type, nil, ctype_attrs)
      part.set_content_disposition(content_disposition) 
    end        
  else
    if String === body
      @parts.unshift Part.new(:charset => charset, :body => @body, :content_type => 'text/plain')
      @body = nil
    end
      
    @parts.each do |p|
      prt = (TMail::Mail === p ? p : p.to_mail(defaults))
      part.parts << prt
    end
    
    part.set_content_type(real_content_type, nil, ctype_attrs) if real_content_type =~ /multipart/
  end

  headers.each { |k,v| part[k] = v }

  part
end