mailing.rb
1.58 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
require 'mailing_job'
class Mailing < ActiveRecord::Base
attr_accessible :subject, :body
validates_presence_of :source_id, :subject, :body
belongs_to :source, :foreign_key => :source_id, :polymorphic => true
belongs_to :person
has_many :mailing_sents
xss_terminate :only => [ :subject, :body ], :with => 'white_list', :on => 'validation'
after_create do |mailing|
mailing.schedule
end
def schedule
Delayed::Job.enqueue MailingJob.new(self.id)
end
def generate_from
"#{source.name} <#{if source.is_a? Environment then source.noreply_email else source.contact_email end}>"
end
def generate_subject
'[%s] %s' % [source.name, subject]
end
def signature_message
_('Sent by Noosfero.')
end
def url
''
end
def deliver
each_recipient do |recipient|
begin
Mailing::Sender.notification(self, recipient.email).deliver
self.mailing_sents.create(:person => recipient)
rescue Exception
# FIXME should not discard errors silently. An idea is to collect all
# errors and generate a task (notification) for the +source+
# (environment/organization) listing these errors.
end
end
end
class Sender < ActionMailer::Base
def notification(mailing, recipient)
@message = mailing.body
@signature_message = mailing.signature_message
@url = mailing.url
mail(
:content_type => 'text/html',
:to => recipient,
:from => mailing.generate_from,
:reply_to => mailing.person.email,
:subject => mailing.generate_subject
)
end
end
end