Class: Mail::SMTP
- Inherits:
-
Object
- Object
- Mail::SMTP
- Defined in:
- lib/mail/network/delivery_methods/smtp.rb
Overview
Sending Email with SMTP
Mail allows you to send emails using SMTP. This is done by wrapping Net::SMTP in an easy to use manner.
Sending via SMTP server on Localhost
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.
Sending via MobileMe
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 }
end
Sending via GMail
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 }
end
Configuring TLS/SSL and STARTTLS
A few remarks:
-
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.
-
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 providing a truthy value for ‘enable_starttls`, the `enable_starttls_auto`-flag will be ignored.
-
when none of ‘tls`, `ssl`, `enable_starttls` or `enable_starttls_auto` is set, the fallback will be `enable_starttls` `:auto`.
Certificate verification
When using TLS, some mail servers provide certificates that are self-signed or whose names do not exactly match the hostname given in the address. OpenSSL will reject these by default. The best remedy is to use the correct hostname or update the certificate authorities trusted by your ruby. If that isn’t possible, you can control this behavior with an :openssl_verify_mode setting. Its value may be either an OpenSSL verify mode constant (OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER), or a string containing the name of an OpenSSL verify mode (none, peer).
Others
Feel free to send me other examples that were tricky
Delivering the email
Once you have the settings right, sending the email is done by:
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!
Constant Summary collapse
- DEFAULTS =
{ :address => 'localhost', :port => 25, :domain => 'localhost.localdomain', :user_name => nil, :password => nil, :authentication => nil, :enable_starttls => nil, :enable_starttls_auto => nil, :openssl_verify_mode => nil, :ssl => nil, :tls => nil, :open_timeout => 5, :read_timeout => 5 }
Instance Attribute Summary collapse
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
- #deliver!(mail) ⇒ Object
-
#initialize(values) ⇒ SMTP
constructor
A new instance of SMTP.
Constructor Details
Instance Attribute Details
#settings ⇒ Object
Returns the value of attribute settings.
85 86 87 |
# File 'lib/mail/network/delivery_methods/smtp.rb', line 85 def settings @settings end |
Instance Method Details
#deliver!(mail) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/mail/network/delivery_methods/smtp.rb', line 107 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 |