Class: Mail

Inherits:
Object
  • Object
show all
Defined in:
app/models/mail.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, config, data) ⇒ Mail

Returns a new instance of Mail.



4
5
6
7
8
9
10
# File 'app/models/mail.rb', line 4

def initialize(page, config, data)
  @page, @config, @data = page, config.with_indifferent_access, data
  @required = required_fields
  @leave_blank = leave_blank_field
  @disallow_links = disallow_link_fields
  @errors = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



2
3
4
# File 'app/models/mail.rb', line 2

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



2
3
4
# File 'app/models/mail.rb', line 2

def data
  @data
end

Returns the value of attribute disallow_links.



2
3
4
# File 'app/models/mail.rb', line 2

def disallow_links
  @disallow_links
end

#errorsObject (readonly)

Returns the value of attribute errors.



2
3
4
# File 'app/models/mail.rb', line 2

def errors
  @errors
end

#leave_blankObject (readonly)

Returns the value of attribute leave_blank.



2
3
4
# File 'app/models/mail.rb', line 2

def leave_blank
  @leave_blank
end

#pageObject (readonly)

Returns the value of attribute page.



2
3
4
# File 'app/models/mail.rb', line 2

def page
  @page
end

Class Method Details

.config_error_messages(config) ⇒ Object



26
27
28
29
30
# File 'app/models/mail.rb', line 26

def self.config_error_messages(config)
  config_errors(config).sort.collect do |field, message|
    "'#{field}' #{message}"
  end.to_sentence
end

.config_errors(config) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'app/models/mail.rb', line 16

def self.config_errors(config)
  config_errors = {}
  %w(recipients from).each do |required_field|
    if config[required_field].blank? and config["#{required_field}_field"].blank?
      config_errors[required_field] = "is required"
    end
  end
  config_errors
end

.valid_config?(config) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/models/mail.rb', line 12

def self.valid_config?(config)
  config_errors(config).empty?
end

Instance Method Details

#ccObject



118
119
120
# File 'app/models/mail.rb', line 118

def cc
  data[config[:cc_field]] || config[:cc] || ""
end

#filesObject



122
123
124
125
126
127
128
# File 'app/models/mail.rb', line 122

def files
  res = []
  data.each_value do |d|
    res << d if StringIO === d or Tempfile === d
  end
  res
end

#filesize_limitObject



130
131
132
# File 'app/models/mail.rb', line 130

def filesize_limit
  config[:filesize_limit] || 0
end

#fromObject



98
99
100
# File 'app/models/mail.rb', line 98

def from
  config[:from] || data[config[:from_field]]
end

#html_bodyObject



139
140
141
142
# File 'app/models/mail.rb', line 139

def html_body
  return nil if not valid?
  @html_body = page.render_part( :email_html ) || nil
end

#plain_bodyObject



134
135
136
137
# File 'app/models/mail.rb', line 134

def plain_body
  return nil if not valid?
  @plain_body ||= (page.part( :email ) ? page.render_part( :email ) : page.render_part( :email_plain ))
end

#recipientsObject



102
103
104
# File 'app/models/mail.rb', line 102

def recipients
  config[:recipients] || data[config[:recipients_field]].split(/,/).collect{|e| e.strip}
end

#reply_toObject



106
107
108
# File 'app/models/mail.rb', line 106

def reply_to
  config[:reply_to] || data[config[:reply_to_field]]
end

#sendObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/models/mail.rb', line 144

def send
  return false if not valid?

  if plain_body.blank? and html_body.blank?
    @plain_body = <<-EMAIL
The following information was posted:
#{data.to_hash.to_yaml}
    EMAIL
  end

  headers = { 'Reply-To' => reply_to || from }
  if sender
    headers['Return-Path'] = sender
    headers['Sender'] = sender
  end

  Mailer.deliver_generic_mail(
    :recipients => recipients,
    :from => from,
    :subject => subject,
    :plain_body => @plain_body,
    :html_body => @html_body,
    :cc => cc,
    :headers => headers,
    :files => files,
    :filesize_limit => filesize_limit
  )
  @sent = true
rescue Exception => e
  errors['base'] = e.message
  @sent = false
end

#senderObject



110
111
112
# File 'app/models/mail.rb', line 110

def sender
  config[:sender]
end

#sent?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'app/models/mail.rb', line 177

def sent?
  @sent
end

#subjectObject



114
115
116
# File 'app/models/mail.rb', line 114

def subject
  data[:subject] || config[:subject] || "Form Mail from #{page.request.host}"
end

#valid?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/mail.rb', line 32

def valid?
  unless defined?(@valid)
    @valid = true
    if recipients.blank? and !is_required_field?(config[:recipients_field])
      errors['form'] = 'Recipients are required.'
      @valid = false
    end

    if recipients.any?{|e| !valid_email?(e)}
      errors['form'] = 'Recipients are invalid.'
      @valid = false
    end

    if from.blank? and !is_required_field?(config[:from_field])
      errors['form'] = 'From is required.'
      @valid = false
    end

    if !valid_email?(from)
      errors['form'] = 'From is invalid.'
      @valid = false
    end

    if @required
      @required.each do |name, msg|
        if "as_email" == msg
          unless valid_email?(data[name])
            errors[name] = "invalid email address."
            @valid = false
          end
        elsif m = msg.match(/\/(.*)\//)
          regex = Regexp.new(m[1])
          unless data[name] =~ regex
            errors[name] = "doesn't match regex (#{m[1]})"
            @valid = false
          end
        else
          if data[name].blank?
            errors[name] = ((msg.blank? || %w(1 true required not_blank).include?(msg)) ? "is required." : msg)
            @valid = false
          end
        end
      end
    end
    
    if @disallow_links.present?
      pattern = /www|&amp;|http:|mailto:|bcc:|href|cc:|multipart|\[url|Content-Type:/i
      @disallow_links.each do |field|
        if @data[field] =~ pattern
          errors[field] = %q(must not contain the following text: "www", "&amp;amp;", "http:", "mailto:", "bcc:", "href", "multipart", "[url", or "Content-Type:")
          @valid = false
        end
      end
    end 

    if @leave_blank.present?
      unless @data[@leave_blank] == ''
        errors[@leave_blank] = "must be left blank."
        @valid = false
      end
    end
  end
  
  @valid
end