lib/openwfe/participants/enoparticipant.rb in openwferu-0.9.4 vs lib/openwfe/participants/enoparticipant.rb in openwferu-0.9.5

- old
+ new

@@ -72,42 +72,103 @@ # end # set :field => 'customer_name' do # "Monsieur Toto" # end # participant :ref => 'eno' - # print "ok" # end # end # end # end # - # TODO #8426 : use a block to define template + # The 'template' parameter may contain an instance of File instead of + # an instance of String. # + # @engine.register_participant( + # 'eno', + # EmailNotificationParticipant.new( + # "googlemail.l.google.com", + # 25, + # "eno@outoftheblue.co.jp", + # File.new("path/to/my/mail/template.txt"))) + # + # You can also define the email template as a ruby block : + # + # p = EmailNotificationParticipant.new("googlemail.l.google.com", 25, "eno@co.co.jp") do | flowexpression, participant, workitem | + # + # # generally, only the workitem is used within a template + # + # s = "" + # + # # the header of the message + # + # s << "Subject: #{workitem.subject}\n\n" + # + # # then, the body + # + # workitem.attributes.each do |key, value| + # s << "- '#{k}' => '#{value}'\n" + # end + # s << "\ndone.\n" + # end + # + # Note that the template integrates the subject and requires then a double + # newline before the message body. + # class EmailNotificationParticipant include LocalParticipant # # Create a new email notification participant. Requires # a mail server, port, a from address, and a mail message template # - def initialize (smtp_server, smtp_port, from_address, template) + def initialize ( + smtp_server, smtp_port, from_address, template=nil, &block) + @smtp_server = smtp_server @smtp_port = smtp_port @from_address = from_address @template = template + + @block_template = block end + # + # The method called each time a workitem reaches this participant + # def consume (workitem) + fe = get_flow_expression(workitem) to_address = workitem.email_target + + # # 1. Expand variables - msg = OpenWFE::dosub(@template, fe, workitem) + + msg = if @block_template + @block_template.call(fe, self, workitem) + elsif @template + template = if @template.kind_of? File + @template.readlines + else + @template.to_s + end + OpenWFE::dosub(template, fe, workitem) + else + "(no template given)" + end + + puts "msg >>>\n#{msg}<<<" + + # # 2. Send message + Net::SMTP.start(@smtp_server, @smtp_port) do |smtp| smtp.send_message(msg, @from_address, to_address) end + + # # 3. Reply to engine - reply_to_engine(workitem) + + reply_to_engine(workitem) end end end