cirandas.net

ref: master

plugins/send_email/lib/send_email_plugin/mail.rb


 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
class SendEmailPlugin::Mail
  include ActiveModel::Validations

  cN_('Subject'); cN_('Message'); N_('To'); cN_('From')

  attr_accessor :environment, :from, :to, :subject, :message, :params

  validates_presence_of :environment
  validates_presence_of :to, :message
  validate :recipients_format

  def initialize(attributes = {:subject => 'New mail'})
    if attributes
      attributes.each do |attr,value|
        self.send("#{attr}=", value)
      end
    end
  end

  def recipients_format
    if to_as_list.any? do |value|
        if value !~ Noosfero::Constants::EMAIL_FORMAT
          self.errors.add(:to, _("'%s' isn't a valid e-mail address") % value)
        end
      end
    else
      allowed_emails = environment ? environment.send_email_plugin_allow_to.to_s.gsub(/\s+/, '').split(/,/) : []
      if to_as_list.any? do |value|
          if !allowed_emails.include?(value)
            self.errors.add(:to, _("'%s' address is not allowed (see SendEmailPlugin config)") % value)
          end
        end
      end
    end
  end

  def params=(value = {})
    [:profile, :action, :controller, :to, :message, :subject, :from, :commit].each{|k| value.delete(k)}
    @params = value
  end

  def to_as_list
    to && to.split(/,/) || []
  end

end