Class: Mailer2

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
lib/html2mail/mailer2.rb

Instance Method Summary collapse

Instance Method Details

#mail2(html_file, email, subject, from) ⇒ Object



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
# File 'lib/html2mail/mailer2.rb', line 6

def mail2(html_file, email, subject, from)
  html_string = File.read(html_file).gsub('[%%FEmail%%]', email)

  premailer = Premailer.new(StringIO.new(html_string), :warn_level => Premailer::Warnings::SAFE)
  doc = Nokogiri::HTML(premailer.to_inline_css)

  doc.css('img').each do |img|
    path = img['src']
    filesystem_path = File.expand_path(path, File.dirname(html_file))
    if File.readable?(filesystem_path)
      # STDERR.puts "Process img: #{path}"
      attachments.inline[File.basename(path)] = File.read(filesystem_path)
      img['src'] = attachments.inline[File.basename(path)].url
    end
  end

  head_title = doc.css('head title')
  subject ||= head_title.text if head_title.present?
  subject ||= File.basename(html_file, File.extname(html_file))

  html = doc.to_s.html_safe

  # from ||= email

  mail from: from, to: email, subject: subject do |format|
    format.text { render plain: premailer.to_plain_text }
    format.html { render html: html }
  end
end