Class: Mail::SMTPConnection

Inherits:
Object show all
Includes:
CheckDeliveryParams
Defined in:
lib/mail/network/delivery_methods/smtp_connection.rb

Overview

Sending Email with SMTP

Mail allows you to send emails using an open SMTP connection. This is done by passing a created Net::SMTP object. This way we can get better performance to our local mail server by reducing the number of connections at any one time.

Sending via SMTP server on Localhost

To send mail open a connection with Net::Smtp using any options you like

Delivering the email

Once you have the settings right, sending the email is done by:

smtp_conn = Net::SMTP.start(settings[:address], settings[:port])
Mail.defaults do
  delivery_method :smtp_connection, { :connection => smtp_conn }
end

Mail.deliver do
  to 'mikel@test.lindsaar.net'
  from 'ada@test.lindsaar.net'
  subject 'testing sendmail'
  body 'testing sendmail'
end

Or by calling deliver on a Mail message

mail = Mail.new do
  to 'mikel@test.lindsaar.net'
  from 'ada@test.lindsaar.net'
  subject 'testing sendmail'
  body 'testing sendmail'
end

mail.deliver!

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from CheckDeliveryParams

#check_delivery_params

Constructor Details

- (SMTPConnection) initialize(values)

Returns a new instance of SMTPConnection

Raises:

  • (ArgumentError)


42
43
44
45
46
# File 'lib/mail/network/delivery_methods/smtp_connection.rb', line 42

def initialize(values)
  raise ArgumentError.new('A Net::SMTP object is required for this delivery method') if values[:connection].nil?
  self.smtp = values[:connection]
  self.settings = values
end

Instance Attribute Details

- (Object) settings

Returns the value of attribute settings



49
50
51
# File 'lib/mail/network/delivery_methods/smtp_connection.rb', line 49

def settings
  @settings
end

- (Object) smtp

Returns the value of attribute smtp



48
49
50
# File 'lib/mail/network/delivery_methods/smtp_connection.rb', line 48

def smtp
  @smtp
end

Instance Method Details

- (Object) deliver!(mail)

Send the message via SMTP. The from and to attributes are optional. If not set, they are retrieve from the Message.



53
54
55
56
57
58
# File 'lib/mail/network/delivery_methods/smtp_connection.rb', line 53

def deliver!(mail)
  smtp_from, smtp_to, message = check_delivery_params(mail)
  response = smtp.sendmail(message, smtp_from, smtp_to)

  settings[:return_response] ? response : self
end