Ven*_*atK 42 ruby ruby-on-rails actionmailer ruby-on-rails-3
很长一段时间以来,我一直在使用rails.现在我在ActionMailer中面临一个小问题.我想在用户注册时发送电子邮件以确认他的注册.我能够发送电子邮件中的发展模式,但如果作为没有在生产模式.
异常Errno :: ECONNREFUSED:连接被拒绝 -每次调用传递方法时,connect(2)都会出现.
我写了以下代码.
我的SMTP配置看起来:
config.action_mailer.default_url_options = {:host =>"localhost:3000"}
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE,
:ssl => true,
:enable_starttls_auto => true, #this is the important stuff!
:address => 'smtp.xxxx.xxx',
:port => xxx,
:domain => 'xxxxxx',
:authentication => :plain,
:user_name => 'xxxxxxx@xxx.xxx',
:password => 'xxxxxxxxx'
}
Run Code Online (Sandbox Code Playgroud)
在控制器中,我写了以下内容:
def confirm_registration_in_c
@user = User.find_by_email(asdf123@gmail.com)
if @user
UserMailer.confirm_registration(@user).deliver
end
end
Run Code Online (Sandbox Code Playgroud)
在我的user_mailer.rb中:
class UserMailer < ActionMailer::Base
default from: "from@example.com"
def confirm_registration(user)
@user = user
@user_name = @user.name
email = @user.email
mail(:to => email, :subject => "Reset your password")
end
end
Run Code Online (Sandbox Code Playgroud)
我能够在本地主机中以开发模式发送电子邮件,但我无法在专用服务器中发送电子邮件.
有人能帮帮我吗?
opp*_*pih 22
在我的情况下,当我尝试通过发送电子邮件Rails应用程序教程时遇到类似问题,Heroku日志一直告诉我
......
Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
......
在我将代码与作者代码进行比较后,我发现我没有在config/environments/production.rb文件中设置我的ActionMailer配置.
然后我意识到我只是将配置/环境/ development.rb配置为发送电子邮件,但我没有为我的config/environments/production.rb做过.
因此,当您的应用在开发和生产之间的行为不同时,您可以检查它.
我的问题与这个问题并不相同,但我觉得很多人会通过谷歌找到这个线程。
如果您使用外部 SMTP 服务(例如 sendgrid)并相应地设置了 ActionMailer,但它仍然会出现此错误:
Errno::ECONNREFUSED:连接被拒绝 - “localhost”端口 25 的 connect(2)
您可能会使用字符串键传递配置哈希,该哈希会被忽略。按键必须是符号!
如果反序列化,可能会发生这种情况,我所做的是确保键是符号:
config.action_mailer.smtp_settings = get_smtp_setting.symbolize_keys
Run Code Online (Sandbox Code Playgroud)
对于生产,你不能写
config.action_mailer.default_url_options = { :host => "localhost:3000" }
Run Code Online (Sandbox Code Playgroud)
为主机添加生产网址,例如,
config.action_mailer.default_url_options = { :host => "http://www.yourdomain.com" }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76190 次 |
| 最近记录: |