邮件不在ROR中本地发送

时间:2014-01-17 06:13:08

标签: devise ruby-on-rails-4 mailer

我正在尝试在本地发送邮件,但它不发送邮件。实际发生了什么......?

位指示

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

  # 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

  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@gmail.com"

  def welcome_user(forest)
    @forest = forest
    mail(to: @forest.email, subject: "Hello Man", body: @forest.body)
  end
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"

发生了什么......?为什么不发邮件......?我无法理解这个问题....任何有关此问题的建议对我有帮助...... ????

1 个答案:

答案 0 :(得分:0)

您是如何在development.rb中配置邮件的?以下作品对我很有用:

  # Don't care if the mailer can't send.   
  config.action_mailer.raise_delivery_errors = true

  # Change mail delvery to either :smtp, :sendmail, :file, :test
  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]   
  }