Class: MailService
- Inherits:
-
Object
- Object
- MailService
- Defined in:
- lib/service/mail.rb
Constant Summary collapse
- @@instance =
nil
Instance Attribute Summary collapse
-
#default_recipients ⇒ Object
readonly
Returns the value of attribute default_recipients.
-
#default_sender ⇒ Object
readonly
Returns the value of attribute default_sender.
-
#retries ⇒ Object
readonly
Returns the value of attribute retries.
-
#smtp_port ⇒ Object
readonly
Returns the value of attribute smtp_port.
-
#smtp_server ⇒ Object
readonly
Returns the value of attribute smtp_server.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(smtp_server:, smtp_port:, default_sender:, default_recipients:, retries:) ⇒ MailService
constructor
A new instance of MailService.
- #send(subject:, body:, from: @default_sender, to: @default_recipients, retries: @retries) ⇒ Object
Constructor Details
#initialize(smtp_server:, smtp_port:, default_sender:, default_recipients:, retries:) ⇒ MailService
Returns a new instance of MailService.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/service/mail.rb', line 28 def initialize(smtp_server:, smtp_port:, default_sender:, default_recipients:, retries:) @smtp_server = smtp_server @smtp_port = smtp_port @default_sender = default_sender @default_recipients = default_recipients @retries = retries Mail.defaults do delivery_method :smtp, { address: smtp_server, port: smtp_port, openssl_verify_mode: 'none' } end end |
Instance Attribute Details
#default_recipients ⇒ Object (readonly)
Returns the value of attribute default_recipients.
8 9 10 |
# File 'lib/service/mail.rb', line 8 def default_recipients @default_recipients end |
#default_sender ⇒ Object (readonly)
Returns the value of attribute default_sender.
8 9 10 |
# File 'lib/service/mail.rb', line 8 def default_sender @default_sender end |
#retries ⇒ Object (readonly)
Returns the value of attribute retries.
8 9 10 |
# File 'lib/service/mail.rb', line 8 def retries @retries end |
#smtp_port ⇒ Object (readonly)
Returns the value of attribute smtp_port.
8 9 10 |
# File 'lib/service/mail.rb', line 8 def smtp_port @smtp_port end |
#smtp_server ⇒ Object (readonly)
Returns the value of attribute smtp_server.
8 9 10 |
# File 'lib/service/mail.rb', line 8 def smtp_server @smtp_server end |
Class Method Details
.instance ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/service/mail.rb', line 12 def self.instance return @@instance if @@instance smtp_server = ENV['SMTP_SERVER'] smtp_port = ENV['SMTP_PORT'].to_i default_sender = ENV['SMTP_SENDER'] default_recipients = ENV['SMTP_RECIPIENTS'] retries = 5 puts "server #{smtp_server}:#{smtp_port} from: #{default_sender} to:#{default_recipients}" @@instance = new(smtp_server:, smtp_port:, default_sender:, default_recipients:, retries:) end |
Instance Method Details
#send(subject:, body:, from: @default_sender, to: @default_recipients, retries: @retries) ⇒ Object
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 69 70 71 72 |
# File 'lib/service/mail.rb', line 44 def send(subject:, body:, from: @default_sender, to: @default_recipients, retries: @retries) = Mail.new do from from to to subject subject html_part do content_type 'text/html; charset=UTF-8' body body end end puts begin .deliver! rescue Net::OpenTimeout, Net::ReadTimeout => e if retries > 0 sleep 5 send(from:, to:, subject:, retries: retries - 1) else puts 'Retry limit exceeded. Email not sent.' end rescue StandardError => e puts "Error: #{e.}" end end |