如何通过本地主机发送邮件?

how to send mail through the localhost?

请帮忙解决问题。

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

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

我使用 gem 设计。我使用 'forgot youre password'-函数(设计可恢复模块)。结果没收到邮件信息。但是我在哪里可以找到试图发送信件的标记(日志)?有没有存放它们的目录?

尝试 gem mailcatcher 用于开发目的。

http://mailcatcher.me/

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

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

您需要编辑 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'
}

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

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