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

Defined Under Namespace

Classes: DeliveryError

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Sendmail

Returns a new instance of Sendmail.

Raises:

  • (ArgumentError)


51
52
53
54
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 51

def initialize(values)
  self.settings = self.class::DEFAULTS.merge(values)
  raise ArgumentError, ":arguments expected to be an Array of individual string args" if settings[:arguments].is_a?(String)
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

Instance Method Details

#deliver!(mail) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 60

def deliver!(mail)
  envelope = Mail::SmtpEnvelope.new(mail)

  command = [settings[:location]]
  command.concat Array(settings[:arguments])
  command.concat [ '-f', envelope.from ] if envelope.from

  if destinations = destinations_for(envelope)
    command.push '--'
    command.concat destinations
  end

  popen(command) do |io|
    io.puts ::Mail::Utilities.binary_unsafe_to_lf(envelope.message)
    io.flush
  end
end

#destinations_for(envelope) ⇒ Object



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

def destinations_for(envelope)
  envelope.to
end