public
Description: An extension for Radiant CMS that allows you to create 'contact us' and other mail-bound forms.
Homepage:
Clone URL: git://github.com/radiant/radiant-mailer-extension.git
100644 121 lines (100 sloc) 2.85 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class Mail
  attr_reader :page, :config, :data, :errors
  def initialize(page, config, data)
    @page, @config, @data = page, config.with_indifferent_access, data
    @required = @data.delete(:required)
    @errors = {}
  end
 
  def self.valid_config?(config)
    return false if config['recipients'].blank? and config['recipients_field'].blank?
    return false if config['from'].blank? and config['from_field'].blank?
    true
  end
 
  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 data[name].blank?
            errors[name] = ((msg.blank? || %w(1 true required).include?(msg)) ? "is required." : msg)
            @valid = false
          end
        end
      end
    end
    @valid
  end
 
  def from
    config[:from] || data[config[:from_field]]
  end
 
  def recipients
    config[:recipients] || data[config[:recipients_field]].split(/,/).collect{|e| e.strip}
  end
 
  def reply_to
    config[:reply_to] || data[config[:reply_to_field]]
  end
 
  def sender
    config[:sender]
  end
 
  def subject
    data[:subject] || config[:subject] || "Form Mail from #{page.request.host}"
  end
  
  def cc
    data[config[:cc_field]] || config[:cc] || ""
  end
  
  def send
    return false if not valid?
 
    plain_body = (page.part( :email ) ? page.render_part( :email ) : page.render_part( :email_plain ))
    html_body = page.render_part( :email_html ) || nil
 
    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
    )
    @sent = true
  rescue Exception => e
    errors['base'] = e.message
    @sent = false
  end
 
  def sent?
    @sent
  end
 
  protected
 
  def valid_email?(email)
    (email.blank? ? true : email =~ /.@.+\../)
  end
  
  def is_required_field?(field_name)
    @required && @required.any? {|name,_| name == field_name}
  end
end