Ruby on Rails,为什么我的电子邮件系统不起作用

时间:2015-04-02 17:44:25

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

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

development.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
}

application.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

寄件人/ 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

寄件人/ application_mailer:

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

视图/ 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>'

有人可以帮忙。

1 个答案:

答案 0 :(得分:0)

替换

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
}

并重新启动您的服务器。 如果有效,请添加&#39; your_email&#39;和&#39; your_password&#39;在这样的env var中(不需要figaro gem)。

edit the ~/.bashrc file

并添加

export GMAIL_USERNAME="myname@gmail.com"

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

对于Windows:

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

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

为了在&#34;变量&#34;中添加gmail_username(你的var env的名字)。和 增加价值。

enter image description here

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