Class: CARPS::SMTP

Inherits:
Object
  • Object
show all
Defined in:
lib/carps/email/smtp.rb

Overview

SMTP connection

Instance Method Summary collapse

Constructor Details

#initialize(settings, password) ⇒ SMTP

Connects to the server



33
34
35
36
37
38
39
40
41
42
# File 'lib/carps/email/smtp.rb', line 33

def initialize settings, password
   @port = settings["port"]
   @username = settings["user"]
   @password = password
   @server = settings["server"]
   @starttls = settings["starttls"]
   @tls = settings["tls"]
   @login = settings["login"]
   @cram_md5 = settings["cram_md5"]
end

Instance Method Details

#ok?Boolean

Are the settings okay?

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
# File 'lib/carps/email/smtp.rb', line 60

def ok?
   good = false
   begin
      with_attempt_connection {}
      good = true
   rescue StandardError => e
      UI::put_error e.to_s
   end
   good
end

#send(to, message) ⇒ Object

Send an email message

Untaints its arguments



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/carps/email/smtp.rb', line 47

def send to, message
   to.untaint
   message.untaint
   if $DEBUG
      puts "Sending mail to #{to}"
   end
   with_connection do |smtp|
      message = to_mail "Content-Type: application/octet-stream\r\n" + message
      smtp.send_message message, @username, [to]
   end
end

#with_connection(&todo) ⇒ Object

Perform an action with a connection



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/carps/email/smtp.rb', line 72

def with_connection &todo
   until false 
      begin
         with_attempt_connection &todo
         return
      rescue Net::SMTPAuthenticationError => e
         UI::put_error e.message, false
         @password = UI::secret "Enter SMTP password for #{@username}:"
      rescue StandardError => e
         UI::warn "Could not connect to SMTP server", "Attempting to reconnect in 10 seconds."
         UI::put_error e.message, false
         sleep 10
      end
   end
end