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

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 64
64:     def initialize(values)
65:       self.settings = { :address              => "localhost",
66:                         :port                 => 25,
67:                         :domain               => 'localhost.localdomain',
68:                         :user_name            => nil,
69:                         :password             => nil,
70:                         :authentication       => nil,
71:                         :enable_starttls_auto => true  }.merge!(values)
72:     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 78
 78:     def deliver!(mail)
 79: 
 80:       # Set the envelope from to be either the return-path, the sender or the first from address
 81:       envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
 82:       if envelope_from.blank?
 83:         raise ArgumentError.new('A sender (Return-Path, Sender or From) required to send a message') 
 84:       end
 85:       
 86:       destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
 87:       if destinations.blank?
 88:         raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message') 
 89:       end
 90:       
 91:       message ||= mail.encoded if mail.respond_to?(:encoded)
 92:       if message.blank?
 93:         raise ArgumentError.new('A encoded content is required to send a message')
 94:       end
 95:       
 96:       smtp = Net::SMTP.new(settings[:address], settings[:port])
 97:       if settings[:enable_starttls_auto]
 98:         smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto) 
 99:       end
100:       
101:       smtp.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication]) do |smtp|
102:         smtp.sendmail(message, envelope_from, destinations)
103:       end
104:       
105:       self
106:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.