Class: MailNotify

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
app/models/mail_notify.rb

Overview

Send a publication email whenever an elt is saved

Important: the bcc, which is actually used for sending, is setup in the Mail class

Instance Method Summary collapse

Instance Method Details

#publish(elt) ⇒ Object



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
# File 'app/models/mail_notify.rb', line 8

def publish(elt)
  logger.info "Create a mail for publication"
  subject elt.subject

  ml = mailing_list(elt.parent)

  recipients ml.subject.to_s \
    + " <#{ml.id}@#{ActionMailer::Base.smtp_settings[:domain]}>"

  from ((elt.person and elt.person.name) ? elt.person.name : ANONYMOUS_POSTER) \
    + " <" \
    + ((elt.person and elt.person.email) \
      ? elt.person.email : "#{ANONYMOUS_POSTER}@#{ActionMailer::Base.smtp_settings[:domain]}") \
    + ">"

  # Try to render the element as html
  body :elt => elt

  templates = Dir.glob("#{template_path}/#{@template}.*")
  templates.each do |path|
    # TODO: don't hardcode rhtml|rxml
    basename = File.basename(path)
    next unless md = /^([^\.]+)\.([^\.]+\.[^\+]+)\.(rhtml|rxml)$/.match(basename)
    template_name = basename
    content_type = md.captures[1].gsub('.', '/')
    @parts << ActionMailer::Part.new(:content_type => content_type,
                                     :disposition => "inline", :charset => charset,
                                     :body => render_message(template_name, @body))
  end
  unless @parts.empty?
    @content_type = "multipart/alternative"
    @parts = sort_parts(@parts, @implicit_parts_order)
  end

  elt.attachments.each { |att|
    attachment(att.content_type ? att.content_type : "") { |a|
      a.filename = att.file.gsub /^.*public.*\//, ''
      a.body = File.read(att.file)
    }
  }

  # This is the essential of a mailing list, you reply to the mailing list,
  # where every body sends their mail.
  # This very mail can be a mailing list all by itself...
  @headers['Reply-to'] = "#{ml.id}@#{ActionMailer::Base.smtp_settings[:domain]}"
  #puts "#{ml.id}@#{ActionMailer::Base.smtp_settings[:domain]}"

  @headers['In-Reply-To'] = elt.parent.mail.message \
    if elt.parent and elt.parent.mail and elt.parent.mail.message

  @sent_on = elt.created_on

  parentMsg = elt.parent.mail
  if parentMsg
    parentMsg.reload # Mostly necessary for tests
    @headers['references'] = ''
    @headers['references'] << parentMsg.mail_parents if parentMsg.mail_parents
    @headers['references'] << parentMsg.message if parentMsg.message
  end

  @headers['X-Mailer'] = "#{ActionMailer::Base.smtp_settings[:domain]} v#{PARLEMENT_VERSION}"

  logger.info "Mail created"
end