Class: Waw::Tools::MailAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/waw/tools/mail/mail_agent.rb,
lib/waw/tools/mail/mail.rb,
lib/waw/tools/mail/mailbox.rb,
lib/waw/tools/mail/template.rb

Overview

Provides a tool to send mails simply

Defined Under Namespace

Classes: Mail, Mailbox, Template

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, force_real = false) ⇒ MailAgent

Creates a default mail agent



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/waw/tools/mail/mail_agent.rb', line 7

def initialize(opts = {}, force_real = false)
  @force_real = force_real
  if do_it_really?
    @smtp_host = opts[:host]
    @smtp_port = opts[:port]
    @smtp_timeout = opts[:timeout]
    raise ArgumentError, "Opts should include :host, :port and :timeout parameters\n#{opts.inspect} received"\
      if @smtp_host.nil? or @smtp_port.nil? or @smtp_timeout.nil?
  end
  @templates = {}
end

Instance Method Details

#add_template(name, *args) ⇒ Object

Adds a mail template under a given name



32
33
34
35
36
37
38
# File 'lib/waw/tools/mail/mail_agent.rb', line 32

def add_template(name, *args)
  if args.size == 1 and Template===args[0]
    @templates[name] = args[0]
  else
    @templates[name] = template(*args)
  end
end

#do_it_really?Boolean

Send mails, really?

Returns:



47
48
49
# File 'lib/waw/tools/mail/mail_agent.rb', line 47

def do_it_really?
  @force_real or (Waw.config and ['production', 'acceptation'].include?(Waw.config.deploy_mode))
end

#mail(*args) ⇒ Object

Builds a mail instance. This method should always be used preferably to an explicit Mail.new invocation.



21
22
23
# File 'lib/waw/tools/mail/mail_agent.rb', line 21

def mail(*args)
  Mail.new(*args)
end

#mailbox(receiver) ⇒ Object

Returns the mailbox of a given receiver, creating a new one if it cannot be found



58
59
60
# File 'lib/waw/tools/mail/mail_agent.rb', line 58

def mailbox(receiver)
  mailboxes[receiver]
end

#mailboxesObject

Returns installed mailboxes



52
53
54
# File 'lib/waw/tools/mail/mail_agent.rb', line 52

def mailboxes
  @mailboxes ||= Hash.new{|h, k| h[k] = Mailbox.new(k)}
end

#send_mail(*args) ⇒ Object Also known as: <<

Sends an email. If Waw.config.deploy_mode is set to ‘production’ or ‘acceptation’, the mail is really sent. Otherwise it is pushed in the receiver mailbox.

This method accepts the following invocation signatures:

# sending a real Mail instance (and only one)
agent.send_mail(mail)

# sending from a template for which receivers have been already set
agent.send_mail(:my_template, {:title => "Hello"})

# sending from a template, with explicit receivers as varargs
agent.send_mail(:my_template, {:title => "Hello"}, "[email protected]", ..., "[email protected]")

# sending from a template, with explicit receivers as an array
agent.send_mail(:my_template, {:title => "Hello"}, [...])


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/waw/tools/mail/mail_agent.rb', line 81

def send_mail(*args)
  # Let try to match the signature
  if args.size == 1 and Mail===args[0]
    # first one
    mail = args[0]
  elsif Symbol===args[0] and Hash===args[1]
    # the others
    mail = to_mail(args.shift, args.shift)
    unless args.empty?
      if args.size == 1 and Array===args[0]
        # third one
        mail.to = args[0]
      else
        # fourth one
        mail.to = args
      end
    else
      # second one
    end
  else
    raise ArgumentError, "Unable to find the send_mail sigature you want with #{args.inspect}"
  end
  
  if do_it_really?
    require 'net/smtp'
    smtp_conn = Net::SMTP.new(@smtp_host, @smtp_port)
    smtp_conn.open_timeout = @smtp_timeout
    smtp_conn.start
    smtp_conn.send_message(mail.dump, mail.from, *mail.to)
    smtp_conn.finish
  else
    #Waw.logger.debug mail.dump
    sent = Mail.parse(mail.dump)
    mail.to.each {|who| mailbox(who) << sent.dup}
    mail.cc.each {|who| mailbox(who) << sent.dup}
    mail.bcc.each {|who| mailbox(who) << sent.dup}
  end
end

#template(*args) ⇒ Object

Builds a template instance. This method should always be used preferably to an explicit Template.new invocation.



27
28
29
# File 'lib/waw/tools/mail/mail_agent.rb', line 27

def template(*args)
  Template.new(*args)
end

#to_mail(template_name, wlang_context) ⇒ Object

Converts a named template to a mail

Raises:

  • (ArgumentError)


41
42
43
44
# File 'lib/waw/tools/mail/mail_agent.rb', line 41

def to_mail(template_name, wlang_context)
  raise ArgumentError, "No such template #{template_name}" unless @templates.has_key?(template_name)
  @templates[template_name].to_mail(wlang_context)
end