Class: Mail::NotesMailer

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

Constant Summary collapse

@@logger =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ NotesMailer

NotesMailer supports the following settings: Mandatory:

:user_name  ... Lotus Notes user
:db             ... Latus Notes database

Optional

:password   ... Latus Notes password
:adress       ... Adress of your Lotus Notes Server, emty if you are using a local database 
::mock_win32ole => true     ... test without using Lotus Notes 
:logger => true,                 ... use logger
:debug => true                   ...


78
79
80
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
# File 'lib/notes_mailer.rb', line 78

def initialize(values)
  self.settings = { :adress       => '', #server is localhost
                         :password => ''
                         }.merge(values)
                         
 
  #raise "Notes password missing" unless settings[:password]
  raise "Notes db missing" unless settings[:db]
  raise "Notes user_name missing" unless settings[:user_name]
  
  if settings[:mock_win32ole] 
    $LOAD_PATH.unshift File.dirname(__FILE__) + '/../test/mock/win32ole'
  end
  require 'win32ole'
  
  if settings[:logger] and !@@logger
    NotesMailer.init_logger(settings[:debug])
  end

  log_debug "--- settings --- "
  log_debug settings

  s = WIN32OLE.new 'Lotus.NotesSession'
  s.Initialize(settings[:password])
  
  @notes_db = s.GetDatabase(settings[:adress], settings[:db])
  raise "Cannot open #{settings[:db]}" unless @notes_db.IsOpen()

end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



108
109
110
# File 'lib/notes_mailer.rb', line 108

def settings
  @settings
end

Class Method Details

.deliveriesObject

Provides a store of all the emails



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

def NotesMailer.deliveries
  @@deliveries ||= []
end

.deliveries=(val) ⇒ Object



64
65
66
# File 'lib/notes_mailer.rb', line 64

def NotesMailer.deliveries=(val)
  @@deliveries = val
end

.init_logger(debug) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/notes_mailer.rb', line 157

def NotesMailer.init_logger(debug)
  FileUtils::mkdir_p("#{Dir.pwd}/log")
  @@logger = Log4r::Logger.new("notes_mailer_logger") 
  
  if debug
    puts "see logfile: #{Dir.pwd}/log/notes_mailer.log"
    log_level = Log4r::DEBUG
  else
    log_level = Log4r::INFO
  end
  
  Log4r::FileOutputter.new('logfile', 
                     :filename=>"#{Dir.pwd}/log/notes_mailer.log", 
                     :trunc=>false,
                     :level=> log_level)
                     
  @@logger.add('logfile')

end

Instance Method Details

#deliver!(mail) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/notes_mailer.rb', line 110

def deliver!(mail)
  #Mail::NotesMailer.deliveries << mail
  
  destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
  if destinations.blank?
    raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message') 
  end
  
  log_debug "--- log mail ---"
  log_debug "destinations : "
  log_debug  destinations
  log_debug "mail to      : "
  log_debug mail.to
  log_debug "mail cc      : "
  log_debug mail.cc
  log_debug "mail bcc      : "
  log_debug mail.bcc
  log_debug "mail subject : "
  log_debug mail.subject
  log_debug "mail body    : " 
  log_debug mail.body.to_s 
  log_debug "--- end ---"
  
  new_mail = @notes_db.CreateDocument
  new_mail.replaceItemValue("Form",'Memo')
  new_mail.replaceItemValue("SendTo",mail.to) 
  new_mail.replaceItemValue("CopyTo",mail.cc) 
  new_mail.replaceItemValue("BlindCopyTo",mail.bcc) 
  new_mail.replaceItemValue("Subject",mail.subject)
  
  #new_mail.replaceItemValue("Body", mail.body.to_s)
  rti = new_mail.CreateRichTextItem("Body")
  rti.AppendText( mail.body.to_s)
  rti.AddNewLine()
  rti.AppendText( "\nfrom #{mail.from.to_s}\n") if mail.from.to_s.size > 0
  log_debug("\nfrom #{mail.from.to_s}\n") if mail.from.to_s.size > 0

  mail.files_to_attach.each do |filename|
    log_debug "attach: " + filename
    # EMBED_ATTACHMENT (1454), EMBED_OBJECT (1453), EMBED_OBJECTLINK (1452)
    rti.EmbedObject(1454,File.basename(filename),filename) # 1454 == EMBED_ATTACHMENT
  end      
  new_mail.Send(0) 
  
  self
end

#log_debug(*args, &block) ⇒ Object



181
182
183
184
185
186
# File 'lib/notes_mailer.rb', line 181

def log_debug(*args, &block)
  if @@logger
     p args
     @@logger.debug(*args, &block)
  end
end

#log_info(*args, &block) ⇒ Object



176
177
178
179
180
# File 'lib/notes_mailer.rb', line 176

def log_info(*args, &block)
  if @@logger
     @@logger.info(*args, &block)
  end
end