Class: FastMailer::SMTP

Inherits:
Object
  • Object
show all
Includes:
Hooks
Defined in:
lib/fast-mailer/smtp.rb

Overview

Sends messages using an SMTP server. The SMTP options is passed through FastMailer::Configuration, so you can set your system up to automatically use your SMTP configuration. Also, FastMailer::SMTP supports that you send multiple emails over a single connection, allowing for faster delivery of multiple emails.

Note that if an error is raised by Net::SMTP, the connection may not be reliable. As a best practice, always discard the current connection and reopen a new one in case you encounter an error. Do not attempt to continue sending mail on the same connection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ SMTP

Returns a new instance of SMTP.



23
24
25
# File 'lib/fast-mailer/smtp.rb', line 23

def initialize(options = nil)
  @configuration = FastMailer::Configuration.smtp_configuration options
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



21
22
23
# File 'lib/fast-mailer/smtp.rb', line 21

def configuration
  @configuration
end

Instance Method Details

#deliver(mail) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/fast-mailer/smtp.rb', line 46

def deliver(mail)
  if @opened
    perform_delivery(mail)
  else
    open do
      deliver mail
    end
  end
end

#openObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fast-mailer/smtp.rb', line 27

def open
  @opened = true

  configuration[:host] ||= configuration[:address]
  configuration[:username] ||= configuration[:user_name]
  
  @smtp = connection_class.new(configuration[:host], configuration[:port])
  if configuration[:enable_starttls_auto]
    smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto) 
  end
  
  @smtp.start(configuration[:domain], configuration[:username], configuration[:password], configuration[:authentication]) do |smtp|
    yield self
  end
ensure
  @smtp = nil
  @opened = false
end