Class: Mailing::SmtpChannel

Inherits:
BaseChannel show all
Defined in:
lib/mailing/smtp_channel.rb

Instance Attribute Summary

Attributes inherited from BaseChannel

#config

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SmtpChannel

config: hash with smtp configuration

:address              => "host.domain.com",
:port                 => 25,
:domain               => 'domain.com',
:user_name            => '[email protected]',
:password             => 'password',
:authentication       => :login,
:enable_starttls_auto => true,
:openssl_verify_mode  => 'none'



18
19
20
21
# File 'lib/mailing/smtp_channel.rb', line 18

def initialize(config)
  super
  @smtp = nil
end

Instance Method Details

#deliver(mail, envelope_from, recipient) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/mailing/smtp_channel.rb', line 30

def deliver(mail, envelope_from, recipient)
  begin
    @smtp.sendmail(mail, envelope_from, recipient)
    "Sent to #{recipient}"
  rescue Exception => e
    finish
    start
    "Error for #{recipient}: #{e}".strip
  end
end

#finishObject



41
42
43
# File 'lib/mailing/smtp_channel.rb', line 41

def finish
  @smtp.finish
end

#startObject



23
24
25
26
27
28
# File 'lib/mailing/smtp_channel.rb', line 23

def start
  @smtp = Net::SMTP.new(@config[:address], @config[:port])
  @smtp.enable_starttls_auto if @config[:enable_starttls_auto]
  @smtp.start(@config[:domain], @config[:user_name],
             @config[:password], @config[:authentication])
end