Class: Mail::Sendmail

Inherits:
Object
  • 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!

Direct Known Subclasses

Exim

Constant Summary collapse

DEFAULTS =
{
  :location   => '/usr/sbin/sendmail',
  :arguments  => '-i'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Sendmail

Returns a new instance of Sendmail.


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

def initialize(values)
  self.settings = self.class::DEFAULTS.merge(values)
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings


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

def settings
  @settings
end

Class Method Details

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


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

def self.call(path, arguments, destinations, encoded_message)
  popen "#{path} #{arguments} #{destinations}" do |io|
    io.puts ::Mail::Utilities.binary_unsafe_to_lf(encoded_message)
    io.flush
  end
end

.popen(command, &block) ⇒ Object


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

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

.shellquote(address) ⇒ Object

The following is an adaptation of ruby 1.9.2's shellwords.rb file, with the following modifications:

  • Wraps in double quotes

  • Allows '+' to accept email addresses with them

  • Allows '~' as it is not unescaped in double quotes


85
86
87
88
89
90
91
92
93
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 85

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

#deliver!(mail) ⇒ Object


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

def deliver!(mail)
  smtp_from, smtp_to, message = Mail::CheckDeliveryParams.check(mail)

  from = "-f #{self.class.shellquote(smtp_from)}" if smtp_from
  to = smtp_to.map { |_to| self.class.shellquote(_to) }.join(' ')

  arguments = "#{settings[:arguments]} #{from} --"
  self.class.call(settings[:location], arguments, to, message)
end