开发者

problem with ActionMailer configuration in setup_email.rb

开发者 https://www.devze.com 2023-02-17 17:18 出处:网络
I spent several days trying to get this work => I would like to send email from my application localhost, without using gmail or other mail server, so I tried this:

I spent several days trying to get this work => I would like to send email from my application localhost, without using gmail or other mail server, so I tried this:

in /config/initializers/setup_email.rb

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
    :address => "localhost",
    :port => 25
}

But I got "Connection refused - connect(2)" error message like following:

Connection refused - connect(2)
/home/user1/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/smtp.rb:551:in `initialize'
/home/user1/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/smtp.rb:551:in `open'
/home/user1/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/smtp.rb:551:in `do_start'
/home/user1/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/timeout.rb:67:in `timeout'
/home/user1/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/timeout.rb:101:in `timeout'
/home/user1/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/smtp.rb:551:in `do_start'
/home开发者_StackOverflow中文版/user1/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/smtp.rb:525:in `start'
/home/user1/.rvm/gems/ruby-1.8.7-p330@myapp/gems/mail-2.2.15/lib/mail/network/delivery_methods/smtp.rb:127:in `deliver!'
/home/user1/.rvm/gems/ruby-1.8.7-p330@myapp/gems/mail-2.2.15/lib/mail/message.rb:1967:in `do_delivery'
/home/user1/.rvm/gems/ruby-1.8.7-p330@myapp/gems/mail-2.2.15/lib/mail/message.rb:228:in `deliver'
/home/user1/.rvm/gems/ruby-1.8.7-p330@global/gems/actionmailer-3.0.3/lib/action_mailer/base.rb:401:in `deliver_mail'
/home/user1/.rvm/gems/ruby-1.8.7-p330@global/gems/activesupport-3.0.3/lib/active_support/messages.rb:52:in `instrument'
/home/user1/.rvm/gems/ruby-1.8.7-p330@global/gems/activesupport-3.0.3/lib/active_support/messages/instrumenter.rb:21:in `instrument'

...

How to configure localhost for ActionMailer?

I searched on internet, it seems I MUST provide a username and password in the configuration, and set authentication option to some value, can I configure the ActionMailer without authentication, username and password? (because my app does not have a email account for login) how to configure to make localhost email sending feature to work?


Add this to your environment.rb:

ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"


on localhost you should be uusing sendmail, a good start would be this site

ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = { 
  :location       => '/usr/sbin/sendmail', 
  :arguments      => '-i -t'
}

I know the source is old, but seems to be still valid


On localhost you could also use mailtrap. blog post on mailtrap. There is more documentation here. As far as settings go, I put this in my initializer

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.delivery_method = :test if Rails.env.test?
ActionMailer::Base.smtp_settings = {
 :address => "localhost"
 :port => 2525,
 :domain => "localhost",
}

This method worked for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消