Class: Protege::Gateway::Mail::Outbound

Inherits:
Object
  • Object
show all
Defined in:
lib/protege/gateway/mail/outbound.rb

Overview

Builder for outbound ::Mail::Message objects — the one place the engine assembles mail it sends, whether a brand-new console conversation or a threaded reply.

Centralizing construction keeps the conventions (a synthetic angle-bracketed Message-ID, a UTF-8 plain-text body, a Date stamp) and the threading-header normalization (+In-Reply-To+ via MessageId, References via References) in a single tested spot, rather than duplicated across RepliesController, ThreadsController, and host tools like SendEmailTool.

Examples:

A threaded reply

Outbound.build(
  from:        agent.email_address,
  to:          inbound.from_address,
  subject:     'Re: Hello',
  body:        'Thanks!',
  in_reply_to: inbound.message_id,
  references:  inbound.references_header
)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from:, to:, subject:, body:, cc: [], bcc: [], in_reply_to: nil, references: nil, attachments: [], hops: nil) ⇒ Outbound

Store the envelope and threading inputs. Prefer the .build factory.

Parameters:

  • from (String)

    the sender address.

  • to (String, Array<String>)

    the recipient address(es).

  • subject (String)

    the subject line.

  • body (String)

    the plain-text body.

  • cc (String, Array<String>) (defaults to: [])

    carbon-copy recipient(s); omitted from the message when empty.

  • bcc (String, Array<String>) (defaults to: [])

    blind-carbon-copy recipient(s); omitted when empty.

  • in_reply_to (String, MessageId, nil) (defaults to: nil)

    the parent message id, normalized when present.

  • references (References, Array<String>, String, nil) (defaults to: nil)

    the prior reference chain; Strings are split on whitespace, Arrays/References normalized per id.

  • attachments (Array<Mail::Attachment>) (defaults to: [])

    files to attach; when any are present the message is assembled as multipart with the body as a text part.

  • hops (Integer, nil) (defaults to: nil)

    the X-Protege-Recursion hop count to stamp — agent-sent mail passes it (see Gateway::RECURSION_HEADER); nil (operator/console mail) leaves the header off entirely.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/protege/gateway/mail/outbound.rb', line 74

def initialize(
  from:,
  to:,
  subject:,
  body:,
  cc: [],
  bcc: [],
  in_reply_to: nil,
  references: nil,
  attachments: [],
  hops: nil
)
  @from        = from
  @to          = to
  @subject     = subject
  @body        = body
  @cc          = cc
  @bcc         = bcc
  @in_reply_to = in_reply_to
  @references  = references
  @attachments = attachments
  @hops        = hops
end

Class Method Details

.build(from:, to:, subject:, body:, cc: [], bcc: [], in_reply_to: nil, references: nil, attachments: [], hops: nil) ⇒ ::Mail::Message

Build and return a ready-to-deliver ::Mail::Message.

Parameters:

  • from (String)

    the sender address.

  • to (String, Array<String>)

    the recipient address(es).

  • subject (String)

    the subject line.

  • body (String)

    the plain-text body.

  • cc (String, Array<String>) (defaults to: [])

    carbon-copy recipient(s); omitted from the message when empty.

  • bcc (String, Array<String>) (defaults to: [])

    blind-carbon-copy recipient(s); omitted when empty.

  • in_reply_to (String, MessageId, nil) (defaults to: nil)

    the parent message id, normalized when present.

  • references (References, Array<String>, String, nil) (defaults to: nil)

    the prior reference chain; Strings are split on whitespace, Arrays/References normalized per id.

  • attachments (Array<Mail::Attachment>) (defaults to: [])

    files to attach; when any are present the message is assembled as multipart with the body as a text part.

  • hops (Integer, nil) (defaults to: nil)

    the X-Protege-Recursion hop count to stamp — agent-sent mail passes it (see Gateway::RECURSION_HEADER); nil (operator/console mail) leaves the header off entirely.

Returns:

  • (::Mail::Message)

    the assembled message.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/protege/gateway/mail/outbound.rb', line 44

def build(
  from:,
  to:,
  subject:,
  body:,
  cc: [],
  bcc: [],
  in_reply_to: nil,
  references: nil,
  attachments: [],
  hops: nil
)
  new(
    from:,
    to:,
    subject:,
    body:,
    cc:,
    bcc:,
    in_reply_to:,
    references:,
    attachments:,
    hops:
  ).message
end

Instance Method Details

#message::Mail::Message

Assemble the ::Mail::Message from the stored inputs.

Returns:

  • (::Mail::Message)

    the message with envelope, body, id, date, and threading headers set.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/protege/gateway/mail/outbound.rb', line 101

def message
  ::Mail.new.tap do |m|
    m.from        = @from
    m.to          = @to
    m.cc          = @cc
    m.bcc         = @bcc
    m.subject     = @subject
    m.message_id  = MessageId.synthetic(domain: sender_domain).value
    m.date        = Time.current
    m.in_reply_to = parent_id if parent_id
    m.references  = reference_values if reference_values.any?

    stamp_hops(m)
    assign_content(m)
  end
end