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 = nil) ⇒ Object



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

def mail2(html_file, email, subject, from=nil)
  premailer = Premailer.new(html_file, :warn_level => Premailer::Warnings::SAFE)
  # STDOUT.puts premailer.to_inline_css
  doc = Nokogiri::HTML(premailer.to_inline_css)
  # doc = File.open(html_file) { |f| Nokogiri::XML(f) }

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

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

  html = doc.to_s.html_safe

  from ||= email

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

  return m
end

#prepare_html(html_file) ⇒ Object



7
8
9
# File 'lib/html2mail/mailer2.rb', line 7

def prepare_html(html_file)
  Premailer.new(html_file, :warn_level => Premailer::Warnings::SAFE).to_inline_css
end