Class: Mail::SMTP

Inherits:
Object
  • Object
show all
Defined in:
lib/mail/network/delivery_methods/smtp.rb

Overview

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               => 'baci.lindsaar.net',
                           :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 '[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) ⇒ SMTP

Returns a new instance of SMTP.



64
65
66
67
68
69
70
71
72
# File 'lib/mail/network/delivery_methods/smtp.rb', line 64

def initialize(values)
  self.settings = { :address              => "localhost",
                    :port                 => 25,
                    :domain               => 'localhost.localdomain',
                    :user_name            => nil,
                    :password             => nil,
                    :authentication       => nil,
                    :enable_starttls_auto => true  }.merge!(values)
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



74
75
76
# File 'lib/mail/network/delivery_methods/smtp.rb', line 74

def settings
  @settings
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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mail/network/delivery_methods/smtp.rb', line 78

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
  
  smtp = Net::SMTP.new(settings[:address], settings[:port])
  if settings[:enable_starttls_auto]
    smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto) 
  end
  
  smtp.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication]) do |smtp|
    smtp.sendmail(message, envelope_from, destinations)
  end
  
  self
end