Class: Ruote::SmtpParticipant

Inherits:
Object
  • Object
show all
Includes:
LocalParticipant, TemplateMixin
Defined in:
lib/ruote/part/smtp_participant.rb

Overview

A very stupid SMTP participant.

options

  • :server - the IP address or hostname of the SMTP server/gateway (defaults to ‘127.0.0.1’)

  • :port - the port of the SMTP server/gateway (defaults to 25)

  • :from - the from mail address (mandatory)

  • :to - the to mail address

  • :template - a String template for the mail message

  • :notification - when set to true, the flow will resume immediately after having sent the email

:template

@engine.register_participant(
  :no_good_notification,
  Ruote::SmtpParticipant,
  :server => 'smtp.example.com'
  :port => 25,
  :to => '[email protected]',
  :from => '[email protected]',
  :notification => true,
  :template => "Subject: ${f:email_subject}\n\nno good.")

Process variable / workitem field substitution works the same as in process definitions (in this example, the workitem field email_subject will be used as the subject of the email…)

:to or workitem.fields

The target of the email is either given via the workitem field ‘email_target’, either by the option :to. The workitem field takes precedence if both are present.

This parameter/option may be either a single (string) email address, either an array of (string) email addresses.

final note : mail listener

This participant cannot read POP/IMAP accounts for you. You have to use a mail listener or get a web reply by placing a link in the message…

Instance Attribute Summary

Attributes included from LocalParticipant

#context

Instance Method Summary collapse

Methods included from TemplateMixin

#render_default_template, #render_template

Methods included from LocalParticipant

#re_dispatch, #unschedule_re_dispatch

Methods included from ReceiverMixin

#applied_workitem, #fetch_flow_expression, #launch, #receive, #reply, #reply_to_engine, #sign

Constructor Details

#initialize(opts) ⇒ SmtpParticipant

Returns a new instance of SmtpParticipant.



83
84
85
86
# File 'lib/ruote/part/smtp_participant.rb', line 83

def initialize(opts)

  @opts = Ruote.keys_to_s(opts)
end

Instance Method Details

#cancel(fei, flavour) ⇒ Object



108
109
110
111
112
113
# File 'lib/ruote/part/smtp_participant.rb', line 108

def cancel(fei, flavour)

  # does nothing
  #
  # one variant could send a "cancellation email"
end

#consume(workitem) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ruote/part/smtp_participant.rb', line 88

def consume(workitem)

  to = workitem.fields['email_target'] || @opts['to']
  to = Array(to)

  text = render_template(
    @opts['template'],
    Ruote::Exp::FlowExpression.fetch(@context, workitem.fei.to_h),
    workitem)

  server = @opts['server'] || '127.0.0.1'
  port = @opts['port'] || 25

  Net::SMTP.start(server, port) do |smtp|
    smtp.send_message(text, @opts['from'] || '[email protected]', *to)
  end

  reply_to_engine(workitem) if @opts['notification']
end