Class: Mail::Sendmail

Inherits:
Object show all
Includes:
CheckDeliveryParams
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 '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!

Direct Known Subclasses

Exim

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from CheckDeliveryParams

#check_delivery_params

Constructor Details

- (Sendmail) initialize(values)

A new instance of Sendmail



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

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

Instance Attribute Details

- (Object) settings

Returns the value of attribute settings



47
48
49
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 47

def settings
  @settings
end

Class Method Details

+ (Object) call(path, arguments, destinations, mail)



61
62
63
64
65
66
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 61

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

+ (Object) popen(command, &block)



69
70
71
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 69

def self.popen(command, &block)
  IO.popen "#{command} 2>&1", 'w+', &block
end

+ (Object) shellquote(address)

The following is an adaptation of ruby 1.9.2's shellwords.rb file, it is modified to include '+' in the allowed list to allow for sendmail to accept email addresses as the sender with a + in them.



81
82
83
84
85
86
87
88
89
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 81

def self.shellquote(address)
  # Process as a single byte sequence because not all shell
  # implementations are multibyte aware.
  #
  # A LF cannot be escaped with a backslash because a backslash + LF
  # combo is regarded as line continuation and simply ignored. Strip it.
  escaped = address.gsub(/([^A-Za-z0-9_\s\+\-.,:\/@])/n, "\\\\\\1").gsub("\n", '')
  %("#{escaped}")
end

Instance Method Details

- (Object) deliver!(mail)



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 49

def deliver!(mail)
  check_delivery_params(mail)

  envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
  return_path = "-f #{self.class.shellquote(envelope_from)}" if envelope_from

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

  quoted_destinations = mail.destinations.collect { |d| self.class.shellquote(d) }
  self.class.call(settings[:location], arguments, quoted_destinations.join(' '), mail)
end