【问题标题】:Ruby on Rails, why doesn't my email system workRuby on Rails,为什么我的电子邮件系统不起作用
【发布时间】:2015-06-07 18:39:44
【问题描述】:

我正在尝试向用户发送一封电子邮件,以确认他们已成功注册该网站。我已遵循指南并拥有以下文件。

开发.rb:

config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
 :address              => "smtp.gmail.com",
 :port                 => 587,
 :user_name            => ENV['email'],
 :password             => ENV['password'],
 :authentication       => "plain",
 :enable_starttls_auto => true
}

应用程序.rb:

require File.expand_path('../boot', __FILE__)
require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module ThorCinema
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true    
    config.autoload_paths << Rails.root.join('app', 'validators')
    gmail_username: 'email'
    gmail_password: 'password'
  end

end

users_controller:

def create
@user = User.new(user_params)
if @user.save
  # login is achieved by saving a user's 'id' in a session variable, 
  # accessible to all pages
   session[:user_id] = @user.id
   #UserMailer.welcome_email(@user).deliver_now
   UserMailer.welcome_email(@user).deliver
   @user.add_default_preferences
   redirect_to films_path
else
   render action: "new"
end
end

mailers/user_mailer.rb

class UserMailer < ApplicationMailer
  default from: 'no-reply@thorcinemas.com'

  def welcome_email(user)
    @user = user
    @url  = 'http://localhost:3000/users/login'
    # mail(to: @user.email, subject: 'Welcome to My Awesome Site')
    mail(to: @user.email, subject: 'Welcome to My Awesome Site')
  end
end

mailers/application_mailer:

class ApplicationMailer < ActionMailer::Base
  default from: "from@example.com"
  layout 'mailer'
end

views/welcome_email.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to Thor Cinemas, <%= @user.first_name %>!</h1>
    <p>
      You have successfully signed up to Thor Cinemas,
      your username is: <%= @user.first_name %> <%= @user.last_name %>.<br>
    </p>
    <p>
      To login to the site, just follow this link: <%= @url %>.
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

但它不起作用,当我尝试运行 rails s 时,我得到以下输出:

C:\Sites\Thor\Under Construction\ThorCinema\new\Lab\Newu\ThorCinema>rails s
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/ra
iltie.rb:196:in `method_missing': undefined method `gmail_username' for ThorCine
ma::Application:Class (NoMethodError)
        from C:/Sites/Thor/Under Construction/ThorCinema/new/Lab/Newu/ThorCinema
/config/application.rb:25:in `<class:Application>'
        from C:/Sites/Thor/Under Construction/ThorCinema/new/Lab/Newu/ThorCinema
/config/application.rb:9:in `<module:ThorCinema>'
        from C:/Sites/Thor/Under Construction/ThorCinema/new/Lab/Newu/ThorCinema
/config/application.rb:8:in `<top (required)>'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:78:in `require'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:78:in `block in server'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:75:in `tap'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:75:in `server'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

有人可以帮忙吗。

【问题讨论】:

  • 向我们展示完整的config/application.rb 文件。
  • @Зелёный 我已经更新了问题
  • 为什么 gmail_username 和 gmail_password 在 application.rb 中?
  • @JérémyButtice 我遵循了本指南:gotealeaf.com/blog/handling-emails-in-rails,这就是它所说的输入从 gmail 发送的内容。我还有什么方法可以做到?
  • 在本教程中,他解释了将环境变量添加到 application.yml 而不是 application.rb,application.yml 用于 figaro gem。

标签: ruby-on-rails ruby-on-rails-3 email ruby-on-rails-3.2 smtp


【解决方案1】:

替换

config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
 :address              => "smtp.gmail.com",
 :port                 => 587,
 :user_name            => ENV['email'],
 :password             => ENV['password'],
 :authentication       => "plain",
 :enable_starttls_auto => true
}

config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
 :address              => "smtp.gmail.com",
 :port                 => 587,
 :user_name            => 'your_email',
 :password             => 'your_password',
 :authentication       => "plain",
 :enable_starttls_auto => true
}

并重新启动您的服务器。 如果可行,则在 env var 中像这样添加“your_email”和“your_password”(不需要 figaro gem)。

edit the ~/.bashrc file

并添加

export GMAIL_USERNAME="myname@gmail.com"

您必须打开一个新的 shell 或重新启动您的终端应用程序才能继续。

对于窗户:

在桌面上,右键单击计算机图标并选择属性。 如果桌面上没有“计算机”图标,请单击“开始”按钮,右键单击“开始”菜单中的“计算机”选项,然后选择“属性”。 单击左栏中的高级系统设置链接。 在“系统属性”窗口中,单击“高级”选项卡,然后单击该选项卡底部附近的“环境变量”按钮。 在“环境变量”窗口(如下图所示)中,突出显示“系统变量”部分中的“路径”变量,然后单击“编辑”按钮。使用您希望计算机访问的路径添加或修改路径行。每个不同的目录用分号分隔,如下所示。

C:\Program Files;C:\Winnt;C:\Winnt\System32

为您在“变量”中添加 gmail_username(您的 var env 的名称)和 增加价值。

注意:您可以通过突出显示“系统变量”部分中的变量并单击“编辑”来编辑其他环境变量。如果需要新建环境变量,点击新建,输入变量名和变量值。

【讨论】:

  • bashrc file 在哪里?
  • 对不起,我很困惑,我已经听从了您对 windows 的建议,当我单击系统变量“path”上的编辑时,我得到以下信息:变量名:路径变量值:C:\Program Files\...C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys\;C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys\x64\ (我不得不缩短变量适合的价值)你说我应该做什么?
  • 您不应该更改,而是单击“新建”以添加包含您的 gmail 密码的 Windows 环境变量。然后可以通过 Rails 应用程序访问它的值,例如 ENV['gmail_email']('gmail_email' 必须与您在 Windows 上输入的变量名称值匹配......)并且值包含您真正的 gmail 用户名,你明白吗?跨度>
  • 好的,所以我添加了一个名为 gmail_email 的新系统变量,值是电子邮件地址,密码也一样?那么两个不同的变量?
  • 一个变量用于电子邮件,另一个变量用于密码。
猜你喜欢
  • 2015-05-02
  • 2017-07-11
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-03
  • 2015-10-16
  • 2019-11-28
相关资源
最近更新 更多