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:        persona.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: []) ⇒ 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.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/protege/gateway/mail/outbound.rb', line 69

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

Class Method Details

.build(from:, to:, subject:, body:, cc: [], bcc: [], in_reply_to: nil, references: nil, attachments: []) ⇒ ::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.

Returns:

  • (::Mail::Message)

    the assembled message.



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

def build(
  from:,
  to:,
  subject:,
  body:,
  cc: [],
  bcc: [],
  in_reply_to: nil,
  references: nil,
  attachments: []
)
  new(
    from:,
    to:,
    subject:,
    body:,
    cc:,
    bcc:,
    in_reply_to:,
    references:,
    attachments:
  ).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.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/protege/gateway/mail/outbound.rb', line 94

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?

    assign_content(m)
  end
end