从localhost发送电子邮件

con*_*isi 26 email ruby-on-rails localhost

我正在尝试了解rails中的电子邮件.我在localhost上开发一些东西.是否可以从localhost发送电子邮件来说一个像gmail这样的普通邮件帐户?我有安装邮件服务器吗?我刚刚开发了标准导轨安装.

Chu*_*ang 29

rails 4.0的更新
现在您需要这些代码才能使其工作:

# I recommend using this line to show error
config.action_mailer.raise_delivery_errors = true

ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.gmail.com',
  :domain         => 'mail.google.com',
  :port           => 587,
  :user_name      => 'foo@gmail.com',
  :password       => '******',
  :authentication => :plain,
  :enable_starttls_auto => true
}
Run Code Online (Sandbox Code Playgroud)

  • 并且不要忘记重新启动rails服务器 (2认同)

Mik*_*l S 16

您可以将ActionMailer设置为使用config/environment.rb中的类似内容使用Gmail的SMTP服务器:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
    :address        => 'smtp.gmail.com',
    :domain         => '<your domain>',
    :port           => 587,
    :user_name      => '<your gmail>',
    :password       => '<your password>',
    :authentication => :plain
}
Run Code Online (Sandbox Code Playgroud)

编辑:如果遇到任何困难,请将配置设置为显示错误:

ActionMailer::Base.raise_delivery_errors = true
Run Code Online (Sandbox Code Playgroud)


Ana*_*hah 6

看看ActionMailer.在RAILS_ROOT/config/environment/,有一个文件用于不同的环境(开发,测试,生产),可配置的设置放在这些文件中

你像这样指定delivery_method,

ActionMailer::Base.delivery_method = :sendmail
Run Code Online (Sandbox Code Playgroud)

或者如果你想

ActionMailer::Base.delivery_method = :smtp
Run Code Online (Sandbox Code Playgroud)

Mikael S发布了一个详细的设置示例

HTH