如何通过localhost发送邮件?

时间:2015-07-25 07:12:32

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

请帮助解决问题。

我在localhost上使用webrick服务器。在development.rb我注册:

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

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

2 个答案:

答案 0 :(得分:1)

尝试使用gem mailcatcher进行开发。

http://mailcatcher.me/

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

此gem将在浏览器中为您提供一个选项卡,其中除非您删除它们,否则所有电子邮件都将存在。

答案 1 :(得分: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'
}

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

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

相关问题