Class: Mail::SMTPConnection

Inherits:
Object show all
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 '[email protected]'
  from '[email protected]'
  subject 'testing sendmail'
  body 'testing sendmail'
end

Or by calling deliver on a Mail message

mail = Mail.new do
  to '[email protected]'
  from '[email protected]'
  subject 'testing sendmail'
  body 'testing sendmail'
end

mail.deliver!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ SMTPConnection

Returns a new instance of SMTPConnection.

Raises:

  • (ArgumentError)


39
40
41
42
43
# File 'lib/mail/network/delivery_methods/smtp_connection.rb', line 39

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

#settingsObject

Returns the value of attribute settings.



46
47
48
# File 'lib/mail/network/delivery_methods/smtp_connection.rb', line 46

def settings
  @settings
end

#smtpObject

Returns the value of attribute smtp.



45
46
47
# File 'lib/mail/network/delivery_methods/smtp_connection.rb', line 45

def smtp
  @smtp
end

Instance Method Details

#deliver!(mail) ⇒ Object

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mail/network/delivery_methods/smtp_connection.rb', line 50

def deliver!(mail)

  # Set the envelope from to be either the return-path, the sender or the first from address
  envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
  if envelope_from.blank?
    raise ArgumentError.new('A sender (Return-Path, Sender or From) required to send a message') 
  end
  
  destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
  if destinations.blank?
    raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message') 
  end
  
  message ||= mail.encoded if mail.respond_to?(:encoded)
  if message.blank?
    raise ArgumentError.new('A encoded content is required to send a message')
  end
        
  response = smtp.sendmail(message, envelope_from, destinations)
  
  settings[:return_response] ? response : self 
end