【问题标题】:how to send mail through the localhost?如何通过本地主机发送邮件?
【发布时间】:2015-07-25 15:22:38
【问题描述】:

请帮助解决问题。

我在本地主机上使用 webrick 服务器。在 development.rb 我注册了:

config/enviroments/development.rb:
Rails.application.configure do
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end

我使用 gem 设计。我用户“忘记了密码”-功能(设计可恢复模块)。结果没有收到电子邮件。但是我在哪里可以找到试图发送信件的标记(日志)?是否有存放它们的目录?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 devise


    【解决方案1】:

    您需要编辑您的 config/environments/development.rb 文件。

    这里有一些可以帮助您入门的内容,请将其添加到该文件中:

    # port 3000 or whatever port you are using for your localhost
    config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }    
    config.action_mailer.raise_delivery_errors = false
    config.action_mailer.delivery_method = :smtp
    
    # this line is what you want to be true, else you won't get messages!
    config.action_mailer.perform_deliveries = true 
    
    config.action_mailer.smtp_settings = {
      address: "smtp.gmail.com",
      port: 587,
      domain: 'gmail.com' # or to whatever domain your email is
      authentication: "plain",
      enable_starttls_auto: true,
      user_name: 'my_gmail_or_other_username',
      password: 'my_gmail_or_other_password'
    }
    

    现在,当您转向生产甚至只是将其推送到公共存储库时,您显然不想硬编码用户名和密码(出于安全目的)。为此,我建议您查看如何使用环境变量Figaro gem 在这里也可能派上用场,是另一种选择。

    这是默认的 'smtp' 方式,还有其他方法,例如使用前面的答案指定的 gem。

    【讨论】:

      【解决方案2】:

      尝试 gem mailcatcher 用于开发目的。

      http://mailcatcher.me/

      github:https://github.com/sj26/mailcatcher

      此 gem 将在浏览器中为您提供一个选项卡,您的所有电子邮件都将在那里,除非您将其删除。

      【讨论】:

        猜你喜欢
        • 2011-08-17
        • 2013-08-10
        • 1970-01-01
        • 2014-06-02
        • 1970-01-01
        • 1970-01-01
        • 2020-08-03
        • 2015-05-18
        • 1970-01-01
        相关资源
        最近更新 更多