class Mail::SMTP

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
Once you have the settings right, sending the email is done by:
=== Delivering the email
Feel free to send me other examples that were tricky
=== Others
or a string containing the name of an OpenSSL verify mode (none, peer).
verify mode constant (OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER),
an :openssl_verify_mode setting. Its value may be either an OpenSSL
that isn’t possible, you can control this behavior with
hostname or update the certificate authorities trusted by your ruby. If
OpenSSL will reject these by default. The best remedy is to use the correct
or whose names do not exactly match the hostname given in the address.
When using TLS, some mail servers provide certificates that are self-signed
=== Certificate verification
- when none of ‘tls`, `ssl`, `enable_starttls` or `enable_starttls_auto` is set, the fallback will be `enable_starttls` `:auto`.
- when providing a truthy value for `enable_starttls`, the `enable_starttls_auto`-flag will be ignored.
- to configure STARTTLS, use the `enable_starttls`-flag (instead of a combination of `enable_starttls` and `enable_starttls_auto`). Acceptable values are `:always`, `:auto` and `false`.
- when enabling `tls` (or `ssl`), setting (truthy values for) either `enable_starttls` or `enable_starttls_auto` will raise an ArgumentError as TLS and STARTTLS are mutually exclusive.
A few remarks:
=== Configuring TLS/SSL and STARTTLS
end
:enable_starttls => :auto }
:authentication => ’plain’,
:password => ‘<password>’,
:user_name => ‘<username>’,
:domain => ‘your.host.name’,
:port => 587,
delivery_method :smtp, { :address => “smtp.gmail.com”,
Mail.defaults do
=== Sending via GMail
end
:enable_starttls => :auto }
:authentication => ‘plain’,
:password => ‘<password>’,
:user_name => ‘<username>’,
:domain => ‘your.host.name’,
:port => 587,
delivery_method :smtp, { :address => “smtp.me.com”,
Mail.defaults do
=== Sending via MobileMe
be sent in this method.
no special setup. Just to Mail.deliver &block or message.deliver! and it will
Sending locally (to a postfix or sendmail server running on localhost) requires
=== Sending via SMTP server on Localhost
an easy to use manner.
Mail allows you to send emails using SMTP. This is done by wrapping Net::SMTP in
== Sending Email with SMTP

def build_smtp_session

def build_smtp_session
  if smtp_tls? && (settings[:enable_starttls] || settings[:enable_starttls_auto])
    raise ArgumentError, ":enable_starttls and :tls are mutually exclusive. Set :tls if you're on an SMTPS connection. Set :enable_starttls if you're on an SMTP connection and using STARTTLS for secure TLS upgrade."
  end
  Net::SMTP.new(settings[:address], settings[:port]).tap do |smtp|
    if smtp_tls?
      smtp.disable_starttls
      smtp.enable_tls(ssl_context)
    else
      smtp.disable_tls
      case smtp_starttls
      when :always
        smtp.enable_starttls(ssl_context)
      when :auto
        smtp.enable_starttls_auto(ssl_context)
      else
        smtp.disable_starttls
      end
    end
    smtp.open_timeout = settings[:open_timeout] if settings[:open_timeout]
    smtp.read_timeout = settings[:read_timeout] if settings[:read_timeout]
  end
end

def deliver!(mail)

def deliver!(mail)
  response = start_smtp_session do |smtp|
    Mail::SMTPConnection.new(:connection => smtp, :return_response => true).deliver!(mail)
  end
  settings[:return_response] ? response : self
end

def initialize(values)

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

def setting_provided?(k)

`k` is said to be provided when `settings` has a non-nil value for `k`.
def setting_provided?(k)
  !settings[k].nil?
end

def smtp_starttls

Providing a truthy value for `enable_starttls` will ignore `enable_starttls_auto`.
Else defaults to `:auto` when neither `enable_starttls*` flag is provided.
Yields `false` when `smtp_tls?`.
Yields one of `:always`, `:auto` or `false` based on `enable_starttls` and `enable_starttls_auto` flags.
def smtp_starttls
  return false if smtp_tls?
  if setting_provided?(:enable_starttls) && settings[:enable_starttls]
    # enable_starttls: provided and truthy
    case settings[:enable_starttls]
    when :auto then :auto
    when :always then :always
    else
      :always
    end
  else
    # enable_starttls: not provided or false
    if setting_provided?(:enable_starttls_auto)
      settings[:enable_starttls_auto] ? :auto : false
    else
      # enable_starttls_auto: not provided
      # enable_starttls: when provided then false
      # use :auto when neither enable_starttls* provided
      setting_provided?(:enable_starttls) ? false : :auto
    end
  end
end

def smtp_tls?

def smtp_tls?
  (setting_provided?(:tls) && settings[:tls]) || (setting_provided?(:ssl) && settings[:ssl])
end

def ssl_context

Just returns openssl verify mode for Ruby 1.8.x
Allow SSL context to be configured via settings, for Ruby >= 1.9
def ssl_context
  openssl_verify_mode = settings[:openssl_verify_mode]
  if openssl_verify_mode.kind_of?(String)
    openssl_verify_mode = OpenSSL::SSL.const_get("VERIFY_#{openssl_verify_mode.upcase}")
  end
  context = Net::SMTP.default_ssl_context
  context.verify_mode = openssl_verify_mode if openssl_verify_mode
  context.ca_path = settings[:ca_path] if settings[:ca_path]
  context.ca_file = settings[:ca_file] if settings[:ca_file]
  context
end

def start_smtp_session(&block)

def start_smtp_session(&block)
  build_smtp_session.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication], &block)
end