Mail allows you to send emails using SMTP. This is done by wrapping Net::SMTP in an easy to use manner.
Sending locally (to a postfix or sendmail server running on localhost) requires no special setup. Just to Mail.deliver &block or message.deliver! and it will be sent in this method.
Mail.defaults do
delivery_method :smtp, { :address => "smtp.me.com",
:port => 587,
:domain => 'your.host.name',
:user_name => '<username>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
end
Mail.defaults do
delivery_method :smtp, { :address => "smtp.gmail.com",
:port => 587,
:domain => 'your.host.name',
:user_name => '<username>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
end
Feel free to send me other examples that were tricky
Once you have the settings right, sending the email is done by:
Mail.deliver do
to 'mikel@test.lindsaar.net'
from 'ada@test.lindsaar.net'
subject 'testing sendmail'
body 'testing sendmail'
end
Or by calling deliver on a Mail message
mail = Mail.new do
to 'mikel@test.lindsaar.net'
from 'ada@test.lindsaar.net'
subject 'testing sendmail'
body 'testing sendmail'
end
mail.deliver!
# File lib/mail/network/delivery_methods/smtp.rb, line 64
64: def initialize(values)
65: self.settings = { :address => "localhost",
66: :port => 25,
67: :domain => 'localhost.localdomain',
68: :user_name => nil,
69: :password => nil,
70: :authentication => nil,
71: :enable_starttls_auto => true,
72: :openssl_verify_mode => nil
73: }.merge!(values)
74: end
Send the message via SMTP. The from and to attributes are optional. If not set, they are retrieve from the Message.
# File lib/mail/network/delivery_methods/smtp.rb, line 80
80: def deliver!(mail)
81:
82: # Set the envelope from to be either the return-path, the sender or the first from address
83: envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
84: if envelope_from.blank?
85: raise ArgumentError.new('A sender (Return-Path, Sender or From) required to send a message')
86: end
87:
88: destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
89: if destinations.blank?
90: raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message')
91: end
92:
93: message ||= mail.encoded if mail.respond_to?(:encoded)
94: if message.blank?
95: raise ArgumentError.new('A encoded content is required to send a message')
96: end
97:
98: smtp = Net::SMTP.new(settings[:address], settings[:port])
99: if settings[:enable_starttls_auto]
100: if smtp.respond_to?(:enable_starttls_auto)
101: unless settings[:openssl_verify_mode]
102: smtp.enable_starttls_auto
103: else
104: openssl_verify_mode = settings[:openssl_verify_mode]
105: if openssl_verify_mode.kind_of?(String)
106: openssl_verify_mode = "OpenSSL::SSL::VERIFY_#{openssl_verify_mode.upcase}".constantize
107: end
108: context = Net::SMTP.default_ssl_context
109: context.verify_mode = openssl_verify_mode
110: smtp.enable_starttls_auto(context)
111: end
112: end
113: end
114:
115: smtp.start(settings[:domain], settings[:user_name], settings[:password], settings[:authentication]) do |smtp|
116: smtp.sendmail(message, envelope_from, destinations)
117: end
118:
119: self
120: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.