Class: Heathrow::SmtpSender

Inherits:
Object
  • Object
show all
Defined in:
lib/heathrow/smtp_sender.rb

Overview

SMTP sender module - supports OAuth2 and standard SMTP Configure oauth2_domains, smtp_command, safe_dir in ~/.heathrowrc

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_address, config = {}) ⇒ SmtpSender

Returns a new instance of SmtpSender.



13
14
15
16
17
18
19
20
21
# File 'lib/heathrow/smtp_sender.rb', line 13

def initialize(from_address, config = {})
  @from_address = from_address
  @config = config
  cfg = Heathrow::Config.instance
  @oauth2_domains = cfg&.rc('oauth2_domains', %w[gmail.com]) || %w[gmail.com]
  @smtp_command = cfg&.rc('smtp_command', File.expand_path('~/bin/gmail_smtp'))
  @safe_dir = cfg&.rc('safe_dir', File.join(Dir.home, '.heathrow', 'mail'))
  @smtp_log_path = File.join(@safe_dir, '.smtp.log')
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/heathrow/smtp_sender.rb', line 11

def config
  @config
end

#from_addressObject (readonly)

Returns the value of attribute from_address.



11
12
13
# File 'lib/heathrow/smtp_sender.rb', line 11

def from_address
  @from_address
end

Class Method Details

.send_message(from, to, subject, body, in_reply_to = nil, config = {}) ⇒ Object

Class method for quick sending



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/heathrow/smtp_sender.rb', line 50

def self.send_message(from, to, subject, body, in_reply_to = nil, config = {})
  require 'mail'
  
  mail = Mail.new do
    from     from
    to       to
    subject  subject
    body     body
  end
  
  # Add threading headers if replying
  if in_reply_to
    mail['In-Reply-To'] = in_reply_to
    mail['References'] = in_reply_to
  end
  
  sender = new(from, config)
  sender.send(mail)
end

Instance Method Details

#send(mail, recipients = nil) ⇒ Hash

Send an email message

Parameters:

  • mail (Mail::Message)

    The mail object to send

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

    Recipient email addresses

Returns:

  • (Hash)

    Result with :success and :message keys



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/heathrow/smtp_sender.rb', line 27

def send(mail, recipients = nil)
  # Extract recipients from mail object if not provided
  recipients ||= extract_recipients(mail)
  
  # Determine sending method based on from address
  if uses_oauth2?
    send_via_gmail_smtp(mail, recipients)
  elsif config['smtp_server']
    send_via_smtp_server(mail, recipients)
  else
    { success: false, message: "No SMTP configuration available for #{from_address}" }
  end
end

#uses_oauth2?Boolean

Check if this address uses OAuth2 via gmail_smtp

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/heathrow/smtp_sender.rb', line 42

def uses_oauth2?
  return false unless from_address
  
  domain = from_address.split('@').last
  @oauth2_domains.include?(domain)
end