Class: Harbor::Mailer

Inherits:
MailBuilder
  • Object
show all
Defined in:
lib/harbor/mailer.rb

Direct Known Subclasses

Test::Mailer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Mailer

Returns a new instance of Mailer.



32
33
34
35
36
37
# File 'lib/harbor/mailer.rb', line 32

def initialize(*args)
  super

  @layout = self.class.layout
  @host = self.class.host
end

Instance Attribute Details

#mail_serverObject

Returns the value of attribute mail_server.



30
31
32
# File 'lib/harbor/mailer.rb', line 30

def mail_server
  @mail_server
end

Class Method Details

.hostObject



26
27
28
# File 'lib/harbor/mailer.rb', line 26

def self.host
  @@host rescue "www.example.com"
end

.host=(host) ⇒ Object



22
23
24
# File 'lib/harbor/mailer.rb', line 22

def self.host=(host)
  @@host = host
end

.layoutObject



18
19
20
# File 'lib/harbor/mailer.rb', line 18

def self.layout
  @@layout rescue nil
end

.layout=(layout) ⇒ Object



14
15
16
# File 'lib/harbor/mailer.rb', line 14

def self.layout=(layout)
  @@layout = layout
end

Instance Method Details

#hostObject



60
61
62
# File 'lib/harbor/mailer.rb', line 60

def host
  @host
end

#methodObject

We want to automatically assign the value of @mailer to self whenever a view is passed to the mailer object. This lets us use, for instance, the envelope_id to track click-through’s and bounces using the same identifier.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/harbor/mailer.rb', line 45

%w(html= text=).each do |method|
  define_method(method) do |value|
    if value.is_a?(Harbor::View)
      value.context.merge(:mailer => self)
    end

    if @layout && layout = @layout.dup
      layout.sub!(/(\.html\.erb$)|$/, ".txt.erb") if method == "text="
      value = Harbor::View.new(layout, :content => value, :mailer => self)
    end

    super(value)
  end
end

#send!Object



91
92
93
# File 'lib/harbor/mailer.rb', line 91

def send!
  mail_server.deliver(self)
end

#tokenize_urls!(mail_server_url) ⇒ Object

Tokenizes urls in the email body by replacing them with the mail_server_url provided. The message’s envelope_id and a base64 encoded version of the original url are passed to the URL provided.

mailer.html = 'Please visit <a href="http://example.com">our site</a> for details.'
mailer.tokenize_urls!("http://example.com/.m/%s?redirect=%s")
mailer.html # => "Please visit <a href=\"http://example.com/.m/%2AF%2Ch2Gtn.ny1poJnnvvCeSMZA?redirect=aHR0cDovL2V4YW1wbGUuY29t%0A\">our site</a> for details."


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/harbor/mailer.rb', line 73

def tokenize_urls!(mail_server_url)
  mail_server_url = "http://#{mail_server_url}" unless mail_server_url =~ /^http/

  [:@html, :@text].each do |ivar|
    if content = instance_variable_get(ivar)
      new_content = content.to_s.gsub(/(https?:\/\/.+?(?=[" <]|$))(\W*)(.{4}|$)/) do |url|
        # Don't tokenize the inner text of a link
        if $3 == '</a>'
          url
        else
          (mail_server_url % [CGI.escape(envelope_id), CGI.escape([$1].pack("m"))]) + $2 + $3
        end
      end
      instance_variable_set(ivar, new_content)
    end
  end
end