Parent

Methods

Class Index [+]

Quicksearch

Mail::SMTP

Sending Email with SMTP

Mail allows you to send emails using SMTP. This is done by wrapping Net::SMTP in an easy to use manner.

Sending via SMTP server on Localhost

Sending locally (to a postfix or sendmail server running on localhost) requires no special setup. Just to Mail.deliver &block or message.deliver! and it will be sent in this method.

Sending via MobileMe

  Mail.defaults do
    delivery_method :smtp, { :address              => "smtp.me.com",
                             :port                 => 587,
                             :domain               => 'your.host.name',
                             :user_name            => '<username>',
                             :password             => '<password>',
                             :authentication       => 'plain',
                             :enable_starttls_auto => true  }
  end

Sending via GMail

  Mail.defaults do
    delivery_method :smtp, { :address              => "smtp.gmail.com",
                             :port                 => 587,
                             :domain               => 'your.host.name',
                             :user_name            => '<username>',
                             :password             => '<password>',
                             :authentication       => 'plain',
                             :enable_starttls_auto => true  }
  end

Certificate verification

When using TLS, some mail servers provide certificates that are self-signed or whose names do not exactly match the hostname given in the address. OpenSSL will reject these by default. The best remedy is to use the correct hostname or update the certificate authorities trusted by your ruby. If that isn’t possible, you can control this behavior with an :openssl_verify_mode setting. Its value may be either an OpenSSL verify mode constant (OpenSSL::SSL::VERIFY_NONE), or a string containing the name of an OpenSSL verify mode (none, peer, client_once, fail_if_no_peer_cert).

Others

Feel free to send me other examples that were tricky

Delivering the email

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

  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

settings[RW]

Public Class Methods

new(values) click to toggle source
    # File lib/mail/network/delivery_methods/smtp.rb, line 76
76:     def initialize(values)
77:       self.settings = { :address              => "localhost",
78:                         :port                 => 25,
79:                         :domain               => 'localhost.localdomain',
80:                         :user_name            => nil,
81:                         :password             => nil,
82:                         :authentication       => nil,
83:                         :enable_starttls_auto => true,
84:                         :openssl_verify_mode  => nil,
85:                         :ssl                  => nil,
86:                         :tls                  => nil
87:                       }.merge!(values)
88:     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.rb, line 94
 94:     def deliver!(mail)
 95: 
 96:       # Set the envelope from to be either the return-path, the sender or the first from address
 97:       envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
 98:       if envelope_from.blank?
 99:         raise ArgumentError.new('A sender (Return-Path, Sender or From) required to send a message') 
100:       end
101:       
102:       destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
103:       if destinations.blank?
104:         raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message') 
105:       end
106:       
107:       message ||= mail.encoded if mail.respond_to?(:encoded)
108:       if message.blank?
109:         raise ArgumentError.new('A encoded content is required to send a message')
110:       end
111:       
112:       smtp = Net::SMTP.new(settings[:address], settings[:port])
113:       if settings[:tls] || settings[:ssl]
114:         if smtp.respond_to?(:enable_tls)
115:           unless settings[:openssl_verify_mode]
116:             smtp.enable_tls
117:           else
118:             openssl_verify_mode = settings[:openssl_verify_mode]
119:             if openssl_verify_mode.kind_of?(String)
120:               openssl_verify_mode = "OpenSSL::SSL::VERIFY_#{openssl_verify_mode.upcase}".constantize
121:             end
122:             context = Net::SMTP.default_ssl_context
123:             context.verify_mode = openssl_verify_mode
124:             smtp.enable_tls(context)
125:           end        
126:         end
127:       elsif settings[:enable_starttls_auto]
128:         if smtp.respond_to?(:enable_starttls_auto) 
129:           unless settings[:openssl_verify_mode]
130:             smtp.enable_starttls_auto
131:           else
132:             openssl_verify_mode = settings[:openssl_verify_mode]
133:             if openssl_verify_mode.kind_of?(String)
134:               openssl_verify_mode = "OpenSSL::SSL::VERIFY_#{openssl_verify_mode.upcase}".constantize
135:             end
136:             context = Net::SMTP.default_ssl_context
137:             context.verify_mode = openssl_verify_mode
138:             smtp.enable_starttls_auto(context)
139:           end
140:         end
141:       end
142:       
143:       response = nil
144:       smtp.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication]) do |smtp_obj|
145:         response = smtp_obj.sendmail(message, envelope_from, destinations)
146:       end
147: 
148:       return settings[:return_response] ? response : self
149:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.