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.



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

def initialize(values)
  if values[:arguments].is_a?(String)
    deprecation_warn.call \
      'Initializing Mail::Sendmail with :arguments of type String is deprecated.' \
      ' Instead ensure :arguments is an array of strings, e.g. ["-i", "-t"]'
  end
  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

Instance Method Details

#deliver!(mail) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 64

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

  arguments = settings[:arguments]
  if arguments.is_a? String
    return old_deliver(envelope)
  end

  command = [settings[:location]]
  command.concat Array(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



60
61
62
# File 'lib/mail/network/delivery_methods/sendmail.rb', line 60

def destinations_for(envelope)
  envelope.to
end