diff --git a/app/mailers/mailing.rb b/app/mailers/mailing.rb new file mode 100644 index 0000000..0442dbd --- /dev/null +++ b/app/mailers/mailing.rb @@ -0,0 +1,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} <#{source.contact_email}>" + 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', + :recipients => recipient, + :from => mailing.generate_from, + :reply_to => mailing.person.email, + :subject => mailing.generate_subject + ) + end + end +end diff --git a/app/models/mailing.rb b/app/models/mailing.rb deleted file mode 100644 index 9d06851..0000000 --- a/app/models/mailing.rb +++ /dev/null @@ -1,63 +0,0 @@ -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} <#{source.contact_email}>" - 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.deliver_mail(self, recipient.email) - 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 mail(mailing, recipient) - content_type 'text/html' - recipients recipient - from mailing.generate_from - reply_to mailing.person.email - subject mailing.generate_subject - body :message => mailing.body, - :signature_message => mailing.signature_message, - :url => mailing.url - end - end -end diff --git a/app/views/mailing/sender/mail.html.erb b/app/views/mailing/sender/mail.html.erb deleted file mode 100644 index dc92944..0000000 --- a/app/views/mailing/sender/mail.html.erb +++ /dev/null @@ -1,17 +0,0 @@ - - -
- - - - <%= word_wrap(@message) %> -
- --
- <%= @signature_message %>
- <%= @url %>
-
+ --
+ <%= @signature_message %>
+ <%= @url %>
+