Class: Mail::Sendmail

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

Overview

A delivery method implementation which sends via sendmail.

To use this, first find out where the sendmail binary is on your computer, if you are on a mac or unix box, it is usually in /usr/sbin/sendmail, this will be your sendmail location.

Mail.defaults do
  delivery_method :sendmail
end

Or if your sendmail binary is not at ‘/usr/sbin/sendmail’

Mail.defaults do
  delivery_method :sendmail, :location => '/absolute/path/to/your/sendmail'
end

Then just deliver the email as normal:

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Sendmail

Returns a new instance of Sendmail.



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

def initialize(values)
  self.settings = { :location       => '/usr/sbin/sendmail',
                    :arguments      => '-i -t' }.merge(values)
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



44
45
46
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 44

def settings
  @settings
end

Class Method Details

.call(path, arguments, destinations, mail) ⇒ Object



55
56
57
58
59
60
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 55

def Sendmail.call(path, arguments, destinations, mail)
  IO.popen("#{path} #{arguments} #{destinations}", "w+") do |io|
    io.puts mail.encoded.to_lf
    io.flush
  end
end

Instance Method Details

#deliver!(mail) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 46

def deliver!(mail)
  envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
  return_path = "-f \"#{envelope_from.to_s.shellescape}\"" if envelope_from

  arguments = [settings[:arguments], return_path].compact.join(" ")

  Sendmail.call(settings[:location], arguments, mail.destinations.collect(&:shellescape).join(" "), mail)
end