Class: InboxBeam::Client

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

Overview

Writes a message directly into your own mailbox using IMAP APPEND.

This is NOT email delivery. Nothing is sent over SMTP and no message leaves for another server — it is appended straight into the target mailbox. Use it to land your own app notifications in your own inbox without SPF/DKIM/DMARC.

Constant Summary collapse

DEFAULT_PORT =
993
DEFAULT_MAILBOX =
"INBOX"

Instance Method Summary collapse

Constructor Details

#initialize(host:, auth:, port: DEFAULT_PORT, ssl: true, mailbox: DEFAULT_MAILBOX, from: nil, to: nil, unread: true, subject_prefix: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • host (String)

    IMAP host, e.g. “imap.gmail.com”

  • auth (Hash)

    { user:, pass: } for an app password, or { user:, access_token: } for OAuth2 (XOAUTH2)

  • port (Integer) (defaults to: DEFAULT_PORT)
  • ssl (Boolean) (defaults to: true)

    use TLS

  • mailbox (String) (defaults to: DEFAULT_MAILBOX)

    target mailbox / label

  • from (String, nil) (defaults to: nil)

    default From (defaults to auth)

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

    default To (defaults to auth)

  • unread (Boolean) (defaults to: true)

    leave appended messages unread

  • subject_prefix (String, nil) (defaults to: nil)

    prepended to every subject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/inbox_beam/client.rb', line 28

def initialize(host:, auth:, port: DEFAULT_PORT, ssl: true,
               mailbox: DEFAULT_MAILBOX, from: nil, to: nil,
               unread: true, subject_prefix: nil)
  @host = host
  @auth = auth
  @port = port
  @ssl = ssl
  @mailbox = mailbox
  @from = from
  @to = to
  @unread = unread
  @subject_prefix = subject_prefix
end

Instance Method Details

#append(mailbox, raw, unread: true) ⇒ InboxBeam::Result

Append an already-encoded RFC 5322 message (e.g. Mail::Message#encoded).

Returns:



58
59
60
61
62
63
# File 'lib/inbox_beam/client.rb', line 58

def append(mailbox, raw, unread: true)
  flags = unread ? [] : [:Seen]
  response = with_connection { |imap| imap.append(mailbox, raw, flags, nil) }
  uid_validity, uid = append_uid(response)
  Result.new(mailbox: mailbox, uid: uid, uid_validity: uid_validity)
end

#beam(subject:, text: nil, html: nil, from: nil, to: nil, mailbox: nil, unread: nil, date: nil) ⇒ InboxBeam::Result

Append one message to the mailbox.

Returns:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/inbox_beam/client.rb', line 44

def beam(subject:, text: nil, html: nil, from: nil, to: nil,
         mailbox: nil, unread: nil, date: nil)
  to ||= @to || @auth[:user]
  from ||= @from || to
  mailbox ||= @mailbox
  unread = @unread if unread.nil?
  subject = "#{@subject_prefix} #{subject}" if @subject_prefix

  raw = Message.build(from: from, to: to, subject: subject, text: text, html: html, date: date)
  append(mailbox, raw, unread: unread)
end