Parent

Methods

Class Index [+]

Quicksearch

Mail::SMTPConnection

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!

Attributes

smtp[RW]
settings[RW]

Public Class Methods

new(values) click to toggle source
    # File lib/mail/network/delivery_methods/smtp_connection.rb, line 39
39:     def initialize(values)
40:       raise ArgumentError.new('A Net::SMTP object is required for this delivery method') if values[:connection].nil?
41:       self.smtp = values[:connection]
42:       self.settings = values
43:     end

Public Instance Methods

deliver!(mail) click to toggle source

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

    # File lib/mail/network/delivery_methods/smtp_connection.rb, line 50
50:     def deliver!(mail)
51: 
52:       # Set the envelope from to be either the return-path, the sender or the first from address
53:       envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
54:       if envelope_from.blank?
55:         raise ArgumentError.new('A sender (Return-Path, Sender or From) required to send a message') 
56:       end
57:       
58:       destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
59:       if destinations.blank?
60:         raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message') 
61:       end
62:       
63:       message ||= mail.encoded if mail.respond_to?(:encoded)
64:       if message.blank?
65:         raise ArgumentError.new('A encoded content is required to send a message')
66:       end
67:             
68:       response = smtp.sendmail(message, envelope_from, destinations)
69:       
70:       settings[:return_response] ? response : self 
71:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.