Class: Mail
- Inherits:
-
Object
- Object
- Defined in:
- app/models/mail.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#disallow_links ⇒ Object
readonly
Returns the value of attribute disallow_links.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#leave_blank ⇒ Object
readonly
Returns the value of attribute leave_blank.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Class Method Summary collapse
- .config_error_messages(config) ⇒ Object
- .config_errors(config) ⇒ Object
- .valid_config?(config) ⇒ Boolean
Instance Method Summary collapse
- #cc ⇒ Object
- #files ⇒ Object
- #filesize_limit ⇒ Object
- #from ⇒ Object
- #html_body ⇒ Object
-
#initialize(page, config, data) ⇒ Mail
constructor
A new instance of Mail.
- #plain_body ⇒ Object
- #recipients ⇒ Object
- #reply_to ⇒ Object
- #send ⇒ Object
- #sender ⇒ Object
- #sent? ⇒ Boolean
- #subject ⇒ Object
- #valid? ⇒ Boolean
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
2 3 4 |
# File 'app/models/mail.rb', line 2 def config @config end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
2 3 4 |
# File 'app/models/mail.rb', line 2 def data @data end |
#disallow_links ⇒ Object (readonly)
Returns the value of attribute disallow_links.
2 3 4 |
# File 'app/models/mail.rb', line 2 def disallow_links @disallow_links end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
2 3 4 |
# File 'app/models/mail.rb', line 2 def errors @errors end |
#leave_blank ⇒ Object (readonly)
Returns the value of attribute leave_blank.
2 3 4 |
# File 'app/models/mail.rb', line 2 def leave_blank @leave_blank end |
#page ⇒ Object (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) config_errors(config).sort.collect do |field, | "'#{field}' #{}" 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
12 13 14 |
# File 'app/models/mail.rb', line 12 def self.valid_config?(config) config_errors(config).empty? end |
Instance Method Details
#cc ⇒ Object
118 119 120 |
# File 'app/models/mail.rb', line 118 def cc data[config[:cc_field]] || config[:cc] || "" end |
#files ⇒ Object
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_limit ⇒ Object
130 131 132 |
# File 'app/models/mail.rb', line 130 def filesize_limit config[:filesize_limit] || 0 end |
#from ⇒ Object
98 99 100 |
# File 'app/models/mail.rb', line 98 def from config[:from] || data[config[:from_field]] end |
#html_body ⇒ Object
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_body ⇒ Object
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 |
#recipients ⇒ Object
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_to ⇒ Object
106 107 108 |
# File 'app/models/mail.rb', line 106 def reply_to config[:reply_to] || data[config[:reply_to_field]] end |
#send ⇒ Object
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. @sent = false end |
#sender ⇒ Object
110 111 112 |
# File 'app/models/mail.rb', line 110 def sender config[:sender] end |
#sent? ⇒ Boolean
177 178 179 |
# File 'app/models/mail.rb', line 177 def sent? @sent end |
#subject ⇒ Object
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
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|&|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;", "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 |