class Mail::Sendmail
mail.deliver!
end
body ‘testing sendmail’
subject ‘testing sendmail’
from ‘ada@test.lindsaar.net’
to ‘mikel@test.lindsaar.net’
mail = Mail.new do
Or by calling deliver on a Mail message
end
body ‘testing sendmail’
subject ‘testing sendmail’
from ‘ada@test.lindsaar.net’
to ‘mikel@test.lindsaar.net’
Mail.deliver do
Then just deliver the email as normal:
end
delivery_method :sendmail, :location => ‘/absolute/path/to/your/sendmail’
Mail.defaults do
Or if your sendmail binary is not at ‘/usr/sbin/sendmail’
end
delivery_method :sendmail
Mail.defaults do
be your sendmail location.
if you are on a mac or unix box, it is usually in /usr/sbin/sendmail, this will
To use this, first find out where the sendmail binary is on your computer,
A delivery method implementation which sends via sendmail.
def deliver!(mail)
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
def deprecation_warn
def deprecation_warn defined?(ActiveSupport::Deprecation.warn) ? ActiveSupport::Deprecation.method(:warn) : Kernel.method(:warn) end
def destinations_for(envelope)
def destinations_for(envelope) envelope.to end
def initialize(values)
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
def old_deliver(envelope)
def old_deliver(envelope) smtp_from = envelope.from smtp_to = destinations_for(envelope) from = "-f #{shellquote(smtp_from)}" if smtp_from destination = smtp_to.map { |to| shellquote(to) }.join(' ') arguments = "#{settings[:arguments]} #{from} --" command = "#{settings[:location]} #{arguments} #{destination}" popen command do |io| io.puts ::Mail::Utilities.binary_unsafe_to_lf(envelope.message) io.flush end end
def popen(command, &block)
def popen(command, &block) IO.popen(command, 'w+', :err => :out, &block).tap do if $?.exitstatus != 0 raise DeliveryError, "Delivery failed with exitstatus #{$?.exitstatus}: #{command.inspect}" end end end
def shellquote(address)
- Allows '+' to accept email addresses with them
- Wraps in double quotes
with the following modifications:
The following is an adaptation of ruby 1.9.2's shellwords.rb file,
def 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