邮件不在ROR中从本地服务器发送

时间:2014-01-20 08:04:31

标签: ruby-on-rails-4

我尝试在本地发送邮件(从我的本地服务器)但是服务器没有启动.....为什么发生这种情况...... ????

位指示

class ForestsController < ApplicationController
  before_action :set_forest, only: [:show, :edit, :update, :destroy]

  # GET /forests
  # GET /forests.json
  def index
    @forests = Forest.all
  end

  # GET /forests/1
  # GET /forests/1.json
  def show
  end

  # GET /forests/new
  def new
    @forest = Forest.new
  end

  # GET /forests/1/edit
  def edit
  end

  # POST /forests
  # POST /forests.json
  def create
    @forest = Forest.new(forest_params)

    respond_to do |format|
      if @forest.save

        UserMailer.welcome_user(@forest).deliver

        format.html { redirect_to @forest, notice: 'Your Message send successfully.' }
        format.json { render action: 'show', status: :created, location: @forest }
      else
        format.html { render action: 'new' }
        format.json { render json: @forest.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /forests/1
  # PATCH/PUT /forests/1.json
  def update
    respond_to do |format|
      if @forest.update(forest_params)
        format.html { redirect_to @forest, notice: 'Your Message updated and send successfully.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @forest.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /forests/1
  # DELETE /forests/1.json
  def destroy
    @forest.destroy
    respond_to do |format|
      format.html { redirect_to forests_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_forest
      @forest = Forest.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def forest_params
      params.require(:forest).permit(:name, :email, :phone, :body)
    end
end

userMailer

class UserMailer < ActionMailer::Base
  default from: "tayyabzahid96@gamil.com"

  def welcome_user(forest)
    @forest = forest
    mail(to: @forest.email, subject: "Hello Man", body: @forest.body)
  end

教职员

= form_for :forest, url:{:action =>"create"}, html:{:class => "form-horizontal" } do |f|
  %div.form-group
    = f.label :name, 'Name', {:class => 'col-lg-2 control-label'} 
    %div.col-lg-3
      = f.text_field :name, {:class => 'form-control', :placeholder => "Your Name"}
  %div.form-group
    = f.label :email, 'Email', {:class => 'col-lg-2 control-label'}
    %div.col-lg-3
      = f.text_field :email, {:class => 'form-control', :placeholder => "someone@example.com"}
  %div.form-group
    = f.label :phone, 'Phone', {:class => 'col-lg-2 control-label'}
    %div.col-lg-3
      = f.text_field :phone, {:class => 'form-control', :placeholder => "Mobile No"}
  %div.form-group
    = f.label :body, 'Body', {:class => 'col-lg-2 control-label'}
    %div.col-lg-3
      = f.text_area :body, {:class => 'form-control', :placeholder => "Your Message"}
  %div.form-group
    %div.col-lg-offset-2.col-lg-10
      = f.submit :class => "btn btn-primary", :value => "Contact Us"

developemnt.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = 
{
address: 'smtp.gmail.com',
port: 587,
domain: CONFIG[:mail_domain],
authentication: 'plain',
enable_starttls_auto: true,
user_name: CONFIG[:mail_username],
password: CONFIG[:mail_password]   
}

错误'服务器未启动'

/Users/T1/forestBankPakistan/config/environments/development.rb:24:in `block in <top (required)>': uninitialized constant CONFIG (NameError)
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/railtie/configurable.rb:24:in `class_eval'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/railtie/configurable.rb:24:in `configure'
    from /Users/T1/forestBankPakistan/config/environments/development.rb:1:in `<top (required)>'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/engine.rb:591:in `block (2 levels) in <class:Engine>'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/engine.rb:590:in `each'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/engine.rb:590:in `block in <class:Engine>'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/initializable.rb:30:in `run'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/T1/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
    from /Users/T1/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
    from /Users/T1/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:210:in `block (2 levels) in each_strongly_connected_component_from'
    from /Users/T1/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
    from /Users/T1/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:209:in `block in each_strongly_connected_component_from'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/initializable.rb:44:in `each'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/initializable.rb:44:in `tsort_each_child'
    from /Users/T1/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:203:in `each_strongly_connected_component_from'
    from /Users/T1/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
    from /Users/T1/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:180:in `each'
    from /Users/T1/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
    from /Users/T1/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/application.rb:215:in `initialize!'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/T1/forestBankPakistan/config/environment.rb:5:in `<top (required)>'
    from /Users/T1/forestBankPakistan/config.ru:3:in `block in <main>'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
    from /Users/T1/forestBankPakistan/config.ru:in `new'
    from /Users/T1/forestBankPakistan/config.ru:in `<main>'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands/server.rb:48:in `app'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands/server.rb:75:in `start'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands.rb:76:in `block in <top (required)>'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands.rb:71:in `tap'
    from /Users/T1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.2/lib/rails/commands.rb:71:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

为什么会发生这种情况......我无法理解......?任何建议

1 个答案:

答案 0 :(得分:0)

那是你的问题:

uninitialized constant CONFIG (NameError)

这是您的解决方案(将CONFIG var替换为您的实际数据):

#config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
    address: 'smtp.gmail.com',
    port: 587,
    domain: *************,
    authentication: 'plain',
    enable_starttls_auto: true,
    user_name: *************,
    password: ************  
}

我建议您使用Figaro gem来访问ENV个变量(因此您可以像CONFIG一样使用它们,但ENV}