Class: HtmlMailer

Inherits:
Object
  • Object
show all
Defined in:
lib/html2email/html_mailer.rb

Constant Summary collapse

MAX_RECIPIENTS =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages, list, options = {}) ⇒ HtmlMailer

Returns a new instance of HtmlMailer.



8
9
10
11
12
# File 'lib/html2email/html_mailer.rb', line 8

def initialize(messages, list, options = {})
  @messages, @list = messages, list
  @from_addr = options[:from_addr] ||
               "do-not-reply@#{ENV['HOSTNAME'] || 'localhost'}"
end

Instance Attribute Details

#from_addrObject (readonly)

Returns the value of attribute from_addr.



6
7
8
# File 'lib/html2email/html_mailer.rb', line 6

def from_addr
  @from_addr
end

Instance Method Details

#check_recipients(list) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/html2email/html_mailer.rb', line 39

def check_recipients(list)
  if list.empty?
    raise '# No recipients defined for email test!'
  elsif list.size > MAX_RECIPIENTS
    raise "# Too many recipients defined! You shouldn't need any more than " +
          MAX_RECIPIENTS.to_s
  else
    warn "# Sending test to #{list.join ', '}"
  end
end

#header(title) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/html2email/html_mailer.rb', line 27

def header(title)
  %{From: Html2Email <#{from_addr}>
    MIME-Version: 1.0
    Content-type: text/html
    Subject: Html2Email test#{": #{title}" if title && !title.empty?}\n
  }.gsub(/^ +/,'')
end

#html_sendObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/html2email/html_mailer.rb', line 14

def html_send
  check_recipients @list

  Net::SMTP.start 'localhost' do |smtp|
    @messages.each do |html|
      smtp.send_message header(page_title(html)) + html, from_addr, @list
    end
  end
# user may not have a local mail server; let them down gentle
rescue Errno::ECONNREFUSED
  warn '# Connection to localhost:25 refused! Is your mailserver running?'
end

#page_title(html) ⇒ Object



35
36
37
# File 'lib/html2email/html_mailer.rb', line 35

def page_title(html)
  html[/<head.*?>.*?<title.*?>(.*?)<\/title>/m, 1]
end