ruby-on-rails - 例如,如何使用 gmail 帐户从 ruby​​ 发送电子邮件?

标签 ruby-on-rails ruby

我是 ruby​​ on Rails 的新手,我读过很多关于这方面的教程,但我仍然不明白出了什么问题。

我已经设置了environment.rb,底部包含以下几行:

ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.default_content_type = "text/html"

ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.gmail.com',
  :port           => 25,
  :domain         => 'gmail.com',
  :authentication => :login,
  :user_name      => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="caa7b3a4aba7af8aada7aba3a6e4a9a5a7" rel="noreferrer noopener nofollow">[email protected]</a>',
  :password       => 'mypassword'
}

然后,我得到了我正在使用的 baseApp 附带的一些代码,它执行以下操作:

UserMailer.deliver_forgot_password(self)

其中 self 是用户对象,包含电子邮件、登录名等。

该方法(属于 UserMailer 类)具有以下代码:

  def forgot_password(user)
    setup_email(user)
    @subject += "Forgotten password instructions"
    #{user.password_reset_code}
    @body[:url]  = "http://#{Setting.get(:site_url)}/users/reset_password/#"

  end

protected
    def setup_email(user)
      @recipients  = "#{user.email}"
      @from        = "#{Setting.get(:support_name)} <#{Setting.get(:support_email)}>"
      @subject     = "[#{Setting.get(:site_name)}] "
      @sent_on     = Time.now
      @body[:user] = user

      # Get Settings
      [:site_name, :company_name, :support_email, :support_name].each do |id|
        @body[id] = Setting.get(id)
      end
    end

嗯,电子邮件很好,我已经检查过它们并进行了 raise 检查,但它没有发送......并且它也没有给我抛出异常。

现在,我在尝试发送电子邮件时收到 Errno::ECONNREFUSED 错误。现在怎么了?

谢谢, 布赖恩

最佳答案

Gmail 端口应为 587 或 465。

并添加

:tls => true,

你有ActionMailerTLS吗? gem 安装了吗?

关于ruby-on-rails - 例如,如何使用 gmail 帐户从 ruby​​ 发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1882240/

相关文章:

ruby-on-rails - 带有嵌套参数 cUrl 的多部分 POST

ruby-on-rails - Ruby 改进和钩子(Hook)

mysql - Ruby #{} 插入换行符

ruby - 类型错误 : wrong argument type String (expected Module) when trying to use rspec

ruby - 从字符串创建正则表达式

ruby-on-rails - 从 ActiveRecord/ActiveModel JSON 输出中过滤字段(神奇!)

ruby-on-rails - 如何在 Rails 3 中的 reset_session 命令之后找到新的 session_id?

mysql - 如何在 Rails 中有效地获取这些代理 ID

ruby-on-rails - 异常通知 gem "ERROR: Failed to generate exception summary"

ruby - Ruby Matrix 类的复制/继承(核心/标准库)